通过Eureka、DiscoveryClient实现Spring Boot Admin管理功能
Spring Boot Admin,Eureka, DiscoveryClient
Spring Boot Admin 文档在github,我主要是根据这篇文档的2.2.2. Spring Cloud Discovery进行了简单的整理。
1. 建空maven-demo项目
选择maven-demo后,不勾选内容,填好下一步的内容,然后创建即可。
2. 新建eureka-server模块
在maven-demo项目上【右键】,【New】–>【Module】,选择【Spring Initializr】–>【Default】
在【Dependencies】中默认选【Cloud Discovery】–>【Eureka Server】,下一步,创建即可
【pom】文件不动
EurekaServerApplication.java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml
server:
port: 8761
eureka:
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 4000 #清理间隔(单位毫秒)
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
management:
port: 8770
security:
enabled: false
3. 新建admin-server模块
在maven-demo项目上【右键】,【New】–>【Module】,选择【Spring Initializr】–>【Default】
在【Dependencies】中默认选【core】,下一步,创建即可
在【pom】文件中添加一下三项依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
【注意】,由于我使用的是在线生成Spring 项目,spring-boot-starter-parent版本会是(当前)最新版1.5.8,所以,spring-boot-admin-server和spring-boot-admin-server-ui也要用最新的版本,否则会出现错误
java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/ServletRegistrationBean
我个人在出此编辑的时候就是将spring-boot-admin-server和spring-boot-admin-server-ui的版本使用成了1.3.4结果一直出现这个问题,后来换成1.5.4就OK了。
AdminServerApplication.java
@EnableAdminServer
@Configuration
@EnableDiscoveryClient
@EnableAutoConfiguration
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
【注意】在这里不要添加@SpringBootApplication注解,否则在启动后,并不能发现注册在Eureka的服务。
application.yml
server:
port: 8090
spring:
application:
name: Spring Boot Admin Server
index: 1
jackson:
serialization:
indent_output: true
boot:
admin:
url: http://localhost:${server.port}
management:
security:
enabled: false
context-path: /manage
endpoints:
health:
sensitive: false
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.index:${random.value}}
prefer-ip-address: true
hostname: eureka-client-${spring.application.index}
status-page-url-path: ${management.context-path}/info
health-check-url-path: ${management.context-path}/health
metadata-map:
management:
context-path: ${management.context-path}
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4. 创建admin-eureka-client模块
在maven-demo项目上【右键】,【New】–>【Module】,选择【Spring Initializr】–>【Default】
在【Dependencies】中默认选【Cloud Discovery】–>【Eureka Discovery】,下一步,创建即可
在【pom】文件中添加一项依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
</dependencies>
AdminEurekaClientApplication.java
@SpringBootApplication
@EnableEurekaClient
public class AdminEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminEurekaClientApplication.class, args);
}
}
application.yml
spring:
application:
name: admin-client
index: 1
server:
port: 8080
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.index:${random.value}}
prefer-ip-address: true
hostname: eureka-client-${spring.application.index}
status-page-url-path: ${management.context-path}/info
health-check-url-path: ${management.context-path}/health
metadata-map:
management:
context-path: ${management.context-path}
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
security:
enabled: false
context-path: /manage
启动
依次启动各模块,在浏览器中输入http://localhost:8761/,弹出以下界面
可以发现admin-server和admin-eureka-client模块均已在eureka-server注册成功
在浏览器中输入http://localhost:8090,弹出以下界面
发现admin-eureka-client已经加入到监控中,点击右侧的【Details】,可以观察其他详细内容:
以上就是本次Spring Boot Admin和Eureka结合实现监控的小demo,欢迎大家指正。
源码:spring-boot-admin-eureka
更多推荐
所有评论(0)