一、前言
通过前面的学习,虽然我们已经可以在 Spring Boot Admin 中查看应用中 Actuator 的监控信息了,但是这种方式有一点不好的地方,就是每个被监控的服务都必须配置 Spring Boot Admin 的地址,还得引入依赖。为了解决这一问题我们通过Eureka注册中心来解耦这一复杂的问题。
本节我们将 Spring Boot Admin 也注册到 Eureka 中,然后自动获取 Eureka 中注册的服务信息来统一查看。
二、代码演示
1、首先修改microservice-provider -> pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>microservice-minitor</artifactId>
<groupId>com.microservice</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>de.codecentric</groupId>-->
<!-- <artifactId>spring-boot-admin-starter-client</artifactId>-->
<!-- <version>2.2.0</version>-->
<!-- </dependency>-->
</dependencies>
</project>
2、修改microservice-provider -> application.yml
spring:
application:
name: microservice-provider
# boot:
# admin:
# client:
# url: http://localhost:8888
server:
port: 8101
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8001/register/eureka/
3、修改 microservice-monitor-server -> pom.xml,添加Eureka客户端依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>microservice-minitor</artifactId>
<groupId>com.microservice</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-monitor-server</artifactId>
<dependencies>
<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>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
4、修改 microservice-monitor-server ->application.xml,添加Eureka客户端配置
server:
port: 8888
spring:
application:
name: SpringBootAdmin
boot:
admin:
ui:
title: SpringBootAdmin-Server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8001/register/eureka/
三、运行测试
1、Eureka 注册中心:

Spring Boot Admin:

四、总结
通过上面的示例,我们可以知道,我们只需要将Spring Boot Admin 注册到Eureka注册中心里面,那么 Spring Boot Admin就可以通过Eureka获取到每个微服务的地址以及actuator暴露出来的地址。Spring Boot Admin就可以监控每个微服务实例了。
来源:https://www.cnblogs.com/yansg/p/12582067.html