当前位置: 首页 > wzjs >正文

温州网站制作套餐太原seo优化公司

温州网站制作套餐,太原seo优化公司,网站开发和网页设计的区别,兰州市政府门户网站作风建设年活动作风评议项目中有一个需求,spring boot项目连接postgres数据库的地址,是存储在etcd当中的,在程序启动后,当etcd中的地址变化时,需要程序去连接新的postgres地址。 1. 修改Datasource定义,改为使用DynamicPGDataSou…

项目中有一个需求,spring boot项目连接postgres数据库的地址,是存储在etcd当中的,在程序启动后,当etcd中的地址变化时,需要程序去连接新的postgres地址。

1. 修改Datasource定义,改为使用DynamicPGDataSource,它是一个自定义类,集成了

AbstractRoutingDataSource
@Primary@Beanpublic DataSource druidDataSource() {DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();druidDataSource.setUrl(url);druidDataSource.setUsername(username);druidDataSource.setPassword(password);druidDataSource.setDriverClassName(driverClassName);druidDataSource.setInitialSize(druidInitSize);// 初始化连接数druidDataSource.setMinIdle(druidMinIdle); // 最小连接数druidDataSource.setMaxActive(druidMaxActive);// 最大连接数druidDataSource.setPoolPreparedStatements(true);// 开启缓存preparedStatementdruidDataSource.setUseGlobalDataSourceStat(true);// 开启Druid提供的3s慢SQL监控Properties properties = new Properties();properties.put("druid.stat.mergeSql", true);properties.put("druid.stat.slowSqlMillis", 3000);druidDataSource.setConnectProperties(properties);try {druidDataSource.setFilters("stat,wall");druidDataSource.init();} catch (SQLException e) {log.error("构建数据库连接池异常,异常原因:{}", e);throw new RuntimeException(e);}DynamicPGDataSource dynamicPGDataSource = new DynamicPGDataSource();Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put(ConnectInfo.currentPGIP, druidDataSource);dynamicPGDataSource.setTargetDataSources(targetDataSources);dynamicPGDataSource.setDefaultTargetDataSource(druidDataSource);return dynamicPGDataSource;}

2. 类定义

public class DynamicPGDataSource extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey() {return DataSourceContext.getPGDataSource();}
}
public class DataSourceContext {private static String pgDataSource;public static void setPGDataSource(String ds) {pgDataSource = ds;}public static String getPGDataSource() {return pgDataSource;}
}

3. 定义re方法,当监听到数据源IP更改之后,去切换连接到新的数据源,并且关闭老的数据源连接。

public void refreshPGDataSource(String ip) {if(ConnectInfo.currentPGIP.equals(ip)) {log.info("currentPGIP equals ip, not operate, ip:{}", ip);}DynamicPGDataSource dynamicPGDataSource = ((DynamicPGDataSource)druidDataSource);Field field = null;try {field = AbstractRoutingDataSource.class.getDeclaredField("targetDataSources");} catch (NoSuchFieldException e) {e.printStackTrace();}field.setAccessible(true);// 获取当前的 targetDataSourcesMap<Object, Object> currentDataSources = null;try {currentDataSources = (Map<Object, Object>) field.get(dynamicPGDataSource);if(!currentDataSources.containsKey(ip)) {DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();String address = String.format("jdbc:postgresql://%s:5432/dsgdb?characterEncoding=utf-8&useSSL=false", ip);druidDataSource.setUrl(address);druidDataSource.setUsername(username);druidDataSource.setPassword(password);druidDataSource.setDriverClassName(driverClassName);druidDataSource.setInitialSize(20);// 初始化连接数druidDataSource.setMinIdle(10); // 最小连接数druidDataSource.setMaxActive(100);// 最大连接数druidDataSource.setPoolPreparedStatements(true);// 开启缓存preparedStatementdruidDataSource.setUseGlobalDataSourceStat(true);// 开启Druid提供的3s慢SQL监控Properties properties = new Properties();properties.put("druid.stat.mergeSql", true);properties.put("druid.stat.slowSqlMillis", 3000);druidDataSource.setConnectProperties(properties);druidDataSource.setFilters("stat,wall");currentDataSources.put(ip, druidDataSource);field.set(dynamicPGDataSource, currentDataSources);dynamicPGDataSource.afterPropertiesSet();}DataSourceContext.setPGDataSource(ip);ConnectInfo.currentPGIP = ip;// 关闭无用连接Iterator<Map.Entry<Object, Object>> iterator = currentDataSources.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<Object, Object> entry = iterator.next();if(!ip.equals(entry.getKey())) {DruidDataSource dataSource = (DruidDataSource)currentDataSources.get(entry.getKey());dataSource.close();iterator.remove();}}} catch (IllegalAccessException | SQLException e) {e.printStackTrace();}}

http://www.dtcms.com/wzjs/409096.html

相关文章:

  • 网站搜索引擎优化方案论文惠州seo外包
  • 制作一个网站要多少钱东莞网络推广招聘
  • 判断网站的好坏网站在线客服系统免费
  • 华为网站建设和阿里云哪个好百度旗下有哪些app
  • 哪里有免费的网站域名长尾关键词挖掘网站
  • 计算机网站建设和维护海南百度推广公司
  • 网站开发属于软件设计嘛北京seo网站优化公司
  • 做印刷网站公司哪家好b站推广在哪里
  • 网站怎么做优化推广网络整合营销策划书
  • 4a级旅游网站建设的要求什么叫做优化
  • 做网站毕业答辩会问什么站长工具网
  • python做网站guthub关键词优化seo公司
  • 做期货浏览哪些网站百度服务商
  • 网站建设动图代码seo系统培训班
  • 网站开发什么比较有创意百度网络推广怎么收费
  • 加强党政建设网站百度快照怎么发布
  • ps做字幕模板下载网站国内seo排名分析主要针对百度
  • 荣成住房和城乡建设部网站系统开发
  • 天津网站设计佛山关键词排名效果
  • 汽车网站管理系统seo网站排名优化公司哪家
  • wordpress常见的15个问题百度seo关键词排名优化教程
  • 夏都通app下载最新版本seo关键词如何布局
  • 视频制作的详细步骤宁波企业seo推广
  • 日本a片女人和狗做的网站鹤壁网站推广公司
  • 宁波建设网站哪家好百度提交工具
  • 页面模板微信seo优化招聘
  • 花都手机网站建设网络营销知识
  • wordpress图片站模板公司的网站制作
  • 沙井做网站引流推广效果好的app
  • 查找网站备案号推广普通话的意义50字