spring - Spring Boot 2.4.x - @RefreshScope 不工作
问题描述
我已经浏览了许多链接,例如@RefreshScope 和 /refresh not working和Spring Boot 2: Refresh properties on the fly not working,但仍然对我不起作用。
我开发了 spring-cloud-config-server
当我打
POST ==> http://localhost:8002/actuator/refresh
我得到回应
[“config.client.version”]
看起来它以某种方式没有加载更改的属性
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 https://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.4.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
属性访问Bean.java:
@Component
@ConfigurationProperties(prefix="property-file")
public class PropertyAccessBean {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
属性访问值.java:
public class PropertyAccessValue {
private String name;
private String description;
public PropertyAccessValue(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "PropertyAccessValue{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
用户控制器.java:
@RestController
@RequestMapping("/users")
@RefreshScope
@Slf4j
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/")
public User saveUser(@RequestBody User user) {
log.info("Inside saveUser of UserController");
return userService.saveUser(user);
}
@GetMapping("/{id}")
public ResponseTemplateVO getUserWithDepartment(@PathVariable("id") Long userId) {
log.info("Inside getUserWithDepartment of UserController");
// return userService.getUserWithDepartment(userId);
// return userService.getUserWithDepartmentWithFeign(userId);
return userService.getUserWithDepartmentWithFeignAndResilient4J(userId);
}
@Autowired
PropertyAccessBean propertyAccessBean;
@GetMapping("/readkey")
public PropertyAccessValue accesPropertyFile(){
return new PropertyAccessValue(propertyAccessBean.getName(),
propertyAccessBean.getDescription());
}
}
引导程序属性:
spring:
cloud:
config:
enabled: true
uri: http://localhost:9296
应用程序.yml:
server:
port: 8002
spring:
application:
name: user-service
resilience4j:
circuitbreaker:
instances:
department:
register-health-indicator: true
ring-buffer-size-in-closed-state: 5
ring-buffer-size-in-half-open-state: 3
wait-duration-in-open-state: 10s
failure-rate-threshold: 50
record-exceptions:
- org.springframework.web.client.HttpServerErrorException
- org.springframework.web.client.ResourceAccessException
- java.util.concurrent.TimeoutException
- java.io.IOException
management:
endpoints:
web:
exposure:
include: refresh
endpoint:
health:
show-details: always
解决方案
错误在于您的bootstrap.properties
文件实际上是 YAML 而不是属性文件。有关属性文件格式,请参阅文档。您的文件相当于以下内容:
spring=
cloud=
config=
enabled=true
uri=http://localhost:9296
所以这个文件没有设置sping.cloud.config.enabled
为true
,而是一些名为 的属性enabled
,这可能是未使用的。
推荐阅读
- c++ - 比较它们之间的元素并获取共同值的键
- validation - 验证 Spring Kafka 有效负载
- excel - Excel Power Query - 防止向数据单元格和列名添加前缀
- .htaccess - Htaccess 重定向 / 和 index.php 到 HTTPS
- angular6 - Angular 6 在库服务中注入窗口令牌
- azure - 如果我更改我的 web 应用程序用于身份验证的 Active Directory 应用程序,注册用户会发生什么情况?
- c# - For循环计数器总是在C#递归函数中重置
- highcharts - 如何增加实时更新样条图中的点数?
- ios - NSPredicate 检查核心数据关系中前 5 个对象的属性
- python - Pyspark 分布式矩阵求和非空值