Spring Boot Admin是一个Github上的一个开源项目,它在Spring Boot Actuator的基础上提供简洁的可视化WEB UI,是用来管理 Spring Boot 应用程序的一个简单的界面,提供如下功能:

显示 name/id 和版本号
显示在线状态
Logging日志级别管理
JMX beans管理
Threads会话和线程管理
Trace应用请求跟踪
应用运行参数信息,如:
Java 系统属性
Java 环境变量属性
内存信息
Spring 环境属性
Spring Boot Admin 包含服务端和客户端,按照以下配置可让Spring Boot Admin运行起来。

二、入门
1. 创建 Spring Boot Admin Server
pom.xml

官网应用如下

https://codecentric.github.io/spring-boot-admin/current/#_customizing_login_logo

整个pom文件如下

<?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 http://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.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-server</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<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>

application.yml

server:
port: 9999
spring:
application:
name: service-admin
logging:
file: "/logs/boot-admin-sample.log" # 日志输出路径

启动类 AdminServerApplication

启动类加上@EnableAdminServer注解,开启AdminServer的功能:

package com.tuling.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration; import de.codecentric.boot.admin.server.config.EnableAdminServer; @Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminServer { public static void main(String[] args) {
SpringApplication.run(SpringBootAdminServer.class, args);
}
}

2. 创建 Spring Boot Admin Client

pom.xml

这里没有引入acturor jar包,因为admin client默认就帮我们引入了

整个pom文件如下

<?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 http://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.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-client</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<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>

application.yml
spring.boot.admin.client.url:要注册的Spring Boot Admin Server的URL。
management.endpoints.web.exposure.include:与Spring Boot 2一样,默认情况下,大多数actuator的端口都不会通过http公开,* 代表公开所有这些端点。对于生产环境,应该仔细选择要公开的端点。

spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:9999
server:
port: 8768 management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS

启动类 AdminClientApplication

package com.tuling.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 使用Eureka做服务发现.
*/
@SpringBootApplication
public class SpringBootAdminClient {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminClient.class, args);
}
}

启动两个工程,在浏览器上输入localhost:9999 ,浏览器显示的界面如下:

特别需要注意的是:在spring boot admin的页面上要显示日志,需要做下面的配置手动指定logfile的位置

三、spring boot admin server集成 Eureka

1. 创建 sc-eureka-server

这是一个 eureka-server 注册中心。

整个pom文件如下


<?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 http://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.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>sc-admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sc-admin-server</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</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>

 

application.yml  spring boot admin server也是注册中心的客户端,需要向注册中心进行注册

server:
port: 9999
spring:
application:
name: service-admin
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
logging:
file: "/logs/boot-admin-sample.log" # 日志输出路径

启动类 ScAdminServerApplication

package com.tuling.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration; import de.codecentric.boot.admin.server.config.EnableAdminServer; @Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableDiscoveryClient
public class SpringBootAdminServerWithEureka { public static void main(String[] args) {
SpringApplication.run(SpringBootAdminServerWithEureka.class, args);
}
}

启动类中一定要添加上@EnableDiscoveryClient注解

接下来我们修改spring boot admin 的客户端

这是一个 Spring Boot Admin client 端。

pom.xml

<?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 http://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.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>sc-admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sc-admin-client</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>

application.yml

spring:
application:
name: admin-client
server:
port: 8768 eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS

启动类

package com.tuling.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* 使用Eureka做服务发现.
*/
@SpringBootApplication
@EnableDiscoveryClient
public class SpringBootAdminClientWithEureka {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminClientWithEureka.class, args);
}
}
admin 会自己拉取 Eureka 上注册的 app 信息,主动去注册。这也是唯一区别之前入门中手动注册的地方,就是 client 端不需要 admin-client 的依赖,也不需要配置 admin 地址了,一切全部由 admin-server 自己实现。这样的设计对环境变化很友好,不用改了admin-server后去改所有app 的配置了。

四、集成 Spring Security
Web应用程序中的身份验证和授权有多种方法,因此Spring Boot Admin不提供默认方法。默认情况下,spring-boot-admin-server-ui提供登录页面和注销按钮。我们结合 Spring Security 实现需要用户名和密码登录的安全认证。

sc-admin-server工程的pom文件需要增加以下的依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
整个配置文件变成下面的形式
<?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 http://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.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>sc-admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sc-admin-server</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</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>
在 sc-admin-server工的配置文件 application.yml 中配置 spring security 的用户名和密码,这时需要在服务注册时带上 metadata-map 的信息,如下:
spring:
security:
user:
name: "admin"
password: "admin" eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
 application配置文件如下
server:
port: 9999
spring:
application:
name: service-admin
security:
user:
name: "admin"
password: "admin"
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
logging:
file: "/logs/boot-admin-sample.log" # 日志输出路径


@EnableWebSecurity注解以及WebSecurityConfigurerAdapter一起配合提供基于web的security。继承了WebSecurityConfigurerAdapter之后,再加上几行代码,我们就能实现要求用户在进入应用的任何URL之前都进行验证的功能,写一个配置类SecuritySecureConfig继承WebSecurityConfigurerAdapter,配置如下:

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
} @Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/"); http.authorizeRequests()
//授予对所有静态资产和登录页面的公共访问权限。
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
//必须对每个其他请求进行身份验证
.anyRequest().authenticated()
.and()
//配置登录和注销
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
//启用HTTP-Basic支持。这是Spring Boot Admin Client注册所必需的
.httpBasic().and();
// @formatter:on
}
}

整个配置类如下

package com.tuling.cloud.study;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; import de.codecentric.boot.admin.server.config.AdminServerProperties; @Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
} @Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/"); http.authorizeRequests()
//授予对所有静态资产和登录页面的公共访问权限。
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
//必须对每个其他请求进行身份验证
.anyRequest().authenticated()
.and()
//配置登录和注销
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
//启用HTTP-Basic支持。这是Spring Boot Admin Client注册所必需的
.httpBasic().and();
// @formatter:on
}
}

重新访问 http://localhost:9999/ 会出现登录界面,密码是 配置文件中配置好的,账号 admin 密码 admin,界面如下:

五、通知

1. 邮件通知

在 Spring Boot Admin 中 当注册的应用程序状态更改为DOWN、UNKNOWN、OFFLINE 都可以指定触发通知,下面讲解配置邮件通知。

在sc-admin-server工程pom文件,加上mail的依赖,如下:

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

在配置文件application.yml文件中,配置收发邮件的配置:

spring:
mail:
host: smtp.163.com
username: xxxx@163.com
password: xxxx
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
boot:
admin:
notify:
mail:
from: xxxx@163.com
to: xxxx@qq.com

配置后,重启sc-admin-server工程,之后若出现注册的客户端的状态从 UP 变为 OFFLINE 或其他状态,服务端就会自动将电子邮件发送到上面配置的收件地址。

注意 : 配置了邮件通知后,会出现 反复通知 service offline / up。这个问题的原因在于 查询应用程序的状态和信息超时,下面给出两种解决方案:

#方法一:增加超时时间(单位:ms)

spring.boot.admin.monitor.read-timeout=20000

#方法二:关闭闭未使用或不重要的检查点

management.health.db.enabled=false
management.health.mail.enabled=false
management.health.redis.enabled=false
management.health.mongo.enabled=false

Spring Boot Admin Server 配置属性详解

https://codecentric.github.io/spring-boot-admin/current/#spring-cloud-discovery-support

在admin boot Server中加上下面的配置

server:
port: 9999
spring:
application:
name: service-admin
security:
user:
name: "admin"
password: "admin"
boot:
admin:
monitor:
read-timeout: 20000
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
logging:
file: "/logs/boot-admin-sample.log" # 日志输出路径

在spring boot admin client客户端加上下面的配置

spring:
application:
name: admin-client
server:
port: 8768 eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
redis:
enabled: false
db:
enabled: false
mongo:
enabled: false
mail:
enabled: false

全部组件如下

2. 自定义通知
可以通过添加实现Notifier接口的Spring Beans来添加您自己的通知程序,最好通过扩展 AbstractEventNotifier或AbstractStatusChangeNotifier。在sc-admin-server工程中编写一个自定义的通知器:

所有的功能在官方提供的英文文档里面都有说明

https://codecentric.github.io/spring-boot-admin/current/#spring-cloud-discovery-support

所谓的离线功能就是如下

之前admin-client

1、在spring admin min中设置要监听哪些服务,哪些服务不监听

在admin server中的配置文件如下所示

server:
port: 9999
spring:
application:
name: service-admin
security:
user:
name: "admin"
password: "admin"
boot:
admin:
monitor:
read-timeout: 20000
discovery:
services: admin-client eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
logging:
file: "/logs/boot-admin-sample.log" # 日志输出路径

这里我们只能监听 admin-client这个服务,其他服务是无法进行监听的

spring boot admin Environment Manager的作用

支持Spring Cloud的postable,env和refresh-endpoint。我们可以集中配置我们程序中的配置,可以运用我们admin后台来修改配置类解决线上一些突发情况。

 支持Spring Cloud的postable,env和refresh-endpoint。我们可以集中配置我们程序中的配置,可以运用我们admin后台来修改配置类解决线上一些突发情况。
/env 和 /env/{toMatch}
该端点与 /configprops 不同,它用来获取应用所有可用的环境属性报告。包括:环境变量、JVM属性、应用的配置配置、命令行中的参数。从下面该端点返回的示例片段中,我们可以看到它不仅返回了应用的配置属性,还返回了系统属性、环境变量等丰富的配置信息,其中也包括了应用还没有使用的配置。所以它可以帮助我们方便地看到当前应用可以加载的配置信息,并配合@ConfigurationProperties注解将它们引入到我们的应用程序中来进行使用。另外,为了配置属性的安全,对于一些类似密码等敏感信息,该端点都会进行隐私保护,但是我们需要让属性名中包含:password、secret、key这些关键词,这样该端点在返回它们的时候会使用*来替代实际的属性值。2
我们在spring boot admin 的中增加一个配置文件

package com.tuling.cloud.study;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "test")
public class Student { private String name;
private Integer age;
private String sex; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}

我们在配置文件中设置下面的配置

application.properties

test.name=fei
test.age=20
test.sex=man

我们在ui上可以看到下面的值

上面会显示程序中所有使用@ConfigurationProperties注解

值如下

现在我们要动态的修改上面属性的值,可以如下

【动态修改环境变量值】
2. 在Environment manager中可以指定property-name,修改property-value,然后update environment。可修改多个
3. 修改后的值在 PropertySource manager 中会显示,在/env端点中也会显示。在@ConfigurationProperties绑定的类中的值会更新。此操作可用来动态更新业务配置
4. 点击 Reset environment,修改的所有值会还原。@ConfigurationProperties绑定的类中的值也还原
5. 进程重启后,所有环境变量值按初始化值

点击页面上的update,我们可以看到调用的接口是

点击update的时候实际上调用的是/actuator/env动态的更新参数,参数更新之后,我们可以通过

package com.tuling.cloud.study.user.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.tuling.cloud.study.Student; @RestController
public class OrderController {
private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
@Autowired
private Student student; @GetMapping("/msg")
public String findById() {
logger.info("================请求用户中心接口==============");
return student.toString();
} }

我们在浏览器上输入,我们可以看到已经实时的更新了参数了

点击页面上的reset按钮,点击 Reset environment,修改的所有值会还原。@ConfigurationProperties绑定的类中的值也还原

底层调用的是/actuator/env的delete请求

这里要特别注意的是,实例暴露的/actuator的全部方法中,如果是post、delete、put请求,在进行更新操作的时候请求头中一定要携带content-type类型,类型必须是Content-Type:application/vnd.spring-boot.actuator.v2+json,这里一定要注意

结论:通过 Environment manager我们可以动态的更新@ConfigurationProperties注解配置的属性

页面上还有一个Refresh context按钮,这个按钮是干啥的,我们来看下

点击的时候,调用的是/actuator/refresh方法是post请求,

具体配置参考博客:https://blog.csdn.net/Anenan/article/details/85134208

@RefreshScope 配置文件自动刷新

1.在类上加@RefreshScope注解。

2.引入配置@Value。

package com.tuling.cloud.study.user.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.tuling.cloud.study.Student; @RestController
@RefreshScope //配置文件自动刷新
public class OrderController {
private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
@Autowired
private Student student; @GetMapping("/msg")
public String findById() {
logger.info("================请求用户中心接口==============");
return student.toString();
} @Value("${test.xzh}") //引入配置
private String xzh; @RequestMapping("/test1")
public String test1(){
return xzh;
} }

这里一定要带上注解@RefreshScope,否则调用/actuator/refresh方法是post请求,也无法动态更新参数

3.配置文件

test.name=fei
test.age=20
test.sex=man
test.xzh=xiangzhenhua
management.security.enabled=false

4.git配置webhooks,也可手动发送POST请求 http://localhost:1003/refresh

默认的值如下

接下来我们动态的来更新test.xzh的值

修改值之后,先调用update方法更新env参数,然后点击refresh context按钮,调用refresh方法更新参数,这样参数就更新了

注意:第一一定要先点击update按钮,在点击refresh confrim按钮

spring boot admin项目的集成和开发的更多相关文章

  1. Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知

    1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...

  2. Spring Boot微服务如何集成fescar解决分布式事务问题?

    什么是fescar? 关于fescar的详细介绍,请参阅fescar wiki. 传统的2PC提交协议,会持有一个全局性的锁,所有局部事务预提交成功后一起提交,或有一个局部事务预提交失败后一起回滚,最 ...

  3. Spring Boot Admin,贼好使!

    Spring Boot Admin(SBA)是一个开源的社区项目,用于管理和监控 Spring Boot 应用程序.应用程序可以通过 http 的方式,或 Spring Cloud 服务发现机制注册到 ...

  4. spring-boot-plus集成Spring Boot Admin管理和监控应用(十一)

    spring-boot-plus集成Spring Boot Admin管理和监控应用 spring boot admin Spring Boot Admin用来管理和监控Spring Boot应用程序 ...

  5. 【Spring Boot】利用 Spring Boot Admin 进行项目监控管理

    利用 Spring Boot Admin 进行项目监控管理 一.Spring Boot Admin 是什么 Spring Boot Admin (SBA) 是一个社区开源项目,用于管理和监视 Spri ...

  6. spring-boot-plus集成Spring Boot Admin管理和监控应用

    Spring Boot Admin Spring Boot Admin用来管理和监控Spring Boot应用程序 应用程序向我们的Spring Boot Admin Client注册(通过HTTP) ...

  7. SpringBoot | 第二十八章:监控管理之Spring Boot Admin使用

    前言 上一章节,我们介绍了Actuator的使用,知道了可通过访问不同的端点路径,获取相应的监控信息.但使用后也能发现,返回的监控数据都是以JSON串的形式进行返回的,对于实施或者其他人员来说,不是很 ...

  8. Spring Boot Admin简介及实践

    问题 在若干年前的单体应用时代,我们可以相对轻松地对整个业务项目进行健康检查.指标监控.配置管理等等项目治理.如今随着微服务的发展,我们将大型单体应用按业务模型进行划分,以此形成众多小而自治的微服务, ...

  9. Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix

    Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...

随机推荐

  1. [JavaWeb基础] 017.Struts2 和 ajax交互简介

    在网页开发中,我们为了只对网页的某块内容进行实时更新,而不对其他不需要更新的内容进行刷新,从而提高响应速度和节省流量,我们采用了页面的异步刷新技术Ajax,那么我们的Struts2框架在这一方面是如何 ...

  2. Rocket - tilelink - BankBinder

    https://mp.weixin.qq.com/s/oZCYBdy5glxJQmYKVWvpvA   简单介绍BankBinder的实现.   ​​   1. 基本介绍   A BankBinder ...

  3. Rocket - diplomacy - BaseNode

    https://mp.weixin.qq.com/s/eOgNLi_MJ8HJOpepGaaW8Q   简单介绍BaseNode的实现.   ​​   1. You cannot create a n ...

  4. Java实现 洛谷 P1579 哥德巴赫猜想(升级版)

    题目背景 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约数的数,如2和11都是质数,而6不是 ...

  5. java实现第五届蓝桥杯等额本金

    等额本金 题目描述 小明从银行贷款3万元.约定分24个月,以等额本金方式还款. 这种还款方式就是把贷款额度等分到24个月.每个月除了要还固定的本金外,还要还贷款余额在一个月中产生的利息. 假设月利率是 ...

  6. 卷积生成对抗网络(DCGAN)---生成手写数字

    深度卷积生成对抗网络(DCGAN) ---- 生成 MNIST 手写图片 1.基本原理 生成对抗网络(GAN)由2个重要的部分构成: 生成器(Generator):通过机器生成数据(大部分情况下是图像 ...

  7. set基本运用

    /* set集合基本用法 */ #include<iostream> #include<set> using namespace std; //set<T>a; v ...

  8. 白嫖永久免费云服务器教程,永久免费虚拟主机、永久免费云数据库、搭建FTP服务器、服务器安装Linux / windows操作系统、服务器部署网站、宝塔一键部署多网站、独立ip、永久国内高速云服务器

    一.准备工作 1. 注册账号 声明:切记不可用服务器做违法的事情 申请地址:https://www.sanfengyun.com/ 图文教程地址:https://www.cnblogs.com/zwn ...

  9. 常见ie9兼容问题

    公司项目要求需要兼容ie9,开发过程中遇到了许多问题,在这里记录一下,希望可以帮到其他需要的小伙伴. 浏览器兼容性问题无外乎三点,css样式兼容.JavaScript兼容及h5部分标签的兼容.主要介绍 ...

  10. 一次性搞懂 PHP 中面向对象的所有知识点。

    OOP是什么? OOP是面向对象编程,面向对象编程是一种计算机编程架构. OOP的基本原则是计算机程序是由单个能起到子程序作用的单元或对象组合而成. 基本概念: 类:定义了事务的抽象特点.包含了数据的 ...