使用nacos实现动态配置

我这里以influxDB配置的连接池为例

package sca.pro.business.common.influxdb;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.influxdb.InfluxDB;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@EnableConfigurationProperties({InfluxDBPoolFactory.class})
@Configuration
@Log4j2
public class InfluxDBPool implements InitializingBean {

    @Resource
    private NacosConfigManager nacosConfigManager;


    @Autowired
    InfluxDBPoolFactory influxDBPoolFactory;

    @Autowired
    private  TaskExecutor taskExecutor;

    private static GenericObjectPool<InfluxDB> pool;

    @Value("${spring.influx.poolMaxSize}")
    private  Integer poolMaxSize;

    @Value("${spring.influx.poolMaxIde}")
    private  Integer poolMaxIde;


    @PostConstruct
    public  void initPool() throws Exception {
        //连接池配置
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(poolMaxIde);
        genericObjectPoolConfig.setMinIdle(2);
        genericObjectPoolConfig.setMaxTotal(poolMaxSize);
//        genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis();
        genericObjectPoolConfig.setTestWhileIdle(true);
        genericObjectPoolConfig.setNumTestsPerEvictionRun(10);
        genericObjectPoolConfig.setSoftMinEvictableIdleTimeMillis(-1);
        genericObjectPoolConfig.setMinEvictableIdleTimeMillis(-1);
        pool = new GenericObjectPool<>(influxDBPoolFactory,genericObjectPoolConfig);
        pool.addObjects(Math.min(poolMaxIde,poolMaxSize));

    }



    public static InfluxDB getConnection() {

        try {

            return pool.borrowObject();

        } catch (Exception e) {

            e.printStackTrace();

            return null;

        }

    }


    public static void returnConnection(InfluxDB influxDB) {

        pool.returnObject(influxDB);

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //对nacos中动态线程池的配置文件dynamic-threadpool.properties注册监听
        nacosConfigManager.getConfigService().addListener("sca-cms-business.yaml", "DEFAULT_GROUP",
                new Listener() {
                   //配置检测的线程池
                    @Override
                    public Executor getExecutor() {
                        return taskExecutor;
                    }
                    //当有参数更新后执行的逻辑
                    @Override
                    public void receiveConfigInfo(String configInfo) {
                        dynamicRefresh(configInfo);
                    }
                });
    }
    private void dynamicRefresh(String configInfo) {
        log.info("influxdb连接池配置发生变化");
        Properties properties = new Properties();
        try {
            //获取到所有的配置参数,转为properties
            properties.load(new StringReader(configInfo));
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("修改前配置为poolMaxSize={},poolMaxIde={}",pool.getMaxTotal(),pool.getMaxIdle());
       pool.setMaxTotal(Integer.valueOf(properties.get("poolMaxSize").toString()));
        pool.setMaxIdle(Integer.valueOf(properties.get("poolMaxIde").toString()));
        log.info("修改后配置为poolMaxSize={},poolMaxIde={}",properties.get("poolMaxSize").toString(),properties.get("poolMaxIde").toString());
        //根据线程池配置刷新线程池实例
    }
    @PreDestroy
    public void destroy() {
        if (pool != null) {
            pool.close();
        }
    }


}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EntyIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值