Spring boot admin - 微服務監測demo
阿新 • • 發佈:2019-02-15
inf man 顯示 str version cti location url tid
因為服務器的不穩定,而且小的服務越來越多,添加一個微服務的監測會更放心一些。
參考: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
- 按文檔說明,最好創建一個新的Project用於監測。創建到選擇Dependencies的時候,發現Ops下可以選Spring Boot Admin(Server)
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 publicclass MonitorApplication { public static void main(String[] args) { SpringApplication.run(MonitorApplication.class, args); } }
- application.properties(以下配置為了在微服務狀態變化時發送郵件)
spring.mail.host=XXX.123.net spring.mail.port=25 spring.mail.username=username spring.mail.password=123456 spring.boot.admin.notify.mail.from=[email protected] spring.boot.admin.notify.mail.to[email protected]
- pom.xml
<?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的相關配置。
- pom.xml(註意!client的version最好和spring-boot-starter-parent的version保持一致,否則可能會出現運行錯誤)
<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)
Spring boot admin - 微服務監測demo