700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring Boot Admin (Spring Clould Admin) 官方文档重点内容中文翻译

Spring Boot Admin (Spring Clould Admin) 官方文档重点内容中文翻译

时间:2021-01-12 01:01:45

相关推荐

Spring Boot Admin (Spring Clould Admin) 官方文档重点内容中文翻译

GitHub地址

文章目录

1. Spring Boot Admin简介2. 快速入门2.1 配置Spring Boot Admin Server (SBA Server)2.2 注册Client应用2.1 Spring Boot Admin Client2.2.2 Spring Cloud Discovery(注册发现)3. Client客户端应用3.5 Spring Boot Admin Client 配置文件4. Spring Boot Admin Server4.1 反向代理配置4.2 其他配置选项4.3 Spring Cloud Discovery注册发现4.3.1 使用`SimpleDiscoveryClient`进行静态配置4.3.2 其他DiscoveryClients4.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 依赖:

<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.3.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

Application上增添@EnableAdminServer注解

@Configuration@EnableAutoConfiguration@EnableAdminServerpublic 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依赖

<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.3.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>

application.properties中进行配置:

spring.boot.admin.client.url=http://localhost:8080 # SBA Server的URLmanagement.endpoints.web.exposure.include=* # 配置需要监控哪些指标

使被监控端(acuator endpoints)可用:

@Configurationpublic static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {@Overrideprotected 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依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

增加@EnableDiscoveryClient开启发现

@Configuration@EnableAutoConfiguration@EnableDiscoveryClient@EnableAdminServerpublic class SpringBootAdminApplication {public static void main(String[] args) {SpringApplication.run(SpringBootAdminApplication.class, args);}@Configurationpublic static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().permitAll() .and().csrf().disable();}}}

简单起见,这里禁用了security

告诉Eureka客户端去哪找到该服务注册

eureka: instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/healthmetadata-map:startup: ${random.int} #needed to trigger info and endpoint update after restartclient:registryFetchIntervalSeconds: 5serviceUrl:defaultZone: ${EUREKA_SERVICE_URL:http://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:8080metadata:management.context-path: /actuator- uri: http://instance2.intern:8080metadata: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依赖:

<dependency><groupId>com.hazelcast</groupId><artifactId>hazelcast</artifactId></dependency>

实例化一个HazelcastConfig:

@Beanpublic 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相关配置

4.5 通知

4.5.1 邮件通知

邮件使用Thymeleaf模板渲染的HTML邮件。要开启邮件通知,需要使用spring-boot-starter-mail配置JavaMailSender并且设置接收人。

增加spring-boot-starter-mail依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>

配置JavaMailSender

spring.mail.host=spring.boot.admin.notify.mail.to=admin@

配置相关参数:

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.classIt 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.jarAction: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}/actuatorstatusPageUrlPath: ${server.servlet.context-path}/actuator/infohome-page-url-path: ${server.servlet.context-path}health-check-url-path: ${server.servlet.context-path}/actuator/health

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。