spring-boot-admin,简称SBA,是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。他可以:
在列表中
浏览所有被监控spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。
只需简单几步,就可以配置和使用SBA(分为监控端和被监控端):
监控端:
1、创建项目(略)
2、引入依赖:
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-server</artifactId>
- <version>1.5.0</version>
- </dependency>
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-server-ui</artifactId>
- <version>1.5.0</version>
- </dependency>
3、配置文件(application.yml)配置(可选):
- spring:
- application:
- name: svc-monitor
- boot:
- admin:
- context-path: /sba # 配置访问路径为:http://localhost:64000/svc-monitor/sba
- server:
- port: 64000
- context-path: /svc-monitor/ #统一为访问的url加上一个前缀
4、使用@EnableAdminServer注解激活SBA:
- @SpringBootApplication
- @EnableScheduling
- @EnableAdminServer
- public class SvcMonitorApplication {
- public static void main(String[] args) {
- SpringApplication.run(SvcMonitorApplication.class, args);
- }
- }
被监控端(spring-boot项目)向监控端注册自己:
1、添加依赖:
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-client</artifactId>
- <version>1.5.0</version>
- </dependency>
2、配置文件(application.yml)配置:
- spring:
- boot:
- admin:
- client:
- prefer-ip: true # 解决windows下运行时无法识别主机名的问题
- url: http://localhost:64000/svc-monitor # 向服务端注册的地址
- management:
- port: 64001
- security:
- enabled: false # spring-boot 1.5.2之后严格执行安全策略,所以需要配置这个为false
- info: #定义各种额外的详情给服务端显示
- app:
- name: "@project.name@" #从pom.xml中获取
- description: "@project.description@"
- version: "@project.version@"
- spring-boot-version: "@project.parent.version@"
3、其他配置:
如果需要显示项目版本号,需要在pom.xml中添加这个(build-info):
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>build-info</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </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中发现并监控这个服务。此方法调试起来比较复杂,这里先不介绍了。
原文地址:https://blog.csdn.net/clementad/article/details/70613209