Spring Cloud -Hoxton.RELEASE(六):监控中心-Admin

Spring Boot Admin

Admin的官网对Admin的描述如下:

codecentric’s Spring Boot Admin is a community project to manage and monitor your Spring Boot ® applications. The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ® (e.g. Eureka, Consul). The UI is just a Vue.js application on top of the Spring Boot Actuator endpoints.

codecentric的Spring Boot Admin是一个社区项目,管理和监控您的 Spring Boot ®应用。应用程序向我们的Spring Boot Admin Client注册(通过HTTP)或使用SpringCloud®(例如Eureka,Consul)发现。UI只是Spring Boot Actuator端点上的Vue.js应用程序。

简单来讲官方对Admin的定位是应用监控

创建Admin项目

创建admin项目依赖继承monitor-center项目,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.liuzhuoming23</groupId>
<artifactId>monitor-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>

<artifactId>admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin</name>
<description>Demo project for Spring Cloud</description>

<properties>
<spring-boot-admin.version>2.2.2</spring-boot-admin.version>
</properties>

<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

在启动类AdminApplication添加注解@EnableAdminServer

package com.github.liuzhuoming23.admin;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* 启动类
*
* @author x-047
*/
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {

public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}

}

在application.yml添加配置:

server:
port: 8060

spring:
application:
name: @pom.artifactId@
security:
user:
name: 'admin'
password: 'admin'

eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8000/eureka/

启动admin项目,并访问http://localhost:8060,显示页面如下:
01.jpg
可以很清楚地看到服务状态及基本信息,说明admin项目启动成功。
切换标签页到WallBoard,显示页面为:
02.jpg
其中绿色代表服务实例全部UP,黄色代表服务实例部分UP,灰色代表服务实例全部DOWN。
详情可以点击相应服务实例图标查看,包括详情信息和http请求追踪及统计。
http请求追踪需要项目开启actuator的httptrace endpoint(也许添加不同endpoint还会显示其他菜单,自行尝试),比如在gateway项目application.yml开启httptrace endpoint:

server:
port: 8030

spring:
application:
name: @pom.artifactId@
redis:
host: localhost
port: 6379
password:
timeout: 500
cloud:
gateway:
# discovery:
# locator:
# enabled: true
routes:
- id: feign
uri: lb://FEIGN
predicates:
- Path=/feign/**
filters:
- StripPrefix=1
- PrintUriAndQuery=true
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 1
redis-rate-limiter.burstCapacity: 3
key-resolver: "#{@hostnameKeyResolver}"

eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8000/eureka/

management:
endpoints:
web:
exposure:
include: httptrace,info,health

重启gateway项目,并多次请求http://localhost:8030/feign/port/🐕,然后打开admin中gateway实例详情:
03.jpg
可以看到http请求追踪信息已经统计成功了。
Admin的信息相对Eureka稍有延迟,刷新几次就同步过来了。