通过Eureka、DiscoveryClient实现Spring Boot Admin管理功能

来源:互联网 发布:2016电脑安全软件排行 编辑:程序博客网 时间:2024/06/05 09:10

Spring Boot Admin 文档在github,我主要是根据这篇文档的2.2.2. Spring Cloud Discovery进行了简单的整理。

1. 建空maven-demo项目

选择maven-demo后,不勾选内容,填好下一步的内容,然后创建即可。
创建maven空项目

2. 新建eureka-server模块

在maven-demo项目上【右键】,【New】–>【Module】,选择【Spring Initializr】–>【Default】
新建admin-server
在【Dependencies】中默认选【Cloud Discovery】–>【Eureka Server】,下一步,创建即可
依赖项
【pom】文件不动

EurekaServerApplication.java

@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaServerApplication.class, args);    }}

application.yml

server:  port: 8761eureka:  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】
新建admin-server
在【Dependencies】中默认选【core】,下一步,创建即可

依赖项

在【pom】文件中添加一下三项依赖
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@EnableAutoConfigurationpublic class AdminServerApplication {    public static void main(String[] args) {        SpringApplication.run(AdminServerApplication.class, args);    }}

【注意】在这里不要添加@SpringBootApplication注解,否则在启动后,并不能发现注册在Eureka的服务。

application.yml

server:  port: 8090spring:  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: /manageendpoints:  health:    sensitive: falseeureka:  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】
新建admin-server
在【Dependencies】中默认选【Cloud Discovery】–>【Eureka Discovery】,下一步,创建即可
依赖项
在【pom】文件中添加一项依赖:
actuator

<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@EnableEurekaClientpublic class AdminEurekaClientApplication {    public static void main(String[] args) {        SpringApplication.run(AdminEurekaClientApplication.class, args);    }}

application.yml

spring:  application:    name: admin-client    index: 1server:  port: 8080eureka:  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/,弹出以下界面
Eureka
可以发现admin-server和admin-eureka-client模块均已在eureka-server注册成功
在浏览器中输入http://localhost:8090,弹出以下界面
admin-eureka-client
发现admin-eureka-client已经加入到监控中,点击右侧的【Details】,可以观察其他详细内容:
详细界面
以上就是本次Spring Boot Admin和Eureka结合实现监控的小demo,欢迎大家指正。
源码:spring-boot-admin-eureka

原创粉丝点击