Spring-boot-admin 是一個開源組織寫的服務監控的項目,git 地址 https://github.com/codecentric/spring-boot-admin
主要功能:
1. 監測服務數量、服務對應的實例數量、以及每個服務上線時長
2. 每個實例的JVM信息、CPU以及進線程等信息
3. 加載的Spring bean 信息、yml 中配置的信息、全局的配置信息、日志級別、session 信息等
4. SpringMVC 全局的映射關系, 也就是 url對應的方法信息以及處理的方法(這個可以理解為查看SpringMVC 保存URL信息的org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#mappingLookup<RequestMappingInfo, HandlerMethod> 緩存里面的信息,SpringMVC 項
目啟動過程中會加載相關的mapping 信息到這個類里面)
1. 搭建一個Spring-boot-admin server 端
1. 新建子項目 cloud-admin-server, pom 如下:
<?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"> <parent> <artifactId>cloud</artifactId> <groupId>cn.qz.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-admin-server</artifactId> <dependencies> <!--admin server 需要的依賴--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2. 新增application.yml 文件
spring:
application:
name: admin-server
server:
port: 8801
3. 新增主啟動類
package cn.qz.cloud; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAdminServer public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }
4. 啟動后訪問8081 端口,查看如下:
2. admin-client 通過http 注冊到admin-server
1. pom 增加如下依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. yml 增加配置: 主要是增加admin 的url, 配置類在de.codecentric.boot.admin.client.config.ClientProperties
server: port: 8081 spring: application: name: cloud-provider-hystrix-payment redis: port: 6379 host: localhost session: store-type: redis boot: admin: client: url: http://localhost:8801 eureka: client: register-with-eureka: true fetch-registry: true service-url: #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka defaultZone: http://localhost:7001/eureka management: endpoints: web: exposure: include: '*' endpoint: health: show-details: ALWAYS
3. 啟動server, 然后啟動client ,到8801 端口即可看到實例信息
3. 從eureka 注冊中心直接拿客戶端信息
1. 修改cloud-admin-server的pom ,增加如下引用:
<!--注冊中心,從注冊中心拿數據--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2. yml 修改
spring: application: name: admin-server server: port: 8801 eureka: client: registryFetchIntervalSeconds: 5 service-url: defaultZone: ${EUREKA_SERVICE_URL:http://localhost:7001}/eureka/ instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
3. 測試即可,admin-server 會從eureka 拿取服務數據
4. 查看單個服務信息
還可以整合k8s等,這個等之后學習完k8s 再來補充。