一、工具及说明
开发工具:eclipse Neon.3 Release (4.6.3)
框架:spring boot 2.0.6
服务注册模式监控方式是:
监控服务端和被监控的微服务(即普通模式中的监控客户端),都注册到服务注册中心。
监控服务端从服务注册中心取得所有注册的微服务表。
调用微服务表中的微服务的健康检查节点,获取这些微服务的健康状态,达到监控的目的。
所以,被监控的微服务不需要向监控服务端报告自己的地址,也就不需要引入admin客户端依赖。
二、创建
服务注册模式是在普通模式上进行改造
- 创建服务注册中心springbootadmin-discovery,地址为:http://localhost:8080/eureka/
- 改造服务端springbootadmin-server
2.1 修改pom文件
pom文件中加入spring cloud依赖管理,注释掉原spring-boot-admin-dependencies依赖管理
加入服务注册客服端依赖spring-cloud-starter-netflix-eureka-client
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- admin监控服务端 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.4</version>
</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- <dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> -->
2.2 修改配置文件
在配置文件中加入服务注册中心地址配置
server:
port: 8081
spring:
application:
name: admin-server
#注册中心配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
instance:
prefer-ip-address: false
2.3 修改启动类
在启动类上加上注解@EnableDiscoveryClient,使之启动时注册到服务注册中心
@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
- 改造客户端springbootadmin-client
3.1 修改pom文件
pom文件中加入spring cloud依赖管理。
注释掉原spring-boot-admin-dependencies依赖管理。
加入服务注册客服端依赖spring-cloud-starter-netflix-eureka-client。
加入健康检查依赖spring-boot-starter-actuator。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- admin监控客户端 -->
<!-- <dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency> -->
<!-- 健康检查模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- <dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> -->
3.2 修改配置文件
在配置文件中加入服务注册中心地址配置。
去掉admin监控配置
server:
port: 8082
spring:
application:
name: admin-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
instance:
prefer-ip-address: false
#spring boot2,需要添加一下配置,暴露所有节点
management:
endpoints:
web:
exposure:
include: '*'
3.3 修改启动类
在启动类上加上注解@EnableDiscoveryClient,使之启动时注册到服务注册中心
@EnableDiscoveryClient
@SpringBootApplication
public class SpringbootadminClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootadminClientApplication.class, args);
}
}
三、展示
启动springbootadmin-server、springbootadmin-client、springbootadmin-discovery。
在浏览器打开:http://localhost:8081。
可以看到监控了ADMIN-CLIENT、ADMIN-SERVER,admin服务端连自己也监控了,因为admin服务端也注册到了服务注册中心。
这里有一点与普通模式不一样:
当我们停掉springbootadmin-client,ADMIN-CLIENT会只在监控页面显示十几秒离线状态,然后消失掉。
而普通模式会一直显示该离线服务,直到你重启监控服务端。
所以个人倾向使用普通模式,界面监控信息更完整。