溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

spring boot中如何實現(xiàn)druid多數(shù)據(jù)源配置

發(fā)布時間:2021-12-20 11:07:23 來源:億速云 閱讀:1428 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要為大家展示了“spring boot中如何實現(xiàn)druid多數(shù)據(jù)源配置”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“spring boot中如何實現(xiàn)druid多數(shù)據(jù)源配置”這篇文章吧。

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
      # 配置檢測可以關(guān)閉的空閑連接間隔時間
      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
      # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻 如果項目用的logback則刪掉log4j
      filters: stat,wall
      # 配置Druid Spring監(jiān)控切面
      #      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優(yōu)先于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小時輸出一次統(tǒng)計日志,統(tǒng)計后將清空日志
    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中如何實現(xiàn)druid多數(shù)據(jù)源配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI