温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot中如何实现druid多数据源配置

发布时间:2021-12-20 11:07:23 来源:亿速云 阅读:1435 作者:小新 栏目:大数据

这篇文章主要为大家展示了“spring boot中如何实现druid多数据源配置”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“spring boot中如何实现druid多数据源配置”这篇文章吧。

application.yml

mybatis:
  configLocation: classpath:mybatis_config.xml
spring:
  #  data:
  #    mongodb:
  #      host: 10.0.5.126
  #      port: 27017
  #      database: xx
  #authentication-database: xx
  #password:
  #username:
  datasource:
    druid:
      min-idle: 3
      initial-size: 5
      max-active: 10
      # 连接等待超时时间
      max-wait: 10000
      # 配置检测可以关闭的空闲连接间隔时间
      time-between-eviction-runs-millis: 60000
      # 配置连接在池中的最小生存时间
      min-evictable-idle-time-millis: 300000
      validation-query: select '1'
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      pool-prepared-statements: true
      max-open-prepared-statements: 20
      max-pool-prepared-statement-per-connection-size: 20
      filter:
        stat:
          merge-sql: true
          # 慢日志查询
          log-slow-sql: true
          # 慢SQL记录超过5秒的sql在druid控制台标红
          slow-sql-millis: 5000
        wall:
          enabled: false
        commons-log:
          enabled: false
        log4j:
          connection-log-enabled: false
        slf4j:
          statement-log-enabled: false
        log4j2:
          statement-log-enabled: false
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 如果项目用的logback则删掉log4j
      filters: stat,wall
      # 配置Druid Spring监控切面
      #      aop-patterns: com.xitor.service.*,com.xonitor.schedule.*,com.xonitor.controller.*
      # 配置DruidStatFilter
      web-stat-filter:
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*,/slife_server/druid/*"
      # 配置DruidStatViewServlet
      stat-view-servlet:
        enabled: true
        url-pattern: "/druid/*"
        allow: ""  # allow: 127.0.0.1,192.168.163.1 IP白名单(没有配置或者为空,则允许所有访问)
        # IP黑名单 (存在共同时,deny优先于allow)
        #        deny: 192.168.1.73
        #  禁用HTML页面上的“Reset All”功能 true是不禁用
        reset-enable: true
        login-username: admin
        login-password: xx
      use-global-data-source-stat: false
      clear-filters-enable: true
      time-between-log-stats-millis: 3600000 #配置每1小时输出一次统计日志,统计后将清空日志
    oms:
      name: oms
      driver-class-name: org.postgresql.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:postgresql://127.0.0.1:5432/eagle
      username: xx
      password: xx
    star-talk:
      name: starTalk
      driver-class-name: org.postgresql.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:postgresql://10.0.5.105:5432/ejabberd
      username: xx
      password: xx
    logging:
      name: logging
      driver-class-name: org.postgresql.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:postgresql://127.0.0.1:5432/eagle
      username: xx
      password: xx
    wpms: 
      name: wpms
      driver-class-name: oracle.jdbc.driver.OracleDriver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:oracle:thin:@localhost:1521:ORCL
      username: xx
      password: xx

DataSourceConfigurer

@Configuration
public class OMSDataSourceConfigurer {

    @Value("${mybatis.configLocation}")
    private Resource configLocation;

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.oms")
    public DataSourceProperties omsDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(initMethod = "init")
    @Primary
    @ConfigurationProperties("spring.datasource.druid")
    public DruidDataSource omsDataSource(@Qualifier("omsDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return (DruidDataSource) dataSourceProperties.initializeDataSourceBuilder()
                .build();
    }

    @Bean
    @Primary
    public SqlSessionFactory omsSqlSessionFactory(@Qualifier("omsDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        Assert.notNull(this.configLocation, "");
        Assert.isTrue(this.configLocation.exists(), "");
        bean.setConfigLocation(configLocation);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis_mapper/oms/*.xml"));
        return bean.getObject();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager omsTransactionManager(@Qualifier("omsDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    @Primary
    public SqlSessionTemplate omsSqlSessionTemplate(@Qualifier("omsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

WPMSDataSourceConfigurer

与上面配置相同,不要添加 @Primary

MybatisConfigurer

@Configuration
@ComponentScan("com.gridvo.eagle.repository")
@MapperScan(basePackages = "com.xx.mybatis.oms.dao", sqlSessionTemplateRef = "omsSqlSessionTemplate")
@MapperScan(basePackages = "com.xx.mybatis.startalk.dao", sqlSessionTemplateRef = "starTalkSqlSessionTemplate")
@MapperScan(basePackages = "com.xx.mybatis.logging.dao", sqlSessionTemplateRef = "loggingSqlSessionTemplate")
@MapperScan(basePackages = "com.xxmybatis.wpms.dao", sqlSessionTemplateRef = "wpmsSqlSessionTemplate")
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisConfigurer {

}

以上是“spring boot中如何实现druid多数据源配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI