引入依赖
<dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- Spring Cloud End --> </dependencies>应用程序入口
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }application.yml
spring: application: name: admin-demo-eureka server: port: 8761 eureka: instance: hostname: localhost client: service-url: default-zone: ${eureka.instance.hostname}:${server.port}/eureka register-with-eureka: false fetch-registry: false management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS配置成功后启动应用程序,访问 localhost:8761,可以看到类似下图的 UI 界面:
创建 SBA 服务端引入依赖
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.1.0</version> </dependency> <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.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>应用程序入口
@SpringBootApplication @EnableAdminServer @EnableDiscoveryClient public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }application.yml 配置
spring: application: name: admin-demo-admin-server server: port: 8769 eureka: client: registryFetchIntervalSeconds: 5 service-url: defaultZone: ${EUREKA_SERVICE_URL::8761}/eureka/ instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS 创建 SBA 客户端引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</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>applicaion.yml 配置文件
spring: application: name: admin-demo-admin-client server: port: 8762 eureka: instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health client: registryFetchIntervalSeconds: 5 service-url: defaultZone: ${EUREKA_SERVICE_URL::8761}/eureka/ management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS应用程序入口
@SpringBootApplication @EnableDiscoveryClient public class AdminClientApplication { public static void main(String[] args) { SpringApplication.run(AdminClientApplication.class, args); } }将三个应用启动后,访问 localhost:8769 就可以监控各个应用的运行情况了:
四、集成邮件通知SBA 中也可以集成邮件通知,当注册的服务下线、宕机时,向指定的邮箱发送信息邮件。其配置也十分容易,只需要在 yaml 配置文件中配置邮箱的发送方和邮件的收件方,以及在 pom 文件中引入 `` 的依赖即可。下面演示这段配置: