本文介绍如何对Spring Config的配置项执行修改或者持久化等操作。
前提条件
- Agent方式接入,与流量防护共用Agent,配置-Dahas.switch.agent.plugin.group.enabled=true。
- 应用配置SDK方式接入。具体操作,请参见使用SDK接入。
- 应用配置Spring Boot Starter方式接入。具体操作,请参见使用Spring Boot Starter接入。
背景信息
Spring Demo文件:
- UserController类对象:为@Value配置,用于测试@PostConstruct注解,此阶段可识别持久化值,部分内容如下。
@RestController public class UserController { @Value("${project.name}") public String name; @Value("${user.ahas}") public boolean ahas; @Value("${user.number}") public int num; @Value("${destination}") public String destinationStr; @Autowired private Destination destination; @PostConstruct private void init(){ System.out.println("[UserController] init() value: "+ destinationStr +" , " + num + " , "+ ahas + " , " + name); System.out.println("[UserController] init() configuration: "+destination.getAvg()+" , " + destination.getMax() + " , "+ destination.getMin()); } }
- DemoConfig类对象:为@Value配置,用于测试InitializingBean,afterPropertiesSet函数,初始化阶段可读取到持久化值,内容如下。
@Configuration public class DemoConfig implements InitializingBean { @Autowired private RequestProperties requestProperties; @Override public void afterPropertiesSet() { System.out.println("[DemoConfig] init() port: " + requestProperties.getPort() + " ,interface: " + requestProperties.getInter()); } }
- RequestProperties类对象:为@ConfigurationProperties配置,Value模式。
@Component @ConfigurationProperties(value = "request") public class RequestProperties { private int port; private String inter; public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getInter() { return inter; } public void setInter(String inter) { this.inter = inter; } }
- Destination类对象:为@ConfigurationProperties配置,Prefix模式。
@Component @ConfigurationProperties(prefix = "property.destination") public class Destination { private int max; private int min; private int avg; public int getMax() { return max; } public void setMax(int max) { this.max = max; } public int getMin() { return min; } public void setMin(int min) { this.min = min; } public int getAvg() { return avg; } public void setAvg(int avg) { this.avg = avg; } }
- application.properties配置内容如下。
user.ahas=false user.number=123 request.port=8081 request.inter=/hello destination=sun property.destination.max=300 property.destination.min=10 property.destination.avg=100
查看应用的开关配置
开关配置值的修改
对开关配置值的修改分为单机推送和全局推送两种方式。单机推送的值不会持久化,仅当前微服务实例生命周期有效;全局推送方式会进行值持久化,微服务实例再次启动后可看到持久化值。
单机推送
例如,对RequestProperties类对象中的port字段执行单机推送。

推送后应用中的deport字段的值已被修改,可在控制台中查看推送结果。

同样的方式,您还可以对UserController类对象中num的字段执行单机推送。您可在历史记录中查看历史推送操作。具体操作,请参见查看历史推送记录。
全局推送
例如,对Destination类对象中的max字段执行全局推送。

推送后应用中的max字段已被修改,同时数据已经持久化,可在控制台中查看推送结果。

同样的方式,您还可以对UserController类对象中的destinationStr字段执行全局推送。
开关配置值的持久化验证
初始化阶段
重启应用,在InitializingBean,afterPropertiesSet函数初始化阶段与@PostConstruct初始化阶段均可被读取到已持久化的值。
可通过测试Demo中的启动日志查看,内容如下:

@ConfigurationProperties配置
在控制台可看到通过全局推送方式推送的RequestProperties类对象的inter字段的配置项为持久化值,而通过单机推送的port字段的配置项仍为Spring原始配置内容。
@Value配置
在控制台可看到通过全局推送方式推送的UserController类对象的destinationStr字段的配置项为持久化值,而通过单机推送的num字段的配置项仍为Spring原始配置内容。
查看历史推送记录
- 登录 AHAS控制台,然后在页面左上角选择地域。
- 在控制台左侧导航栏选择功能开关,在应用列表页面单击目标应用的资源卡片。进入目标应用的开关列表页面。
- 在开关列表页面选择System页签,然后单击右上角的历史记录,可查看以往所有的推送记录。
日志动态调整
测试样例如下:
private static final Random RANDOM = new Random();
private static final Logger logger = LoggerFactory.getLogger("xx");
@GetMapping("/hello")
public ResponseEntity<String> hello(){
int random = RANDOM.nextInt(100);
logger.info("[SystemLog] ------------------- info : {} ",random);
logger.debug("[SystemLog] ------------------- debug : {} ",random);
logger.error("[SystemLog] ------------------- error : {} ",random);
if(random < 30) {
return new ResponseEntity<String>("BAD", HttpStatus.BAD_REQUEST);
} else if(random > 50) {
return new ResponseEntity<String>("BAD", HttpStatus.SERVICE_UNAVAILABLE);
} else {
return new ResponseEntity<String>("OK", HttpStatus.OK);
}
}
灰度推送
您还可以在控制台设置开关的灰度推送。