FLink整合Nacos进行动态配置更新

Source

public class FlinkNacosSource extends RichSourceFunction<String> {

    private Properties properties;
    private ConfigService configService;
    private String config;
    private String dataId;
    private String group;
    private String serverAddr;
    private static final long NACOS_TIMEOUT_MS=5000L;

    public FlinkNacosSource() { }

    public FlinkNacosSource(Properties properties, String dataId, String group,String serverAddr) {
        this.properties = properties;
        this.dataId = dataId;
        this.group = group;
        this.serverAddr = serverAddr;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        if(properties != null){
            configService = NacosFactory.createConfigService(properties);

        }else if(!org.apache.commons.lang3.StringUtils.isBlank(serverAddr)){
            configService = NacosFactory.createConfigService(serverAddr);
        }else {
            throw new RuntimeException("I have to know the absolute address of Nacos,Otherwise, it will not work");
        }

        config = configService.getConfig(dataId,group,NACOS_TIMEOUT_MS);


        configService.addListener(dataId, group, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                config = configInfo;
            }
        });

    }

    @Override
    public void run(SourceContext sourceContext) throws Exception {
        while(true){
            Thread.sleep(2000L);
            String[] split = config.split("\r\n");
            for (String s : split) {
                String[] split1 = s.split("=");
                String key = split1[0];
                String value = split1[1];
                System.out.println(key +"="+value);
            }
            
            sourceContext.collect(String.valueOf(System.currentTimeMillis()));
        }
    }

    @Override
    public void cancel() {
        //可以写钉钉报警
    }


   public  static class Builder {
        private Properties properties;
        private String dataId;
        private String group;
        private String serverAddr;

        public Builder setProperties(Properties properties) {
            this.properties = properties;
            return this;
        }

        public Builder setDataId(String dataId) {
            this.dataId = dataId;
            return this;
        }

        public Builder setGroup(String group) {
            this.group = group;
            return this;
        }


        public Builder setIp(String serverAddr) {
            this.serverAddr = serverAddr;
            return this;
        }

        public FlinkNacosSource builder() {
            return new FlinkNacosSource(this.properties, this.dataId, this.group,this.serverAddr);
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值