我这里以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();
}
}
}