Spring Boot Admin极简教程


一、简介
Spring Boot Admin是一个完整的应用程序,用于监控和管理Spring Boot应用。原理是通过调用Spring Boot Actuator提供的http接口来实现的监控和管理,所以要想能够被Spring Boot Admin 的Server端管理,则Client端必须集成了Spring Boot Actuator。

  • 可监控内容:
    状态;基本信息;JVM和堆栈实时数据;应用配置信息;缓存指标;线程数据;接口调用数据

  • 可管理内容:
    按照包名调整日志级别;管理、查看和下载系统日志;报警(服务异常下线发送邮件)

AdminServer的web界面:

二、Spring-Boot集成

Spring Boot Admin是C/S结构,server端负责收集和处理client端的数据,并且可以通过向client端发送命令来管理client。

项目工程目录结构:

三、Server端

  • 引入所需的pom依赖:spring-boot-starter-web;spring-boot-admin-starter-server
  • application.yml配置文件中配置服务的端口:server.port=9000
  • 启动类上面添加EnableAdminServer的注解

完整的pom文件:


<?xml version="1.0" encoding="UTF-8"?>

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.3.RELEASE
         
    
    com.naylor
    admin-server
    0.0.1-SNAPSHOT
    admin-server
    Demo project for Spring Boot


    
        1.8
        2.3.0
    


    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            de.codecentric
            spring-boot-admin-starter-server
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    


    
        
            
                de.codecentric
                spring-boot-admin-dependencies
                ${spring-boot-admin.version}
                pom
                import
            
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




启动AdminServer后访问 http://127.0.0.1:9000/applications 可以看到“暂无应用注册”的提示

四、Client端

  • 引入所需的pom依赖:spring-boot-starter-web;spring-boot-admin-starter-client;spring-boot-actuator
  • application.propertis配置文件中增加相关配置:服务端口;服务名称;开启actuator所有端点都可以被访问;AdminServer服务端的地址

完整的pom文件:


<?xml version="1.0" encoding="UTF-8"?>

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.3.RELEASE
         
    
    com.naylor
    admin-client
    0.0.1-SNAPSHOT
    admin-client
    Demo project for Spring Boot


    
        1.8
        2.3.0
    


    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            de.codecentric
            spring-boot-admin-starter-client
        
        
            org.springframework.boot
            spring-boot-actuator
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    


    
        
            
                de.codecentric
                spring-boot-admin-dependencies
                ${spring-boot-admin.version}
                pom
                import
            
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




完整的配置文件:


spring.boot.admin.client.url=http://localhost:9000


server.port=8003
spring.application.name=admin-client


management.endpoints.web.exposure.include=*

启动AdminClient后,访问 http://127.0.0.1:8003/actuator , 先看看actuator是否正常

同时启动AdminServer和AdminClient:

访问 http://127.0.0.1:9000/wallboard 可以看到一个client端已经注册到了AdminServer之中

五、集成到Eureka

集成到Eureka之后,就不必在每一个Admin-Client的配置文件中都配置一遍Admin-Server的地址,让Admin-Server主动从Eureka中拉取Admin-Client,并获取Actuator地址,进而进行监控和管理。

1、Admin-Server的配置文件中加入Eureka-Server地址:

eureka:
  instance:
    hostname: localhost
    #eureka-client 向 eureka-server端发送心跳包的频率
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
  client:
    #是否把自己向eureka-server注册
    registerWithEureka: false
    fetch-registry: true
    #从eureka-server拉取已注册服务的列表的频率
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://${eurekaclient.username}:${eurekaclient.password}@localhost:7001/eureka/

配置了serviceUrl之后,Admin-Server就可以从Eureka中获取已经注册的微服务信息,并进行监控和管理。

至于registerWithEureka是否配置为true,取决于自己的喜欢。配置为false,Admin-Server不会注册到Eureka-Server
;配置为true,Admin-Server就会注册到Eureka-Server中,那么,意味着Admin-Server从注册中心拉取微服务的时候会把自己(即Admin-Server)也拉取下来进行监控和管理。

2、Admin-Client的配置文件中去除已经配置的Admin-Server地址:


#  注释掉配置的Admin-Server地址
#  spring.boot.admin.client.url=http://localhost:9000

注释掉配置的Admin-Server地址的目的是避免重复,因为当前的Admin-Client作为一个微服务已经注册到了Eureka-Server,那么Admin-Server就已经可以从Eureka-Server中拉取到此服务的Actuator信息。

若不注释,那么在Admin-Server的WallBoard中将看到两个重复的Admin-Client。

引用:

https://www.jianshu.com/p/1749f04105fb