如何解决Spring Boot 2.4.x - @RefreshScope 不工作
我已经浏览了许多链接,例如 @RefreshScope and /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>
PropertyAccessBean.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;
}
}
PropertyAccessValue.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 + '\'' +
'}';
}
}
UserController.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());
}
}
bootstrap.properties:
spring:
cloud:
config:
enabled: true
uri: http://localhost:9296
application.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 而不是属性文件。请参阅documentation for properties file format。您的文件相当于以下内容:
spring=
cloud=
config=
enabled=true
uri=http://localhost:9296
所以这个文件没有将 sping.cloud.config.enabled
设置为 true
,而是将一些名为 enabled
的属性设置为可能未使用。
经过 10 个小时的疯狂调试后,我不得不将 yml 替换为属性文件并开始工作。
我不知道为什么 yml 无法获取更改,这肯定是一个错误!
application.properties
spring.cloud.config.enabled=true
spring.cloud.config.uri=http://localhost:9296
management.endpoints.web.exposure.include=*
management.endpoint.health.enabled=true
server.port=8002
spring.application.name=user-service
resilience4j.circuitbreaker.instances.department.register-health-indicator=true
resilience4j.circuitbreaker.instances.department.ring-buffer-size-in-closed-state=5
resilience4j.circuitbreaker.instances.department.ring-buffer-size-in-half-open-state=3
resilience4j.circuitbreaker.instances.department.wait-duration-in-open-state=10s
resilience4j.circuitbreaker.instances.department.failure-rate-threshold=50
resilience4j.circuitbreaker.instances.department.record-exceptions=org.springframework.web.client.HttpServerErrorException
这是回复
[
"property-file.name","config.client.version","property-file.description"
]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。