springboot2.0加入druid后使用的还是Hikari 5C

配置

 spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    master:
      jdbcUrl: jdbc:mysql://127.0.0.1:3306/spring_boot_learning?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
      username: root
      password: root3306
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      # 初始化大小,最小,最大
      initialSize: 5
      minIdle: 5
      maxActive: 20
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,log4j
      # 合并多个DruidDataSource的监控数据
      #useGlobalDataSourceStat: true
    slave:
      jdbcUrl: jdbc:mysql://127.0.0.1:3306/spring_boot_learning_one?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
      username: root
      password: root3306
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      # 初始化大小,最小,最大
      initialSize: 5
      minIdle: 5
      maxActive: 20
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,log4j
      # 合并多个DruidDataSource的监控数据
      #useGlobalDataSourceStat: true

主数据源配置

 @Configuration
@MapperScan(basePackages = "indi.xulala.dao.master", sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class MasterDataSourceConfig {
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    @Primary
    public DataSource setDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager setTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "masterSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

从数据源配置

 @Configuration
@MapperScan(basePackages = "indi.xulala.dao.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
public class SlaveDataSourceConfig {
    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    @Primary
    public DataSource setDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "slaveTransactionManager")
    @Primary
    public DataSourceTransactionManager setTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "slaveSqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "slaveSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

application

 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@MapperScan(basePackages = {"indi.xulala.dao.master","indi.xulala.dao.slave"})
public class SpringBootLearningApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootLearningApplication.class, args);
    }
}

启动日志

 07:48:27.764 [restartedMain] INFO  o.s.j.e.a.AnnotationMBeanExporter - [afterSingletonsInstantiated,434] - Registering beans for JMX exposure on startup
07:48:27.765 [restartedMain] INFO  o.s.j.e.a.AnnotationMBeanExporter - [autodetect,896] - Bean with name 'masterDataSource' has been autodetected for JMX exposure
07:48:27.765 [restartedMain] INFO  o.s.j.e.a.AnnotationMBeanExporter - [autodetect,896] - Bean with name 'slaveDataSource' has been autodetected for JMX exposure
07:48:27.765 [restartedMain] INFO  o.s.j.e.a.AnnotationMBeanExporter - [autodetect,896] - Bean with name 'statFilter' has been autodetected for JMX exposure
07:48:27.772 [restartedMain] INFO  o.s.j.e.a.AnnotationMBeanExporter - [registerBeanInstance,669] - Located MBean 'masterDataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=masterDataSource,type=HikariDataSource]
07:48:27.773 [restartedMain] INFO  o.s.j.e.a.AnnotationMBeanExporter - [registerBeanInstance,669] - Located MBean 'slaveDataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=slaveDataSource,type=HikariDataSource]
07:48:27.774 [restartedMain] INFO  o.s.j.e.a.AnnotationMBeanExporter - [registerBeanInstance,669] - Located MBean 'statFilter': registering with JMX server as MBean [com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter]

测试日志

 07:49:20.159 [main] INFO  com.zaxxer.hikari.HikariDataSource - [getConnection,110] - HikariPool-1 - Starting...
07:49:20.694 [main] INFO  com.zaxxer.hikari.HikariDataSource - [getConnection,123] - HikariPool-1 - Start completed.
07:49:20.742 [main] INFO  com.zaxxer.hikari.HikariDataSource - [getConnection,110] - HikariPool-2 - Starting...
07:49:20.771 [main] INFO  com.zaxxer.hikari.HikariDataSource - [getConnection,123] - HikariPool-2 - Start completed.
07:49:20.813 [Thread-3] INFO  o.s.w.c.s.GenericWebApplicationContext - [doClose,989] - Closing org.springframework.web.context.support.GenericWebApplicationContext@38831718: startup date [Sat Mar 24 07:49:11 CST 2018]; root of context hierarchy
07:49:20.817 [Thread-3] INFO  com.zaxxer.hikari.HikariDataSource - [close,381] - HikariPool-2 - Shutdown initiated...
07:49:20.833 [Thread-3] INFO  com.zaxxer.hikari.HikariDataSource - [close,383] - HikariPool-2 - Shutdown completed.
07:49:20.834 [Thread-3] INFO  com.zaxxer.hikari.HikariDataSource - [close,381] - HikariPool-1 - Shutdown initiated...
07:49:20.845 [Thread-3] INFO  com.zaxxer.hikari.HikariDataSource - [close,383] - HikariPool-1 - Shutdown completed.

druid-spring-boot-starter版本1.1.9

3个回答

图片说明
解决了,应该用DruidDataSourceBuilder而不是DataSourceBuilder

chanzhuganggang
chanzhuganggang thank you 折腾了好久
大约一年之前 回复

近来,通过建立站群来带来流量,提升主站权重已经是站长比较常用的seo优化方式了,而站群服务器就是为这样的网站运营方式提供服务的。而站长们需要怎么选择站群服务器呢?亚洲的香港、韩国,欧洲的俄罗斯,英国站群服务器等,以及美国都是有站群服务器的资源的。时下最火的站群服务器就属香港站群和美国站群了,由于美国IP资源丰富,所以价格相对比较便宜而且可以IP分可以8-16个c段不等,但是美国服务器的速度不及香港,所以如果对速度比较在意的话可以选择香港站群服务器。

香港站群服务器
E3 8G 1T 5+253ip 1499元/月
美国站群服务器
L5630 32G 1T 5+253ip 999元/月
另:可以混C段

香港cn2:2*E5-2450 32G 1T 10M 劲爆特价只需999元
续费同价、限5台
带宽、IP、防护有需要的话可增加哦。

客服QQ:6077467 Tel:18239932512(微信同步)

Mark回去试一下,一直配置不成功

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问