糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 五分钟学会 Spring Boot Admin:微服务应用监控(小白必看 一看就会教程)

五分钟学会 Spring Boot Admin:微服务应用监控(小白必看 一看就会教程)

时间:2020-04-07 17:37:18

相关推荐

五分钟学会 Spring Boot Admin:微服务应用监控(小白必看 一看就会教程)

Spring Boot Admin:微服务应用监控

Spring Boot Admin 简介监控信息演示结合注册中心使用功能演示添加登录认证文末福利

Spring Boot Admin

可以对SpringBoot应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍。

Spring Boot Admin 简介

SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。Spring Boot Admin不仅可以监控单体应用,还可以和Spring Cloud的注册中心相结合来监控微服务应用。

Spring Boot Admin 可以提供应用的以下监控信息:

监控应用运行过程中的概览信息;度量指标信息,比如JVM、Tomcat及进程信息;环境变量信息,比如系统属性、系统环境变量以及应用配置信息;查看所有创建的Bean信息;查看应用中的所有配置信息;查看应用运行日志信息;查看JVM信息;查看可以访问的Web端点;查看HTTP跟踪信息。

创建admin-server模块

这里我们创建一个admin-server模块来作为监控中心演示其功能。

在pom.xml中添加相关依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId></dependency>

在application.yml中进行配置:

spring:application:name: admin-serverserver:port: 9301

在启动类上添加@EnableAdminServer来启用admin-server功能:

@EnableAdminServer@SpringBootApplicationpublic class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}

创建admin-client模块

这里我们创建一个admin-client模块作为客户端注册到admin-server。

在pom.xml中添加相关依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId></dependency>

在application.yml中进行配置:

spring:

application:

name: admin-client

boot:

admin:

client:

url: http://localhost:9301 #配置admin-server地址

server:port: 9305management:endpoints:web:exposure:include: '*'endpoint:health:show-details: alwayslogging:file: admin-client.log #添加开启admin的日志监控

启动admin-server和admin-client服务。

监控信息演示

访问如下地址打开Spring Boot Admin的主页:http://localhost:9301

点击wallboard按钮,选择admin-client查看监控信息;

监控信息概览;

查看日志信息,需要添加以下配置才能开启;

logging:file: admin-client.log #添加开启admin的日志监控

结合注册中心使用

Spring Boot Admin结合Spring Cloud

注册中心使用,只需将admin-server和注册中心整合即可,admin-server

会自动从注册中心获取服务列表,然后挨个获取监控信息。这里以Eureka注册中心为例来介绍下该功能。

修改admin-server

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

在application-eureka.yml中进行配置,只需添加注册中心配置即可:

spring:application:name: admin-serverserver:port: 9301eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/

在启动类上添加@EnableDiscoveryClient来启用服务注册功能:

@EnableDiscoveryClient@EnableAdminServer@SpringBootApplicationpublic class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}

修改admin-client

在pom.xml中添加相关依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

在application-eureka.yml中进行配置,删除原来的admin-server地址配置,添加注册中心配置即可:

spring:application:name: admin-clientserver:port: 9305management:endpoints:web:exposure:include: '*'endpoint:health:show-details: alwayslogging:file: admin-client.log #添加开启admin的日志监控eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/

在启动类上添加@EnableDiscoveryClient来启用服务注册功能:

@EnableDiscoveryClient@SpringBootApplicationpublic class AdminClientApplication {public static void main(String[] args) {SpringApplication.run(AdminClientApplication.class, args);}}

功能演示

启动eureka-server,使用application-eureka.yml配置启动admin-server,admin-client;查看注册中心发现服务均已注册:http://localhost:8001/查看Spring Boot Admin 主页发现可以看到服务信息:http://localhost:9301

添加登录认证

创建admin-security-server模块

在pom.xml中添加相关依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.1.5</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

在application.yml中进行配置,配置登录用户名和密码,忽略

admin-security-server的监控信息:spring:application:name: admin-security-serversecurity: # 配置登录用户名和密码user:name: macropassword: 123456boot: # 不显示admin-security-server的监控信息admin:discovery:ignored-services: ${spring.application.name}server:port: 9301eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/

对SpringSecurity进行配置,以便admin-client可以注册:

@Configurationpublic class SecuritySecureConfig extends WebSecurityConfigurerAdapter {private final String adminContextPath;public SecuritySecureConfig(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(adminContextPath + "/");http.authorizeRequests()//1.配置所有静态资源和登录页可以公开访问.antMatchers(adminContextPath + "/assets/**").permitAll().antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and()//2.配置登录和登出路径.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout().logoutUrl(adminContextPath + "/logout").and()//3.开启http basic支持,admin-client注册时需要使用.httpBasic().and().csrf()//4.开启基于cookie的csrf保护.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())//5.忽略这些路径的csrf保护以便admin-client注册.ignoringAntMatchers(adminContextPath + "/instances",adminContextPath + "/actuator/**");}}

修改启动类,开启AdminServer及注册发现功能:

@EnableDiscoveryClient@EnableAdminServer@SpringBootApplicationpublic class AdminSecurityServerApplication {public static void main(String[] args) {SpringApplication.run(AdminSecurityServerApplication.class, args);}}

启动eureka-server,admin-security-server,访问Spring Boot Admin主页发现需要登录才能访问:http://localhost:9301

粉丝福利

微信搜一搜「码上代码」回复【面试资料】,【技术书籍】有我准备的一线大厂面试资料和简历模板和java必看技术书籍

大家好,感谢各位人才

能看到这里的都是您已是佼佼者

我会持续为大家做技术分享

预知下篇如何

点赞、收藏和评论,我们下期见!

如果觉得《五分钟学会 Spring Boot Admin:微服务应用监控(小白必看 一看就会教程)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。