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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

哇哈哈_   2021-03-15 我要评论
想了解springboot的yml配置文件通过db2的方式整合mysql的教程的相关内容吗,哇哈哈_在本文为您仔细讲解springboot yml配置文件整合mysql的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot,yml配置文件整合mysql,springboot,yml配置文件,下面大家一起来学习吧。

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时遇到的问题,记录一下

总结

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们