且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

功能开关***实践

更新时间:2022-09-03 22:54:52

前提条件

  • 功能开关Agent方式接入,与流量防护共用Agent,配置-Dahas.switch.agent.plugin.group.enabled=true
  • 功能开关SDK方式接入,参考使用 SDK 接入
  • 功能开关Spring Boot Starter 方式接入,参考使用 Spring Boot Starter 接入

版本:1.1.11起


Spring Demo 文件

@Value 配置

UserController类对象,用于测试@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类对象,用于测试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


配置注册

微服务启动后可在 ‘微服务治理中心>应用配置’菜单看到接入的应用。

功能开关***实践

点击应用进入后可看到开关配置项,对应的配置项可以分组方式展示,分组名为识别出的类名。

开关名为类中字段名,描述内容在@Value 方式为注解中内容,@ConfigurationProperties方式为Spring配置文件中配置项。

功能开关***实践


配置修改

分为单机推送与全局推送两种方式,单机推送的值不会持久化,仅当前微服务实例生命周期有效,全局推送方式会进行值持久化,微服务实例再次启动后可看到持久化值。

单机推送

对RequestProperties,port字段执行单机推送

功能开关***实践

此时应用中port字段即被修改为'8083',可在控制台查看

功能开关***实践

类似的可对UserController中num字段进行单机推送

功能开关***实践

可在推送记录中查看历史操作

功能开关***实践


全局推送

对Destination,max字段执行全局推送

功能开关***实践

推送后可见修改后的值,同时数据已经持久化。

功能开关***实践

类似的可对UserController中destinationStr字段进行全局推送,

功能开关***实践

推送后内存值变更可在控制台查看,同时开关值已持久化。

功能开关***实践

配置持久化

初始化阶段

重启应用,在InitializingBean,afterPropertiesSet函数初始化阶段与@PostConstruct初始化阶段均可被读取到已持久化的值。

可通过测试demo中的启动日志查看,内容如下

功能开关***实践

@ConfigurationProperties 配置

在控制台观察 RequestProperties 可看到全局推送方式推送的‘inter’配置项为持久化值,而单机推送的‘port’配置项仍为Spring原始配置内容。

功能开关***实践

@Value 配置

在控制台观察 UserController 可看到全局推送方式推送的‘destinationStr’配置项为持久化值,而单机推送的‘num’配置项仍为Spring原始配置内容。

功能开关***实践

历史记录

可在‘历史记录’菜单查看推送记录。

功能开关***实践