Use spring-boot-admin to monitor spring-boot services

 

 

 

spring-boot-admin, or SBA for short, is a monitoring tool for UI beautification and encapsulation of spring-boot's actuator interface. he can: Browse the basic information of all monitored spring-boot projects in the list , detailed Health information, memory information, JVM information, garbage collection information, various configuration information (such as data source, cache list and hit rate), etc., you can also directly Modify the level of the logger.
 
In just a few simple steps, you can configure and use SBA (divided into monitoring end and monitored end):
Monitoring end:
1. Create a project (omitted)
2. Introduce dependencies:
[html] view plain copy
  1. <dependency>  
  2.     <groupId>de.codecentric</groupId>  
  3.     <artifactId>spring-boot-admin-server</artifactId>  
  4.     <version>1.5.0</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>de.codecentric</groupId>  
  8.     <artifactId>spring-boot-admin-server-ui</artifactId>  
  9.     <version>1.5.0</version>  
  10. </dependency>  
 
3、配置文件(application.yml)配置(可选):
[plain] view plain copy
  1. spring:  
  2.   application:  
  3.     name: svc-monitor  
  4.   boot:  
  5.     admin:  
  6.       context-path: /sba    # 配置访问路径为:http://localhost:64000/svc-monitor/sba  
  7. server:  
  8.   port: 64000  
  9.   context-path: /svc-monitor/ #统一为访问的url加上一个前缀  
以上配置是为了指定一个特别的访问路径。如果不这样配置,则访问路径为:http://localhost:64000
 
4、使用@EnableAdminServer注解激活SBA:
[java] view plain copy
  1. @SpringBootApplication  
  2. @EnableScheduling  
  3. @EnableAdminServer  
  4. public class SvcMonitorApplication {  
  5.     public static void main(String[] args) {  
  6.         SpringApplication.run(SvcMonitorApplication.class, args);  
  7.     }  
  8. }  
 
被监控端(spring-boot项目)向监控端注册自己:
1、添加依赖:
[html] view plain copy
  1. <dependency>  
  2.     <groupId>de.codecentric</groupId>  
  3.     <artifactId>spring-boot-admin-starter-client</artifactId>  
  4.     <version>1.5.0</version>  
  5. </dependency>  

2、配置文件(application.yml)配置:
[plain] view plain copy
  1. spring:  
  2.   boot:  
  3.     admin:  
  4.       client:  
  5.         prefer-ip: true # 解决windows下运行时无法识别主机名的问题  
  6.       url: http://localhost:64000/svc-monitor # 向服务端注册的地址  
  7. management:  
  8.   port: 64001  
  9.   security:  
  10.     enabled: false # spring-boot 1.5.2之后严格执行安全策略,所以需要配置这个为false  
  11. info: #定义各种额外的详情给服务端显示  
  12.   app:  
  13.     name: "@project.name@" #从pom.xml中获取  
  14.     description: "@project.description@"  
  15.     version: "@project.version@"  
  16.     spring-boot-version: "@project.parent.version@"  
 
3、其他配置:
如果需要显示项目版本号,需要在pom.xml中添加这个(build-info):
[html] view plain copy
  1. <build>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <groupId>org.springframework.boot</groupId>  
  5.             <artifactId>spring-boot-maven-plugin</artifactId>  
  6.             <executions>  
  7.                 <execution>  
  8.                     <goals>  
  9.                         <goal>build-info</goal>  
  10.                     </goals>  
  11.                 </execution>  
  12.             </executions>  
  13.         </plugin>  
  14.     </plugins>  
  15. </build>  

4、问题解决:
如果发现被监控端启动的时候出现 InetAddress.getLocalHost() throws UnknownHostException错误,是因为没配置本机机器名和ip的对应关系。
解决方法:
编辑hosts文件:
vi /etc/hosts
添加ip和机器名的关联:192.168.0.31 host31 myhost-31
 
监控端和被监控端都启动后,访问:http://localhost:64000/svc-monitor/sba,就可以看到被监控服务的各种详情了。
 
以上是被监控端主动注册法。
 
还有另外一种方法是:如果被监控端已经使用了Spring Cloud向Eureka注册了服务,则可以由监控端直接去Euraka中发现并监控这个服务。此方法调试起来比较复杂,这里先不介绍了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326355316&siteId=291194637