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

温州网站制作套餐seo优化方向

温州网站制作套餐,seo优化方向,百度做的网站一般在什么后台,国内比较好的saas平台项目中有一个需求,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/468934.html

相关文章:

  • 住房和城乡建设局部网站域名注册查询官网
  • 网站建设有什么用张雷明履新河南省委常委
  • 漳州微信网站建设中国十大营销策划机构
  • 搜索网站建设寻找客户的12种方法
  • 学做内账的网站苏州seo报价
  • cms建站系统开源2024最火的十大新闻
  • 淄博做网站海外短视频软件
  • 长春火车站位置武汉网络营销推广
  • wordpress旧版本哪个好些windows优化大师自动安装
  • 网站建设报告心得体会百度搜索引擎推广
  • 电商网站开发平台推广软件平台
  • 建筑模板多少钱一平方企业网站seo优化公司
  • 做英文网站多钱快排seo软件
  • 建设安全监督站的网站温州seo按天扣费
  • 合肥滨湖建设指挥部网站seo技术培训海南
  • 二手网站专业做附近人的有吗结构优化
  • 网站如何更换域名百度营销推广官网
  • 渭南做网站的公司如何做网络推广人员
  • 房产这么做网站才多点击量8大营销工具
  • 物流网站设计与实现谷歌推广公司
  • 找哪个公司做网站推广最好网络营销咨询公司
  • 长春好的做网站公司排名怎么做一个自己的网站
  • 绵阳免费网站建设seo指搜索引擎
  • 网站怎么做搜素引擎河南郑州最近的热搜事件
  • 网站建设应该怎么做手机百度搜索引擎
  • 电脑配件电子商务网站设计方案广州网络推广培训
  • pos机做网站推广广州王牌seo
  • asp.net开发移动网站模板下载看片子用什么app免费苹果手机
  • 南平 建网站网站排名优化怎样做
  • 做公众号的网站模板下载吗深圳排名seo公司