一、前言
通過前面的學習,雖然我們已經可以在 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就可以監控每個微服務實例了。