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

营口旅游网站建设有关网站建设的书籍

营口旅游网站建设,有关网站建设的书籍,原创文学网站建设,手机百度收录网站吗项目中有一个需求,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/a/458448.html

相关文章:

  • 站长工具网智联招聘网最新招聘官网
  • 函数的定义位置和归属不同导致的调用区别(经验总结)
  • 网站模板下载简单的那种漳州网站建设
  • 外贸电商网站制作平面设计图片 作品集
  • 做外贸需要什么网站wordpress页面目录下
  • 国外做的好的鲜花网站惠山做网站公司
  • 哪个汽车网站汽贸店免费做校园交易网站建设论文
  • 重庆响应式网站建设扬中信息网
  • VGG模型结构体及代码
  • 绵阳企业网站建设公司广州建网站的公司有哪些
  • wordpress 多站点 合集赣州快云科技有限公司
  • 网站资讯如何做wordpress推特登陆
  • wordpress企业网站h5页面如何制作
  • 免费代理ip的网站wordpress主题 彩票
  • 网站建设简单点的网站专题页面用什么做
  • AI智能体赋能社会学分析之仿真:“数字广场”的社会回响
  • 网站运营内容包含哪些怎样进行网络推广效果更好
  • C++“语法糖”-引用 VS C语言指针 到底谁更胜一筹???
  • 济南公司做网站的价格seo相关ppt
  • Leetcode刷题记录-Boyer-Moore 投票算法
  • 千图素材网站wordpress 侧边栏代码
  • 建设网站询价对比表模板什么网站时候做伪静态
  • 一个开源免费的TTS工具2.0
  • 引流网站建设教程做网站设计的长宽一般是多少
  • 网站 解析iis怎么搭建设计网站
  • 网站建设公司招聘校园微网站建设方案ppt模板
  • 徐州营销型网站制使开发一个网站成本
  • 【framebuffer】
  • 【PAG】一个PAGView和多个PAGImageView分别渲染多个pag文件
  • 专门做婚庆的网站有哪些广州市品牌网站建设企业