抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

概述

随着开发周期的推移,项目会不断变大,切分出的服务也会越来越多,这时一个个的微服务构成了错综复杂的系统。对于各个微服务系统的健康状态、会话数量、并发数、服务资源、延迟等度量信息的收集就成为了一个挑战。

Spring Boot Actuator

Actuator是Spring Boot的模块,它在应用中添加了REST/JMS端点,方便监控和管理应用。端点提供了健康检查、指标监控、访问日志、线程转储、堆转储和环境信息等等。

Spring Boot Admin

Actuator功能强大,便于其他应用使用端点(只需要简单的REST调用)。但是开发人员使用时就没那么方便了。对于开发人员,有良好的交互界面会更方便浏览监控数据和管理应用。这正是Spring Boot Admin做的工作。它为actuator端点提供了良好的交互界面,并提供了额外的特性。

Spring Boot Admin 有两个角色组成,一个是 Spring Boot Admin Server,一个是 Spring Boot Admin Client。

创建Spring Boot Admin服务端

完整pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xzt</groupId>
<artifactId>cloud-sleuth-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-sleuth-service</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.2.1</spring-boot-admin.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>

主要是增加了spring-boot-admin-starter-server依赖

1
2
3
4
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

Application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.xzt.sleuth;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer
public class CloudSleuthServiceApplication {

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

}

bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
spring:
cloud:
config:
name: public-config,sleuth-config
label: master
profile: dev
uri: http://localhost:8888

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
  • 注意这里是使用指定url的方式取配置中心拉取配置文件。如果使用通过eureka获取配置中心地址并拉取配置文件的方式,我这里出现了一个问题:在spring-boot-admin-server之后注册到eureka的服务无法被spring-boot-admin-server获取到,原因未知。下面给出我的版本和配置的相关信息:

    • spring-boot-admin-starter-server:2.2.1
    • spring-cloud:Hoxton.SR1
    • spring-boot:2.2.2.RELEASE
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    spring:
    cloud:
    config:
    name: public-config,sleuth-config
    label: master
    profile: dev
    discovery:
    enabled: true
    service-id: cloud-config-service

    eureka:
    client:
    serviceUrl:
    defaultZone: http://localhost:8761/eureka/

git仓库配置文件

public-config-dev.yml&配置中心和注册中心的application.yml

public-config-dev.yml:所有的项目都要读取的公共配置文件

1
2
3
4
5
6
7
8
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
  • management.endpoints.web.exposure.include=*,Actuator默认只公开了/health/info端点,要想暴露所有端点只需设置成星号即可

  • 注意:.yml结尾的配置文件,*需要加“”

sleuth-config-dev.yml

1
2
3
4
5
6
spring:
application:
name: cloud-sleuth-service

server:
port: 8084

测试

依次启动,zipkin-servereurekaconfigspring boot adminadmin-service,访问:http://localhost:8084

总结

尽管Spring Boot Admin不是Spring团队提供的模块,但是就其使用频率和使用的舒适度以及兼容性等方面考虑,它可以称的上是管理和监控Spring Boot的最好的开源项目。

在与Spring cloud 结合的方面,Spring Boot Admin Service将通过注册中心拉取应用信息,无需SBA客户端,下面是引用官网的一段:

If you already use Spring Cloud Discovery for your applications you don’t need the SBA Client. Just add a DiscoveryClient to Spring Boot Admin Server, the rest is done by our AutoConfiguration.

The following steps uses Eureka, but other Spring Cloud Discovery implementations are supported as well. There are examples using Consul and Zookeeper.

Also have a look at the Spring Cloud documentation.

  1. Add spring-cloud-starter-eureka to you dependencies:

pom.xml

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. Enable discovery by adding @EnableDiscoveryClient to your configuration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}

/**
* For the sake of brevity we’re disabling the security for now. Have a look at the security section on how to deal with secured endpoints.
*/
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
}
  1. Tell the Eureka client where to find the service registry:

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#Configuration section for the Eureka client
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

#As with Spring Boot 2 most of the endpoints aren’t exposed via http by default, we expose all of them. For production you should carefully choose which endpoints to expose.
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS

评论