springboot的yml配置文件通过db2的方式整合mysql的教程

所属分类: 软件编程 / java 阅读数: 52
收藏 0 赞 0 分享

springboot整合MySQL很简单,多数据源就master,slave就行了,但是在整合DB2就需要另起一行,以下是同一个yml文件
先配置MySQL,代码如下

spring:
 datasource:
  type: com.alibaba.druid.pool.DruidDataSource
  druid:
   # 主库数据源
   master:
    url: jdbc:mysql://localhost:3308/<数据库名>?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    username: root
    password: 123456
   # 从库数据源
   slave:
    # 从数据源开关/默认关闭
    enabled: true
    url: jdbc:mysql://localhost:3308/<数据库名>?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    username: root
    password: 123456
   # 初始连接数
   initialSize: 5
   # 最小连接池数量
   minIdle: 10
   # 最大连接池数量
   maxActive: 20
   # 配置获取连接等待超时的时间
   maxWait: 60000
   # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
   timeBetweenEvictionRunsMillis: 60000
   # 配置一个连接在池中最小生存的时间,单位是毫秒
   minEvictableIdleTimeMillis: 300000
   # 配置一个连接在池中最大生存的时间,单位是毫秒
   maxEvictableIdleTimeMillis: 900000
   # 配置检测连接是否有效
   validationQuery: SELECT 1 FROM DUAL
   testWhileIdle: true
   testOnBorrow: false
   testOnReturn: false
   webStatFilter: 
    enabled: true
   statViewServlet:
    enabled: true
    # 设置白名单,不填则允许所有访问
    allow:
    url-pattern: /druid/*
    # 控制台管理用户名和密码
    login-username: 
    login-password: 
   filter:
    stat:
     enabled: true
     # 慢SQL记录
     log-slow-sql: true
     slow-sql-millis: 1000
     merge-sql: true
    wall:
     config:
      multi-statement-allow: true

接下来配置DB2

second:
 spring:
  datasource:
   type: com.alibaba.druid.pool.DruidDataSource
   driver-class-name: com.ibm.db2.jcc.DB2Driver
   url: jdbc:db2://<DB2的IP>:<端口>/<数据库名>:currentSchema=<所要连接的schema名>;
   username: <用户名>
   password: <密码>
   # 初始连接数
   initialSize: 5
   # 最小连接池数量
   minIdle: 10
   # 最大连接池数量
   maxActive: 20
   # 配置获取连接等待超时的时间
   maxWait: 60000
   # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
   timeBetweenEvictionRunsMillis: 60000
   # 配置一个连接在池中最小生存的时间,单位是毫秒
   minEvictableIdleTimeMillis: 300000
   # 配置一个连接在池中最大生存的时间,单位是毫秒
   maxEvictableIdleTimeMillis: 900000
   # 配置检测连接是否有效  注意这里DUAL是检测的表名,可以是当前schema下的任意一张表
   validationQuery: SELECT 1 FROM **<检测表名>**
   testWhileIdle: true
   testOnBorrow: false
   testOnReturn: false
   webStatFilter:
    enabled: true
   statViewServlet:
    enabled: true
    # 设置白名单,不填则允许所有访问
    allow:
    url-pattern: /druid/*
    # 控制台管理用户名和密码
    login-username:
    login-password:
   filter:
    stat:
     enabled: true
     # 慢SQL记录
     log-slow-sql: true
     slow-sql-millis: 1000
     merge-sql: true
    wall:
     config:
      multi-statement-allow: true

OK这样就能通过Config获取到了,下面是Config源码

package com.map.framework.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.Utils;
import com.map.common.enums.DataSourceType;
import com.map.common.utils.spring.SpringUtils;
import com.map.framework.config.properties.DruidProperties;
import com.map.framework.datasource.DynamicDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * druid 配置多数据源
 * 
 *
 */
@Configuration
public class DruidConfig
{

 @Bean
 @ConfigurationProperties("spring.datasource.druid.master")
 public DataSource masterDataSource(DruidProperties druidProperties)
 {
  DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
  return druidProperties.dataSource(dataSource);
 }

 @Bean
 @ConfigurationProperties("spring.datasource.druid.slave")
 @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
 public DataSource slaveDataSource(DruidProperties druidProperties)
 {
  DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
  return druidProperties.dataSource(dataSource);
 }

 @Bean
 @ConfigurationProperties("second.spring.datasource")
 public DataSource db2DataSource(DruidProperties druidProperties)
 {
  DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
  return druidProperties.dataSource(dataSource);
 }

 @Bean(name = "dynamicDataSource")
 @Primary
 public DynamicDataSource dataSource(DataSource masterDataSource)
 {
  Map<Object, Object> targetDataSources = new HashMap<>();
  targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
  setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
  setDataSource(targetDataSources, DataSourceType.DB2.name(), "db2DataSource");
  return new DynamicDataSource(masterDataSource, targetDataSources);
 }

 /**
  * 设置数据源
  * 
  * @param targetDataSources 备选数据源集合
  * @param sourceName 数据源名称
  * @param beanName bean名称
  */
 public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
 {
  try
  {
   DataSource dataSource = SpringUtils.getBean(beanName);
   targetDataSources.put(sourceName, dataSource);
  }
  catch (Exception e)
  {
  }
 }
}

这就是我整合MySQL和DB2时遇到的问题,记录一下

更多精彩内容其他人还在看

详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理

这篇文章主要介绍了详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

了解spring中的CloudNetflix Hystrix弹性客户端

这篇文章主要介绍了了解spring中的CloudNetflix Hystrix弹性客户端,客户端弹性模式是在远程服务发生错误或表现不佳时保护远程资源(另一个微服务调用或者数据库查询)免于崩溃。,需要的朋友可以参考下
收藏 0 赞 0 分享

Spark学习笔记Spark Streaming的使用

这篇文章主要介绍了Spark学习笔记Spark Streaming的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

通过实例讲解springboot整合WebSocket

这篇文章主要介绍了通过实例讲解springboot整合WebSocket,WebSocket为游览器和服务器提供了双工异步通信的功能,即游览器可以向服务器发送消息,服务器也可以向游览器发送消息。,需要的朋友可以参考下
收藏 0 赞 0 分享

java虚拟机学习笔记进阶篇

在本篇内容里小编给大家分享了关于java虚拟机学习笔记的进阶内容,需要的朋友们跟着学习下。
收藏 0 赞 0 分享

java虚拟机学习高级篇

在本篇文章里小编给大家整理了关于java虚拟机学习高级篇的相关内容,有兴趣的朋友们跟着学习参考下。
收藏 0 赞 0 分享

java虚拟机中多线程总结

在本篇内容中小编给大家分享的是关于java虚拟机中多线程的知识点总结内容,需要的朋友们参考学习下。
收藏 0 赞 0 分享

java虚拟机多线程进阶篇总结

在本篇内容里小编给大家整理了关于java虚拟机多线程进阶篇的相关知识点内容,有兴趣的朋友们跟着参考下。
收藏 0 赞 0 分享

java数据结构和算法中数组的简单入门

在本文里小编给大家整理了关于java数据结构和算法中数组的简单入门知识点整理,需要的朋友们学习下。
收藏 0 赞 0 分享

java数据结构和算法中哈希表知识点详解

在本篇文章里小编给大家分享了关于java数据结构和算法中哈希表的相关知识点内容,需要的朋友们学习下。
收藏 0 赞 0 分享
查看更多