com.alibaba
druid-spring-boot-starter
1.1.10
# 实体类包路径
mybatis.type-aliases-package=com.neo.model
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnico
de=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 初始化⼤⼩、最⼩、最⼤连接数
spring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-active=10
# 配置获取连接等待超时的时间
spring.datasource.druid.max-wait=60000
# 监控后台账号和密码
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
# 配置 StatFilter
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
⾸先我们需要配置两个不同的数据源:
spring.datasource.druid.one.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.one.url = jdbc:mysql://localhost:3306/test1?serverTimezone
=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.druid.one.username = root
spring.datasource.druid.one.password = root
spring.datasource.druid.two.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.two.url = jdbc:mysql://localhost:3306/test2?serverTimezone
=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.druid.two.username = root
spring.datasource.druid.two.password = root
# StatViewServlet 配置
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
# 配置 StatFilter
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
# Druid 数据源 1 配置
spring.datasource.druid.one.initial-size=3
spring.datasource.druid.one.min-idle=3
spring.datasource.druid.one.max-active=10
spring.datasource.druid.one.max-wait=60000
# Druid 数据源 2 配置
spring.datasource.druid.two.initial-size=6
spring.datasource.druid.two.min-idle=6
spring.datasource.druid.two.max-active=20
spring.datasource.druid.two.max-wait=120000
@Configuration
public class MultiDataSourceConfig {
@Primary
@Bean(name = "oneDataSource")
@ConfigurationProperties("spring.datasource.druid.one")
public DataSource dataSourceOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "twoDataSource")
@ConfigurationProperties("spring.datasource.druid.two")
public DataSource dataSourceTwo(){
return DruidDataSourceBuilder.create().build();
}
}
Keyword |
value |
解释 |
连接地址 |
jdbc:mysql://localhost:3306/test1? useUnicode=true&characterEncoding=utf-8 |
JDBC 连接字符串串 |
初始化连接⼤大⼩小 |
3 |
连接池建⽴立时创建的初始化连接数 |
最⼩小空闲连接数 |
3 |
连接池中最⼩小的活跃连接数 |
最⼤大连接数 |
10 |
连接池中最⼤大的活跃连接数 |
MaxWait |
10000 |
配置获取连接等待超时的时间 |
Keyword |
value |
解释 |
连接地址 |
jdbc:mysql://localhost:3306/test2? useUnicode=true&characterEncoding=utf-8 |
JDBC 连接字符串串 |
初始化连接⼤大⼩小 |
6 |
连接池建⽴立时创建的初始化连接数 |
最⼩小空闲连接数 |
6 |
连接池中最⼩小的活跃连接数 |
最⼤大连接数 |
20 |
连接池中最⼤大的活跃连接数 |
MaxWait |
120000 |
配置获取连接等待超时的时间 |
org.springframework.boot
spring-boot-starter-web
com.alibaba
druid-spring-boot-starter
1.1.10
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/getUsers")
public List getUsers() {
List users=userRepository.findAll();
return users;
}
}
# 初始化⼤⼩、最⼩、最⼤链接数
spring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-active=10
# 配置获取连接等待超时的时间
spring.datasource.druid.max-wait=60000
# StatViewServlet 配置
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
# 配置 StatFilter
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
com.alibaba
druid
1.1.10
log4j
log4j
1.2.17
数据库1的配置,以 spring.datasource.druid.one 开头:
spring.datasource.druid.one.url=jdbc:mysql://localhost:3306/test1?serverTimezone=U
TC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.druid.one.username=root
spring.datasource.druid.one.password=root
spring.datasource.druid.one.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.one.initialSize=3
spring.datasource.druid.one.minIdle=3
spring.datasource.druid.one.maxActive=10
配置获取连接等待超时的时间
spring.datasource.druid.maxWait=60000
#配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
#配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
spring.datasource.druid.minEvictableIdleTimeMillis=600000
spring.datasource.druid.maxEvictableIdleTimeMillis=900000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
#y检测连接是否有效
spring.datasource.druid.testWhileIdle=true
#是否在从池中取出连接前进⾏检验连接池的可⽤性
spring.datasource.druid.testOnBorrow=false
#是否在归还到池中前进⾏检验连接池的可⽤性
spring.datasource.druid.testOnReturn=false
# 是否打开 PSCache,
spring.datasource.druid.poolPreparedStatements=true
#并且指定每个连接上 PSCache 的⼤⼩
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
#配置监控统计拦截的 filters
spring.datasource.druid.filters=stat,wall,log4j
#通过 connectProperties 属性来打开 mergeSQL 功能,慢 SQL 记录
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.s
lowSqlMillis=600
我们定义⼀个 DruidConfifig 来加载所有的公共配置项,如下:
@Component
@ConfigurationProperties(prefix="spring.datasource.druid")
public class DruidConfig {
protected String url;
protected String username;
protected String password;
protected String driverClassName;
protected int initialSize;
protected int minIdle;
protected int maxActive;
protected int maxWait;
protected int timeBetweenEvictionRunsMillis;
protected long minEvictableIdleTimeMillis;
protected long maxEvictableIdleTimeMillis;
protected String validationQuery;
protected boolean testWhileIdle;
protected boolean testOnBorrow;
protected boolean testOnReturn;
protected boolean poolPreparedStatements;
protected int maxPoolPreparedStatementPerConnectionSize;
protected String filters;
protected String connectionProperties;
// 省略 getter setter
}
@Component
@ConfigurationProperties(prefix="spring.datasource.druid.one")
public class DruidOneConfig extends DruidConfig{
private String url;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxActive;
// 省略 getter setter
}
创建类 DruidDBConfifig 在启动的时候注⼊配置的多数据源信息。
@Configuration
public class DruidDBConfig {
@Autowired
private DruidConfig druidOneConfig;
@Autowired
private DruidConfig druidTwoConfig;
@Autowired
private DruidConfig druidConfig;
}
private DruidDataSource initDruidDataSource(DruidConfig config) {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(config.getUrl());
datasource.setUsername(config.getUsername());
datasource.setPassword(config.getPassword());
datasource.setDriverClassName(config.getDriverClassName());
datasource.setInitialSize(config.getInitialSize());
datasource.setMinIdle(config.getMinIdle());
datasource.setMaxActive(config.getMaxActive());
// common config
datasource.setMaxWait(druidConfig.getMaxWait());
datasource.setTimeBetweenEvictionRunsMillis(druidConfig.getTimeBetweenEviction
RunsMillis());
datasource.setMinEvictableIdleTimeMillis(druidConfig.getMinEvictableIdleTimeMi
llis());
datasource.setMaxEvictableIdleTimeMillis(druidConfig.getMaxEvictableIdleTimeMi
llis());
datasource.setValidationQuery(druidConfig.getValidationQuery());
datasource.setTestWhileIdle(druidConfig.isTestWhileIdle());
datasource.setTestOnBorrow(druidConfig.isTestOnBorrow());
datasource.setTestOnReturn(druidConfig.isTestOnReturn());
datasource.setPoolPreparedStatements(druidConfig.isPoolPreparedStatements());
datasource.setMaxPoolPreparedStatementPerConnectionSize(druidConfig.getMaxPool
PreparedStatementPerConnectionSize());
try {
datasource.setFilters(druidConfig.getFilters());
} catch (SQLException e) {
logger.error("druid configuration initialization filter : {0}", e);
}
datasource.setConnectionProperties(druidConfig.getConnectionProperties());
return datasource;
}
@Bean(name = "primaryDataSource")
public DataSource dataSource() {
return initDruidDataSource(druidOneConfig);
}
@Bean(name = "secondaryDataSource")
@Primary
public DataSource secondaryDataSource() {
return initDruidDataSource(druidTwoConfig);
}
@Configuration
public class DruidConfiguration {
@Bean
public ServletRegistrationBean druidStatViewServlet() {
ServletRegistrationBean servletRegistrationBean = new Ser
vletRegistrationBean<>(new StatViewServlet(), "/druid/*");
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "admin");
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterR
egistrationBean<>(new WebStatFilter());
filterRegistrationBean.setName("DruidWebStatFilter");
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.
png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}