SBA服务端
1. 通过代理访问运行的服务端
如果Spring Boot Admin服务端是通过反向代理访问的,那么可能需要通过 (spring.boot.admin.ui.public-url
) 配置一个公开的url来访问服务端。
此外,当反向代理阻断了https链接时,可能需要配置 server.forward-headers-strategy=native
(同时请参考 Spring Boot 参考指南)。
2. 配置项
属性名 | 说明 | 默认值 |
---|---|---|
spring.boot.admin.server.enabled |
开启Spring Boot Admin 服务端。 |
|
spring.boot.admin.context-path |
访问Admin服务端静态资源以及API的上下文前缀。这是相对于Dispatcher-Servlet来说的。 |
|
spring.boot.admin.monitor.status-interval |
检查实例状态的时间间隔。 |
10,000ms |
spring.boot.admin.monitor.status-lifetime |
状态的生命周期。只要最后一次的状态没有过期就不会更新。 |
10,000ms |
spring.boot.admin.monitor.info-interval |
检查实例信息的时间间隔。 |
1m |
spring.boot.admin.monitor.info-lifetime |
信息的生命周期。只要最后一次的信息没有过期就不会更新。 |
1m |
spring.boot.admin.monitor.default-timeout |
请求默认的超时时间。可以使用 |
10,000 |
spring.boot.admin.monitor.timeout.* |
针对某个接口id超时时间的键值对配置方式。默认情况下就是 default-timeout。 |
|
spring.boot.admin.monitor.default-retries |
请求失败的默认重试次数。修改请求 ( |
0 |
spring.boot.admin.monitor.retries.* |
针对某个接口id重试次数的键值对配置方式。默认情况下就是 default-retries。修改请求 ( |
|
spring.boot.admin.metadata-keys-to-sanitize |
根据正则表达式匹配元数据的key,匹配到的结果在所有的json输出中会被清除。 |
|
spring.boot.admin.probed-endpoints |
针对Spring Boot 1.x版本的客户端应用,SBA使用该选项来配置特定接口。 如果路径和id不同,可以设置为 id:path(比如 health:ping).. |
|
spring.boot.admin.instance-auth.enabled |
开启从spring properties配置文件中读取授权信息。 |
|
spring.boot.admin.instance-auth.default-user-name |
服务注册所使用的默认用户名。前提是 |
|
spring.boot.admin.instance-auth.default-password |
服务注册所使用的默认密码。前提是 |
|
spring.boot.admin.instance-auth.service-map.*.user-name |
某个指定名称的服务注册所使用的用户名。前提是 |
|
spring.boot.admin.instance-auth.service-map.*.user-password |
某个指定名称的服务注册所使用的密码。前提是 |
|
spring.boot.admin.instance-proxy.ignored-headers |
向客户端转发请求时忽略哪些header。 |
|
spring.boot.admin.ui.public-url |
构建页面的时候所使用的base href。 |
如果是通过反向代理进行请求(用到了path重写),那么可以使用该选项来配置正确的引用地址。如果 host/port 被省略了,那么会从请求中推断出来。 |
spring.boot.admin.ui.brand |
导航栏所使用的品牌标识。 |
|
spring.boot.admin.ui.title |
页面中想要显示的标题。 |
|
spring.boot.admin.ui.login-icon |
登录页面所显示的图标。 |
|
spring.boot.admin.ui.favicon |
配置默认的favicon,用作图标和桌面通知图标。 |
|
spring.boot.admin.ui.favicon-danger |
配置默认的favicon,用作图标和桌面通知图标,在服务挂掉的时候显示。 |
|
spring.boot.admin.ui.remember-me-enabled |
切换在登录页面是否显示记住我复选框。 |
|
spring.boot.admin.ui.poll-timer.cache |
获取最新缓存数据的间隔(单位毫秒ms)。 |
|
spring.boot.admin.ui.poll-timer.datasource |
获取最新数据源数据的间隔(单位毫秒ms)。 |
|
spring.boot.admin.ui.poll-timer.gc |
获取最新gc数据的间隔(单位毫秒ms)。 |
|
spring.boot.admin.ui.poll-timer.process |
获取最新cpu数据的间隔(单位毫秒ms)。 |
|
spring.boot.admin.ui.poll-timer.memory |
获取最新内存数据的间隔(单位毫秒ms)。 |
|
spring.boot.admin.ui.poll-timer.threads |
获取最新线程数据的间隔(单位毫秒ms)。 |
|
3. Spring Cloud 服务发现
Spring Boot Admin服务端可以使用Spring Clouds 的 DiscoveryClient
来发现服务。
其优点是客户端不必依赖 spring-boot-admin-starter-client
了。
你只需在admin服务端添加一个 DiscoveryClient
的实现 - 其它一切都会自动配置完成。
3.1. 使用SimpleDiscoveryClient进行静态配置
Spring Cloud提供了一个 SimpleDiscoveryClient
。它允许我们通过静态配置的方式指定客户端应用:
<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
3.2. 其它方式的服务发现客户端
Spring Boot Admin同时也支持其它Spring Cloud DiscoveryClient
的实现(比如Eureka, Zookeeper, Consul, …)。
只需要将他们添加到Spring Boot Admin服务端并正确配置即可。
这里有一份 使用Eureka作为实现 的示例。
3.3. 转换服务实例
服务的注册信息中是通过 ServiceInstanceConverter
来进行转换的。Spring Boot Admin提供了一个默认转换方式,还有一个Eureka转换方式的实现。
这些都是自动配置并选择的。
你也可以通过SBA服务端配置选项,来修改应用注册时使用哪些信息以及哪些实例元数据。元数据的值会优先于服务端的配置。 如果这么多选项仍然满足不了您的需求,那么您也可以自定义一个`ServiceInstanceConverter`。 |
在使用Eureka的时候,healthCheckUrl`选项会被Eureka用在健康检查上,
它也可以通过 `eureka.instance.healthCheckUrl 属性配置在您的客户端上。
|
Key | Value | 默认值 |
---|---|---|
user.name |
访问接口所使用的凭证。 |
|
management.scheme |
服务URL中使用的scheme,用于访问actuator接口。 |
|
management.address |
服务URL中使用的address,用于访问actuator接口。 |
|
management.port |
服务URL中使用的port,用于访问actuator接口。 |
|
management.context-path |
服务URL后添加的上下文路径,用于访问actuator接口。 |
|
health.path |
服务URL后添加的路径,用于健康检查。如果配置了 |
|
属性名 | 说明 | 默认值 |
---|---|---|
spring.boot.admin.discovery.enabled |
开启admin服务端的DiscoveryClient支持。 |
|
spring.boot.admin.discovery.converter.management-context-path |
当management-url被 |
|
spring.boot.admin.discovery.converter.health-endpoint-path |
当health-url被 |
|
spring.boot.admin.discovery.ignored-services |
在发现服务的时候,这些服务会被忽略,不会作为服务注册到服务端。支持一些简单的表达式(比如 "foo*", "*bar", "foo*bar*")。 |
|
spring.boot.admin.discovery.services |
在发现服务的时候,这些服务会被包含进来,作为服务注册到服务端。支持一些简单的表达式(比如 "foo*", "*bar", "foo*bar*")。 |
|
spring.boot.admin.discovery.ignored-instances-metadata |
如果服务实例的元数据被列表匹配到至少一项,那么这个服务就会被忽略。 (例如 "discoverable=false") |
|
spring.boot.admin.discovery.instances-metadata |
如果服务实例的元数据被列表匹配到至少一项,那么这个服务就会被引入进来。 (例如 "discoverable=false") |
3.4. CloudFoundry
如果您想将应用部署到CloudFoundry上,那么就 必须 配置 vcap.application.application_id
以及 vcap.application.instance_index
这两个选项,它们会被添加到元数据中,这样才能正确注册到Spring Boot Admin服务端。
下面是一个针对Eureka的简单配置:
eureka:
instance:
hostname: ${vcap.application.uris[0]}
nonSecurePort: 80
metadata-map:
applicationId: ${vcap.application.application_id}
instanceId: ${vcap.application.instance_index}
4. 集群
Spring Boot Admin服务端支持通过Hazelcast的方式建立集群。当提供 HazelcastConfig
或 HazelcastInstance
Bean的时候会自动开启。 你也可以持久化Hazelcast的配置,这样在服务重启之后仍然能保持之前的状态。 同时推荐参考 Spring Boot对Hazelcast的支持。
-
将Hazelcast添加到依赖中:
pom.xml<dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast</artifactId> </dependency>
-
初始化HazelcastConfig:
@Bean public Config hazelcastConfig() { // This map is used to store the events. // It should be configured to reliably hold all the data, // Spring Boot Admin will compact the events, if there are too many MapConfig eventStoreMap = new MapConfig(DEFAULT_NAME_EVENT_STORE_MAP).setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1) .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMergePolicy.class.getName(), 100)); // This map is used to deduplicate the notifications. // If data in this map gets lost it should not be a big issue as it will atmost // lead to // the same notification to be sent by multiple instances MapConfig sentNotificationsMap = new MapConfig(DEFAULT_NAME_SENT_NOTIFICATIONS_MAP) .setInMemoryFormat(InMemoryFormat.OBJECT).setBackupCount(1) .setEvictionConfig(new EvictionConfig().setEvictionPolicy(EvictionPolicy.LRU) .setMaxSizePolicy(MaxSizePolicy.PER_NODE)) .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMergePolicy.class.getName(), 100)); Config config = new Config(); config.addMapConfig(eventStoreMap); config.addMapConfig(sentNotificationsMap); config.setProperty("hazelcast.jmx", "true"); // WARNING: This setups a local cluster, you change it to fit your needs. config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig(); tcpIpConfig.setEnabled(true); tcpIpConfig.setMembers(singletonList("127.0.0.1")); return config; }
属性名 | 说明 | 默认值 |
---|---|---|
spring.boot.admin.hazelcast.enabled |
开启对Hazelcast的支持 |
|
spring.boot.admin.hazelcast.event-store |
对Hazelcast-map事件的存储名称 |
|
spring.boot.admin.hazelcast.sent-notifications |
用于清除Hazelcast-map对应名称的重复通知 |
|
5. 消息通知
5.1. 邮件通知
邮件通知是以HTML格式渲染的电子邮件,它使用 Thymeleaf 作为模板。 要想开启邮件通知,需要使用 spring-boot-starter-mail
配置 JavaMailSender
并设置一个收件人。

为了防止敏感信息被泄露,默认邮件模板中不显示任何实例的元数据。如果你想显示的话,可以自定义模板。 |
-
将spring-boot-starter-mail添加到依赖中:
pom.xml<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
-
配置一个JavaMailSender
application.propertiesspring.mail.host=smtp.example.com spring.boot.admin.notify.mail.to=admin@example.com
-
根据下面的可选项配置邮件
表格 4. 邮件提醒配置的可选项 属性名 说明 面若防治 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
可以从模板读取的其它属性
5.2. PagerDuty通知
To enable PagerDuty notifications you just have to add a generic service to your PagerDuty-account and set spring.boot.admin.notify.pagerduty.service-key
to the service-key you received.
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.pagerduty.enabled |
Enable mail notifications |
|
spring.boot.admin.notify.pagerduty.ignore-changes |
Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed. |
|
spring.boot.admin.notify.pagerduty.service-key |
Service-key to use for PagerDuty |
|
spring.boot.admin.notify.pagerduty.url |
The Pagerduty-rest-api url |
|
spring.boot.admin.notify.pagerduty.description |
Description to use in the event. SpEL-expressions are supported |
|
spring.boot.admin.notify.pagerduty.client |
Client-name to use in the event |
|
spring.boot.admin.notify.pagerduty.client-url |
Client-url to use in the event |
5.3. OpsGenie通知
To enable OpsGenie notifications you just have to add a new JSON Rest API integration to your OpsGenie account and set spring.boot.admin.notify.opsgenie.api-key
to the apiKey you received.
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.opsgenie.enabled |
Enable OpsGenie notifications |
|
spring.boot.admin.notify.opsgenie.ignore-changes |
Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed. |
|
spring.boot.admin.notify.opsgenie.api-key |
apiKey you received when creating the integration |
|
spring.boot.admin.notify.opsgenie.url |
OpsGenie Alert API url |
|
spring.boot.admin.notify.opsgenie.description |
Description to use in the event. SpEL-expressions are supported |
|
spring.boot.admin.notify.opsgenie.actions |
Comma separated list of actions that can be executed. |
|
spring.boot.admin.notify.opsgenie.source |
Field to specify source of alert. By default, it will be assigned to IP address of incoming request. |
|
spring.boot.admin.notify.opsgenie.tags |
Comma separated list of labels attached to the alert. |
|
spring.boot.admin.notify.opsgenie.entity |
The entity the alert is related to. |
|
spring.boot.admin.notify.opsgenie.user |
Default owner of the execution. If user is not specified, the system becomes owner of the execution. |
5.4. Hipchat通知
To enable Hipchat notifications you need to create an API token on your Hipchat account and set the appropriate configuration properties.
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.hipchat.enabled |
Enable Hipchat notifications |
|
spring.boot.admin.notify.hipchat.ignore-changes |
Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed. |
|
spring.boot.admin.notify.hipchat.url |
The HipChat REST API (V2) URL |
|
spring.boot.admin.notify.hipchat.auth-token |
The API token with access to the notification room |
|
spring.boot.admin.notify.hipchat.room-id |
The ID or url-encoded name of the room to send notifications to |
|
spring.boot.admin.notify.hipchat.notify |
Whether the message should trigger a user notification |
|
spring.boot.admin.notify.hipchat.description |
Description to use in the event. SpEL-expressions are supported |
|
5.5. Slack通知
To enable Slack notifications you need to add a incoming Webhook under custom integrations on your Slack account and configure it appropriately.
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.slack.enabled |
Enable Slack notifications |
|
spring.boot.admin.notify.slack.ignore-changes |
Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed. |
|
spring.boot.admin.notify.slack.webhook-url |
The Slack Webhook URL to send notifications to. |
|
spring.boot.admin.notify.slack.channel |
Optional channel name (without # at the beginning). If different than channel in Slack Webhooks settings |
|
spring.boot.admin.notify.slack.icon |
Optional icon name (without surrounding colons). If different than icon in Slack Webhooks settings |
|
spring.boot.admin.notify.slack.username |
Optional username to send notification if different than in Slack Webhooks settings |
|
spring.boot.admin.notify.slack.message |
Message to use in the event. SpEL-expressions and Slack markups are supported |
|
5.6. Let’s Chat通知
To enable Let’s Chat notifications you need to add the host url and add the API token and username from Let’s Chat
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.letschat.enabled |
Enable let´s Chat notifications |
|
spring.boot.admin.notify.letschat.ignore-changes |
Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed. |
|
spring.boot.admin.notify.letschat.url |
The let´s Chat Host URL to send notifications |
|
spring.boot.admin.notify.letschat.room |
the room where to send the messages |
|
spring.boot.admin.notify.letschat.token |
the token to access the let´s Chat API |
|
spring.boot.admin.notify.letschat.username |
The username for which the token was created |
|
spring.boot.admin.notify.letschat.message |
Message to use in the event. SpEL-expressions are supported |
|
5.7. Microsoft Teams通知
To enable Microsoft Teams notifications you need to setup a connector webhook url and set the appropriate configuration property.
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.ms-teams.enabled |
Enable Microsoft Teams notifications |
|
spring.boot.admin.notify.ms-teams.webhook-url |
The Microsoft Teams webhook url to send the notifications to. |
|
spring.boot.admin.notify.ms-teams.deRegisteredTitle |
Title of the Teams message when an app de-registers. |
De-Registered |
spring.boot.admin.notify.ms-teams.registeredTitle |
Title of the Teams message when an app dregisters. |
Registered |
spring.boot.admin.notify.ms-teams.statusChangedTitle |
Title of the Teams message when an app changes status. |
Status Changed |
spring.boot.admin.notify.ms-teams.messageSummary |
Summary section of every Teams message originating from Spring Boot Admin. |
Spring Boot Admin Notification |
spring.boot.admin.notify.ms-teams.theme_color |
Set the theme color. SpEL-expressions are supported. |
|
spring.boot.admin.notify.ms-teams.deregister_activity_subtitle |
Subtitle of the Activity section of the Teams message when an app de-registers. SpEL-expressions are supported. |
|
spring.boot.admin.notify.ms-teams.register_activity_subtitle |
Subtitle of the Activity section of the Teams message when an app registers. SpEL-expressions are supported. |
|
spring.boot.admin.notify.ms-teams.status_activity_subtitle |
Subtitle of the Activity section of the Teams message when an app changes status. SpEL-expressions are supported. |
|
5.8. Telegram通知
To enable Telegram notifications you need to create and authorize a telegram bot and set the appropriate configuration properties for auth-token and chat-id.
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.telegram.enabled |
Enable Telegram notifications |
|
spring.boot.admin.notify.telegram.auth-token |
The token identifying und authorizing your Telegram bot (e.g. |
|
spring.boot.admin.notify.telegram.chat-id |
Unique identifier for the target chat or username of the target channel |
|
spring.boot.admin.notify.telegram.disable-notify |
If true users will receive a notification with no sound. |
|
spring.boot.admin.notify.telegram.parse_mode |
The parsing mode for the sent message. Currently |
|
spring.boot.admin.notify.telegram.message |
Text to send. SpEL-expressions are supported. |
|
5.9. Discord通知
To enable Discord notifications you need to create a webhook and set the appropriate configuration property.
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.discord.enabled |
Enable Discord notifications |
|
spring.boot.admin.notify.discord.webhook-url |
The Discord webhook url to send the notifications to. |
|
spring.boot.admin.notify.discord.username |
Optional username. |
Default set in Discord |
spring.boot.admin.notify.discord.avatar-url |
Optional URL to avatar. |
Default set in Discord |
spring.boot.admin.notify.discord.tts |
If the message is a text to speech message. |
|
spring.boot.admin.notify.discord.message |
Text to send. SpEL-expressions are supported. |
|
5.10. 为消息通知配置代理
所有的消息通知都是使用 RestTemplate
来进行发送的,所以我们可以为其配置一个代理。
属性名 | 说明 | 默认值 |
---|---|---|
spring.boot.admin.notify.proxy.host |
代理的 host |
|
spring.boot.admin.notify.proxy.port |
代理的 port |
|
spring.boot.admin.notify.proxy.username |
代理的 username (如果代理需要身份认证的话) |
|
spring.boot.admin.notify.proxy.password |
代理的 password (如果代理需要身份认证的话) |
5.11. 通知发送程序
RemindingNotifier
会在应用停机/离线的时候发送消息通知,它会将通知的发送委托给另外一个通知发送程序。
默认情况下,这个通知发送程序会在一个已注册发生 停机
或 离线
的时候触发。
你也可以通过 setReminderStatuses()
来修改这个行为。
当这个状态变成了无需触发,或者是相关服务注销了的话,这个通知发送程序就会结束。
默认情况下,通知发送程序每隔10分钟发送一次,可以使用 setReminderPeriod()
对这个间隔进行修改。
RemindingNotifier
本身不会开启后台线程来调用通知发送程序,你需要按照下面的示例来处理这个问题;
@Configuration
public class NotifierConfiguration {
@Autowired
private Notifier notifier;
@Primary
@Bean(initMethod = "start", destroyMethod = "stop")
public RemindingNotifier remindingNotifier() {
RemindingNotifier notifier = new RemindingNotifier(notifier, repository);
notifier.setReminderPeriod(Duration.ofMinutes(10)); (1)
notifier.setCheckReminderInverval(Duration.ofSeconds(10)); (2)
return notifier;
}
}
1 | 通知发送程序会每隔10分钟发送一次。 |
2 | 每隔10秒种安排一次通知发送。 |
5.12. 过滤通知
FilteringNotifier
允许在运行时添加/删除消息通知过滤的规则。
它会将通知的发送委托给另一个发送程序。
如果向 ApplicationContext
添加了 FilteringNotifier
,那么RESTful页面中的 消息通知/过滤器
就可以访问了。
尤其时在部署应用的时候,如果你不想接受提醒,此时过滤器就能帮到你。
在停止应用之前你可以通过 POST
请求发送一个(带有过期时间的)过滤条件。
@Configuration(proxyBeanMethods = false)
public class NotifierConfig {
private final InstanceRepository repository;
private final ObjectProvider<List<Notifier>> otherNotifiers;
public NotifierConfig(InstanceRepository repository, ObjectProvider<List<Notifier>> otherNotifiers) {
this.repository = repository;
this.otherNotifiers = otherNotifiers;
}
@Bean
public FilteringNotifier filteringNotifier() { (1)
CompositeNotifier delegate = new CompositeNotifier(this.otherNotifiers.getIfAvailable(Collections::emptyList));
return new FilteringNotifier(delegate, this.repository);
}
@Primary
@Bean(initMethod = "start", destroyMethod = "stop")
public RemindingNotifier remindingNotifier() { (2)
RemindingNotifier notifier = new RemindingNotifier(filteringNotifier(), this.repository);
notifier.setReminderPeriod(Duration.ofMinutes(10));
notifier.setCheckReminderInverval(Duration.ofSeconds(10));
return notifier;
}
}
1 | 使用delegate添加一个 FilteringNotifier Bean (例如在配置 MailNotifier 时) |
2 | 使用 FilteringNotifier 作为delegate,将 RemindingNotifier 添加为primary bean。 |
这个示例结合了消息提醒和通知过滤。 这可以让你在部署应用后的一段时间内(直到过滤条件过期)不会收到消息通知。 |
5.13. 钉钉通知
要想启用 DingTalk 消息通知,你需要创建并配置钉钉机器人的授权,并为 webhookUrl 和 secret 提供正确的配置。
属性名 | 说明 | 默认值 |
---|---|---|
spring.boot.admin.notify.dingtalk.enabled |
开启钉钉消息通知。 |
|
spring.boot.admin.notify.dingtalk.webhook-url |
钉钉通知将要发送的webhook url。 |
|
spring.boot.admin.notify.dingtalk.secret |
消息认证的secret。 |
|
spring.boot.admin.notify.dingtalk.message |
发送的文本,支持SpEL表达式。 |
|