概述
随着开发周期的推移,项目会不断变大,切分出的服务也会越来越多,这时一个个的微服务构成了错综复杂的系统。对于各个微服务系统的健康状态、会话数量、并发数、服务资源、延迟等度量信息的收集就成为了一个挑战。
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 |
|
主要是增加了spring-boot-admin-starter-server
依赖
1 | <dependency> |
Application
1 | package com.xzt.sleuth; |
bootstrap.yml
1 | spring: |
注意这里是使用指定url的方式取配置中心拉取配置文件。如果使用通过eureka获取配置中心地址并拉取配置文件的方式,我这里出现了一个问题:在
spring-boot-admin-server
之后注册到eureka
的服务无法被spring-boot-admin-server
获取到,原因未知。下面给出我的版本和配置的相关信息:spring-boot-admin-starter-server:
2.2.1spring-cloud:
Hoxton.SR1spring-boot:
2.2.2.RELEASE
1
2
3
4
5
6
7
8
9
10
11
12
13
14spring:
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 | management: |
management.endpoints.web.exposure.include=*
,Actuator默认只公开了/health
和/info
端点,要想暴露所有端点只需设置成星号即可注意:.yml结尾的配置文件,*需要加“”
sleuth-config-dev.yml
1 | spring: |
测试
依次启动,zipkin-server
,eureka
,config
,spring boot admin
,admin-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.
- 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>
- 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
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.
*/
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
}
- 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