Spring Boot Admin (Spring Clould Admin) 官方文档重点内容中文翻译
GitHub地址
文章目录
- Spring Boot Admin简介
- 快速入门
- 2.1 配置Spring Boot Admin Server (SBA Server)
- 2.2 注册Client应用
- 2.1 Spring Boot Admin Client
- 2.2.2 Spring Cloud Discovery(注册发现)
- Client客户端应用
- 3.5 Spring Boot Admin Client 配置文件
- Spring Boot Admin Server
- 4.1 反向代理配置
- 4.2 其他配置选项
- 4.3 Spring Cloud Discovery注册发现
- 4.3.1 使用`SimpleDiscoveryClient`进行静态配置
- 4.3.2 其他DiscoveryClients
- 4.3.3 Converting ServiceInstances(略)
- 4.3.4 CloudFoundry(略)
- 4.4 集群
- 4.5 通知
- 4.5.1 邮件通知
- 4.5.2 PagerDuty通知(略)
- 4.5.3 OpsGenie通知(略)
- 4.5.4 Hipchat通知(略)
- 4.5.5 Slack通知(略)
- 4.5.6 Let’s Chat通知(略)
- 4.5.7 Microsoft Team通知(略)
- 4.5.8Telegram通知(略)
- 4.5.9 Discord通知(略)
- 4.5.10 通知代理设置(略)
- 4.5.11 后面暂时全略
- 常见问题
- 启动报错(Correct the classpath of your application so that it contains a single, compatible version of )
- 加入Context-path后,无法找到actuator(404)
1. Spring Boot Admin简介
该社区项目为SpringBoot应用提供了监控接口。
Spring Boot Admin 提供了以下特性:
- 显示健康状况
显示指标详情,例如:
- JVM 和 内存指标
- micrometer.io指标
- 数据源指标
- 缓存指标
- 显示 build-info 数量
- 跟踪和下载日志文件
- 展示jvm情况和环境配置
- 展示spring boot配置文件
- 支持spring cloud的 evn-endpoint 和 refresh-endpoint
- 方便的管理日志级别
- 和JMX-beans交互
- 查看thread dump
- 查看http-traces
- 查看auditevents
- 查看http-endpoints
- 查看定时任务(Scheduled tasks)
- 管理session(spring-session)
- 查看 Flyway / Liquibase 数据库迁移
- 下载 heapdump
- 当状态改变的时候可以通知(通过e-mail, Slack, Hipchat等)
- 记录状态变化的事件日志
2. 快速入门
2.1 配置Spring Boot Admin Server (SBA Server)
首先,初始化一个Spring Boot应用,SBA Server就放在这个应用中:
增添Spring Boot Admin Server starter 依赖:
de.codecentric
spring-boot-admin-starter-server
2.3.1
org.springframework.boot
spring-boot-starter-web 在
Application
上增添@EnableAdminServer
注解@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
2.2 注册Client应用
要把你的应用注册到SBA Server上,你可以在pom中增加SBA Client,也可以使用Spring Cloud注册中心。
2.1 Spring Boot Admin Client
想要把应用注册在SBA Server中,需要在pom文件中include Spring Boot Admin Client。为了保证应用的安全,还要增加 spring-boot-starter-security
.
增添
spring-boot-admin-starter-client
依赖
de.codecentric
spring-boot-admin-starter-client
2.3.1
org.springframework.boot
spring-boot-starter-security 在
application.properties
中进行配置:spring.boot.admin.client.url=http://localhost:8080 # SBA Server的URL
management.endpoints.web.exposure.include=* # 配置需要监控哪些指标使被监控端(acuator endpoints)可用:
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
方便起见,直接禁用security
2.2.2 Spring Cloud Discovery(注册发现)
如果你使用了Spring Cloud Discovery,就不需要 SBA Client 了。只需要增加 DiscoveryClient到 SBA Server中,剩下的就会自动配置
增加
spring-cloud-starter-eureka
依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client 增加
@EnableDiscoveryClient
开启发现@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
}
简单起见,这里禁用了security
告诉Eureka客户端去哪找到该服务注册
eureka:
instance:leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
startup: ${random.int} #needed to trigger info and endpoint update after restart
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL
//localhost:8761}/eureka/
management:
endpoints:web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
spring-boot-admin-sample-eureka样例
3. Client客户端应用
… 不重要,省略
3.5 Spring Boot Admin Client 配置文件
具体配置详情
4. Spring Boot Admin Server
4.1 反向代理配置
假设你的SBA Server运行在方向代理的后面,你需要配置 spring.boot.admin.ui.public-url
。同时,如果反向代理终止了http连接,你可能也需要配置server.forward-headers-strategy=native
4.2 其他配置选项
其他配置选项
4.3 Spring Cloud Discovery注册发现
SBA Server可以使用 Spring Clouds 的Discovery Client
去发现应用。优点是,你不需要在Client端包含spring-boot-admin-starter-client
依赖。你只需要在SBA Server端增加一个DiscoveryClient
的实现,剩下的都会通过自动配置(AutoConfiguration)自动完成
4.3.1 使用SimpleDiscoveryClient
进行静态配置
Spring Cloud 提供了SimpleDiscoveryClient
,它允许你通过静态配置指定Client应用:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
spring:
cloud:
discovery:
client:
simple:
instances:
test:
- uri: http://instance1.intern:8080
metadata:
management.context-path: /actuator
- uri: http://instance2.intern:8080
metadata:
management.context-path: /actuator
4.3.2 其他DiscoveryClients
除了Eureka之外,SBA也支持其他注册中心,例如Zookeeper,Consul等。
4.3.3 Converting ServiceInstances(略)
4.3.4 CloudFoundry(略)
4.4 集群
SBA Server 支持通过Hazelcast
进行复制(replication)。当项目中有 HazelcastConfig-
或 HazelcastInstance-
的 Bean
时,它就会自动开启。你也可以配置Hazelcast
实例来使其持久化,以便重启也能保持状态。
增加
Hazelcast
依赖:
com.hazelcast
hazelcast 实例化一个
HazelcastConfig
:@Bean
public Config hazelcastConfig() {
// 该map用来存储事件
// 这应该配置更可靠地持久化
// 如果事件太多,Spring Boot Admin 会对其压缩
MapConfig eventStoreMap = new MapConfig(DEFAULT_NAME_EVENT_STORE_MAP).setInMemoryFormat(InMemoryFormat.OBJECT).setBackupCount(1).setEvictionPolicy(EvictionPolicy.NONE)
.setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
// 该map用于对通知去重
// 如果这个map中的数据丢失了,没事,因为多个实例会推送多次消息
MapConfig sentNotificationsMap = new MapConfig(DEFAULT_NAME_SENT_NOTIFICATIONS_MAP).setInMemoryFormat(InMemoryFormat.OBJECT).setBackupCount(1).setEvictionPolicy(EvictionPolicy.LRU)
.setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
Config config = new Config();
config.addMapConfig(eventStoreMap);
config.addMapConfig(sentNotificationsMap);
config.setProperty(“hazelcast.jmx”, “true”);// WARNING: 这个配置是在本机搞一个集群,你需要根据实际需求进行改造.
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setMembers(singletonList(“127.0.0.1”));
return config;
}
Hazelcast
相关配置
属性名 | 描述 | 默认值 |
---|---|---|
spring.boot.admin.hazelcast.enabled | 开启Hazelcast | true |
spring.boot.admin.hazelcast.event-store | 存储事件的Hazelcast-map的名称 | “spring-boot-admin-event-store” |
spring.boot.admin.hazelcast.sent-notifications | 去重的Hazelcast-map的名称 | “spring-boot-admin-sent-notifications” |
4.5 通知
4.5.1 邮件通知
邮件使用Thymeleaf
模板渲染的HTML邮件。要开启邮件通知,需要使用spring-boot-starter-mail
配置JavaMailSender
并且设置接收人。
增加
spring-boot-starter-mail
依赖
org.springframework.boot
spring-boot-starter-mail 配置
JavaMailSender
spring.mail.host=smtp.example.com
spring.boot.admin.notify.mail.to=admin@example.com配置相关参数:
属性名 | 描述 | 默认值 |
---|---|---|
spring.boot.admin.notify.mail.enabled | 开启邮件通知 | true |
spring.boot.admin.notify.mail.ignore-changes | 忽略从一个状态到另一个状态的改变,多个以逗号分隔。格式为: “<from-status>:<to-status>” 支持通配符 | “UNKNOWN:UP” |
spring.boot.admin.notify.mail.template | Thymeleaf模板的路径 | “classpath:/META-INF/spring-boot-admin-server/mail/status-changed.html” |
spring.boot.admin.notify.mail.to | 邮件接收者列表,多个以逗号分隔 | “root@localhost” |
spring.boot.admin.notify.mail.cc | 邮件抄送人列表,多个以逗号分隔 | |
spring.boot.admin.notify.mail.from | 邮件发送人 | “Spring Boot Admin noreply@localhost” |
spring.boot.admin.notify.mail.additional-properties | 可以从模板访问的其他属性 |
4.5.2 PagerDuty通知(略)
4.5.3 OpsGenie通知(略)
4.5.4 Hipchat通知(略)
4.5.5 Slack通知(略)
4.5.6 Let’s Chat通知(略)
4.5.7 Microsoft Team通知(略)
4.5.8Telegram通知(略)
4.5.9 Discord通知(略)
4.5.10 通知代理设置(略)
4.5.11 后面暂时全略
常见问题
启动报错(Correct the classpath of your application so that it contains a single, compatible version of )
Description:
An attempt was made to call the method org.springframework.beans.factory.ObjectProvider.orderedStream()Ljava/util/stream/Stream; but it does not exist. Its class, org.springframework.beans.factory.ObjectProvider, is available from the following locations:
jar:file:/C:/Users/Administrator/.m2/repository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar!/org/springframework/beans/factory/ObjectProvider.class
It was loaded from the following location:
file:/C:/Users/Administrator/.m2/repository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.ObjectProvider
原因:spring-boot-admin版本不合适
解决办法:更换合适的spring-boot-admin版本
加入Context-path后,无法找到actuator(404)
原因:加入context-path后,全局路径发生改变了,所以需要额外配置
解决方法:增加如下配置
eureka:
instance:
metadata-map:
management:
context-path: ${server.servlet.context-path}/actuator
statusPageUrlPath: ${server.servlet.context-path}/actuator/info
home-page-url-path: ${server.servlet.context-path}
health-check-url-path: ${server.servlet.context-path}/actuator/health
还没有评论,来说两句吧...