Spring 启动 2.4.x - @RefreshScope 不工作
Spring Boot 2.4.x - @RefreshScope is not working
我已经浏览了很多链接,例如 and 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
经过 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"
]
错误在于您的 bootstrap.properties
文件实际上是 YAML 而不是属性文件。请参考documentation for properties file format。您的文件相当于以下内容:
spring=
cloud=
config=
enabled=true
uri=http://localhost:9296
所以这个文件 不是 将 sping.cloud.config.enabled
设置为 true
,而是将一些 属性 称为 enabled
,这是可能没用过。
我已经浏览了很多链接,例如
我开发了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
经过 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"
]
错误在于您的 bootstrap.properties
文件实际上是 YAML 而不是属性文件。请参考documentation for properties file format。您的文件相当于以下内容:
spring=
cloud=
config=
enabled=true
uri=http://localhost:9296
所以这个文件 不是 将 sping.cloud.config.enabled
设置为 true
,而是将一些 属性 称为 enabled
,这是可能没用过。