spring boot admin项目的集成和开发
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); } }
四、集成 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>
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后台来修改配置类解决线上一些突发情况。

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按钮
posted on 2019-12-14 15:03 luzhouxiaoshuai 阅读(1757) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!