When testing the content of the configuration file, we wrote this Controller, and now we need to add a new @refreshScope annotation, which actually declares the scope of automatic configuration update, so we need to add @refreshScope annotation:
@RestController @RequestMapping("/env") @RefreshScope public class EnvController { @Value("${env}") private String env; @GetMapping("print") public String printEnv(){ return env; }}Copy the code
Instead of doing this, we tend to centralize the configuration and declare it on top of the configuration class, assuming that the current Git repository’s shop-dev.yml configuration looks like this:
server:
port: 8090
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/
spring:
application:
name: shop
rabbitmq:
host: 192.168.79.128
port: 5672
username: guest
password: guest
env: dev
boy:
name: Tim
age: 18
Copy the code
Create a new JavaBean called Boy in the config directory, and add @refreshScope to the Boy:
package xpu.edu.shop_service.config;
@Data
@Component
@ConfigurationProperties(prefix = "boy")
@RefreshScope
public class Boy {
private String name;
private int age;
}
Copy the code
At this point, the Controller is changed as follows:
@RestController @RequestMapping("/env") public class EnvController { @Autowired private Boy boy; @GetMapping("print") public String printEnv(){ return "name:" + boy.getName() + " age:" + boy.getAge(); }}Copy the code
Using the /actuator/bus-refresh interface manually, the refresh is achieved, as shown in the following figure:
When updates or other events occur, the /actuator/bus-refresh interface is automatically accessed by the Git-hosting platform and the configuration files are automatically refreshed.