因为服务器的不稳定,而且小的服务越来越多,添加一个微服务的监测会更放心一些。
参考:https://github.com/codecentric/spring-boot-admin
http://codecentric.github.io/spring-boot-admin/2.1.2/#_what_is_spring_boot_admin
首先是用于监测其他服务的Spring boot admin
2. 按文档说明,需要几处代码的添加
package com.cnipr.pt; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableAdminServer @SpringBootApplication //已经包含Configuration和AutoConfiguration public class MonitorApplication { public static void main(String[] args) { SpringApplication.run(MonitorApplication.class, args); } }
spring.mail.host=XXX.123.net spring.mail.port=25 spring.mail.username=username spring.mail.password=123456 spring.boot.admin.notify.mail.from=xxxxx@gmail.com spring.boot.admin.notify.mail.to=xxxxx@gmail.com
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cnipr.pt</groupId> <artifactId>monitor</artifactId> <version>0.0.1-SNAPSHOT</version> <name>monitor</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-boot-admin.version>2.1.1</spring-boot-admin.version> </properties> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-mail</artifactId> </dependency> </dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
为了在SBA(Spring Boot Admin)中注册服务,要在相应的spring boot应用中添加Spring boot admin client的相关配置。
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.1.2</version> </dependency>
为了在SBA上查看到版本号,可以在pom.xml中的spring-boot-maven-plugin的配置中添加build-info。
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin>
application.properties(url指定需要注册的admin的地址; logging.pattern.file的配置可将日志显示为彩色,可不加;name显示服务名称,可不加;prefer-ip为true会显示微服务的ip地址,可不加)
spring.boot.admin.client.instance.name=mail-service
spring.boot.admin.client.instance.prefer-ip = true
spring.boot.admin.client.url=http://localhost:8080 management.endpoints.web.exposure.include=*
logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
admin显示界面(默认端口为8080)
原文:https://www.cnblogs.com/jane850113/p/10385091.html