Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)

Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。

(一)简介

Spring Boot Admin 提供了很多功能,如显示 name、id 和 version,显示在线状态,Loggers 的日志级别管理,Threads 线程管理,Environment 管理等。

(二)Spring Boot Admin 是由服务端和客户端组成

在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的要被监控的应用作为 Client 端,基于这种的配置如下步骤:

2.1 Server

2.1.1 添加相关依赖

  
  
  1. <dependency>
  2. <groupId>de.codecentric </groupId>
  3. <artifactId>spring-boot-admin-starter-server </artifactId>
  4. <version>2.0.1-SNAPSHOT </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot </groupId>
  8. <artifactId>spring-boot-starter-web </artifactId>
  9. </dependency>
2.1.2 启动类添加注解,开启监控

  
  
  1. @Configuration
  2. @EnableAutoConfiguration
  3. @EnableAdminServer
  4. public class SpringBootAdminApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(SpringBootAdminApplication.class, args);
  7. }
  8. }
2.1.3 配置文件

  
  
  1. server:
  2. port: 8788

2.2 Client

2.2.1 添加相关依赖

  
  
  1. <dependency>
  2. <groupId>de.codecentric </groupId>
  3. <artifactId>spring-boot-admin-starter-client </artifactId>
  4. <version>2.0.0 </version>
  5. </dependency>
2.2.2 配置文件

  
  
  1. spring.boot.admin.client.url: "http://localhost:8788"
  2. management.endpoints.web.exposure. include: "*"

以上的配置,就可以实现 Spring Boot 项目中 Spring Boot Admin 监控其他应用了,但是这不是我们的重点,详细信息请参考官网文档:http://codecentric.github.io/spring-boot-admin/2.0.0/,我们的重点是在 Spring Cloud 中使用 Spring Boot Admin 监控 Spring Cloud 的服务,下面我们就详细的讲解在 Spring Cloud 中搭建 Spring Boot Admin

(三)在 Spring Cloud 中基于 Eureka 的 Spring Boot Admin 的搭建

3.1 启动之前的项目 eureka server,端口8761

3.2 新建 module(springboot-admin),创建步骤参考上篇

3.2.1 添加相关依赖,pom文件:

  
  
  1. <dependency>
  2. <groupId>de.codecentric </groupId>
  3. <artifactId>spring-boot-admin-starter-server </artifactId>
  4. <version>2.0.1-SNAPSHOT </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot </groupId>
  8. <artifactId>spring-boot-starter-web </artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot </groupId>
  12. <artifactId>spring-boot-starter-security </artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.cloud </groupId>
  16. <artifactId>spring-cloud-starter-netflix-eureka-client </artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.jolokia </groupId>
  20. <artifactId>jolokia-core </artifactId>
  21. </dependency>

由于项目中使用的是 spring boot 2.0 版本,所以这里要使用 spring boot admin 的最新版本(还未正式发布),本人测试使用 spring boot admin 2.0.0 版本会有问题。

3.2.2 启动类添加注解

  
  
  1. @Configuration
  2. @EnableAutoConfiguration
  3. @EnableAdminServer
  4. @EnableEurekaClient
  5. public class SpringBootAdminApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(SpringBootAdminApplication.class, args);
  8. }
  9. @Profile( "insecure")
  10. @Configuration
  11. public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
  12. @Override
  13. protected void configure(HttpSecurity http) throws Exception {
  14. http.authorizeRequests().anyRequest().permitAll() //
  15. .and().csrf().disable();
  16. }
  17. }
  18. @Profile( "secure")
  19. @Configuration
  20. public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  21. private final String adminContextPath;
  22. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  23. this.adminContextPath = adminServerProperties.getContextPath();
  24. }
  25. @Override
  26. protected void configure(HttpSecurity http) throws Exception {
  27. // @formatter:off
  28. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  29. successHandler.setTargetUrlParameter( "redirectTo");
  30. http.authorizeRequests()
  31. .antMatchers(adminContextPath + "/assets/**").permitAll()
  32. .antMatchers(adminContextPath + "/login").permitAll()
  33. .anyRequest().authenticated()
  34. .and()
  35. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
  36. .logout().logoutUrl(adminContextPath + "/logout").and()
  37. .httpBasic().and()
  38. .csrf().disable();
  39. // @formatter:on
  40. }
  41. }
  42. }

SecurityPermitAllConfig和SecuritySecureConfig的配置是 Spring Boot Admin 官方给的配置,是对 url 进行安全认证等配置,照着配置即可。@EnableEurekaClient 注解是把 Spring Boot Admin 注册到 Eureka 里,这样 Spring Boot Admin 就可以发现注册到 Eureka 里的其他服务实例,@EnableAdminServer 注解是开启监控功能。

3.2.3 配置文件

官方有给出示例,主要是配置 eureka 的地址,这里要强调说明的一点,由于 Spring Boot 2.0 的 Actuator 只暴露了 /health、/info 两个端口(为了安全考虑),所以要配置 management.endpoints.web.exposure.include 的属性,下面的配置文件中暴力了一点,配置暴露了所有的端点,由于 Spring Boot Admin 有 web UI 管理界面,配置了登录的用户名密码如下,使用了 security.user 的属性,其他的详细配置,可以查看官方文档。


  
  
  1. spring:
  2. application:
  3. name: spring-boot-admin
  4. profiles:
  5. active:
  6. - secure
  7. server:
  8. port: 8788
  9. # tag::configuration-eureka[]
  10. eureka: #<1>
  11. instance:
  12. leaseRenewalIntervalInSeconds: 10
  13. health-check-url-path: /actuator/health
  14. client:
  15. registryFetchIntervalSeconds: 5
  16. serviceUrl:
  17. defaultZone: ${EUREKA_SERVICE_URL:http: //localhost:8761}/eureka/
  18. management:
  19. endpoints:
  20. web:
  21. exposure:
  22. include: "*" #<2>
  23. endpoint:
  24. health:
  25. show-details: ALWAYS
  26. # end::configuration-eureka[]
  27. ---
  28. spring:
  29. profiles: insecure
  30. ---
  31. spring:
  32. profiles: secure
  33. security:
  34. user:
  35. name: "user"
  36. password: "password"
  37. eureka:
  38. instance:
  39. metadata-map:
  40. user.name: "user" #These two are needed so that the server
  41. user.password: "password" #can access the protected client endpoints
3.2.4 启动 spring boot admin 服务,界面如下:


用户名密码即上面的配置,user/password登录成功后


此时由于 Eureka 里只有 Spring Boot Admin 自身已注册,所以其监控列表里只有自己,下面我们启动其他的服务,让其注册到 Eureka 里。

3.2.5 启动 spring-demo-service 服务

(之前文章里现有的服务,可以查找此前的文章,或者查看文末的源码下载),在启动前,还有一处要配置,就是在 3.2.3 里说的,Actuator 在 spring boot 2.0 版本后,只暴露了两个端点,所以此时启动,监控不到所需的信息,下面修改配置文件如下:


  
  
  1. server:
  2. port: 8281
  3. eureka:
  4. client:
  5. serviceUrl:
  6. # 向每个注册中心注册
  7. defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  8. spring:
  9. application:
  10. name: spring-demo-service
  11. management:
  12. endpoints:
  13. web:
  14. exposure:
  15. include: '*'
  16. endpoint:
  17. health:
  18. show-details: ALWAYS

此时启动 spring-demo-service,发现监控列表里 spring-demo-service 已经有了


点击 SPRING-DEMO-SERVICE 进入,页面如下,可以看到有我们之前介绍的一些功能。



3.2.6 其他的配置

如果想要显示版本信息,配置文件中加入 info.version=1.0.0 可以配置版本信息

源码下载:https://github.com/shmilyah/spring-cloud-componets
转载自:https://blog.csdn.net/hubo_88/article/details/80671192



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值