调整数据源的配置。之前数据源配置不生效

This commit is contained in:
zhouxiunai
2018-12-24 10:40:12 +08:00
parent cb99392e95
commit 1948c4f194
5 changed files with 221 additions and 41 deletions
@@ -1,28 +0,0 @@
package com.gzzn.omms.adminapi.config;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource()
{
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource()
{
return DataSourceBuilder.create().build();
}
}
@@ -0,0 +1,55 @@
package com.gzzn.omms.adminapi.config;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
@EnableConfigurationProperties(PrimaryDsProperties.class)
@ConditionalOnClass(DataSource.class)
@ConditionalOnProperty(prefix = "spring.datasource.primary", name = "url")
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
public class PrimaryDataSourceConfig {
@Autowired
private PrimaryDsProperties properties;
@Bean(name = "primaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource()
{
DataSource dataSource = new DataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
//dataSource.setPassword(ConfigTools.decrypt(properties.getPassword()));//解密数据库密码
dataSource.setPassword(properties.getPassword());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
dataSource.setDriverClassName(properties.getDriver());
dataSource.setInitialSize(properties.getInitialSize());
dataSource.setMaxWait(properties.getMaxWait());
dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
dataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
return dataSource;
}
}
@@ -0,0 +1,136 @@
package com.gzzn.omms.adminapi.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.datasource.primary")
public class PrimaryDsProperties {
private String url;
private String username;
private String password;
private String driver;
private Integer initialSize;
private Integer minIdle;
private Integer maxPoolPreparedStatementPerConnectionSize;
private Integer maxActive;
private Integer maxWait;
private Integer timeBetweenEvictionRunsMillis;
private Integer minEvictableIdleTimeMillis;
private boolean poolPreparedStatements;
private boolean testOnBorrow;
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getDriver() {
return driver;
}
public Integer getInitialSize() {
return initialSize;
}
public Integer getMinIdle() {
return minIdle;
}
public Integer getMaxPoolPreparedStatementPerConnectionSize() {
return maxPoolPreparedStatementPerConnectionSize;
}
public Integer getMaxActive() {
return maxActive;
}
public Integer getMaxWait() {
return maxWait;
}
public Integer getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public Integer getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public boolean isPoolPreparedStatements() {
return poolPreparedStatements;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setDriver(String driver) {
this.driver = driver;
}
public void setInitialSize(Integer initialSize) {
this.initialSize = initialSize;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) {
this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public void setMaxWait(Integer maxWait) {
this.maxWait = maxWait;
}
public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public void setPoolPreparedStatements(boolean poolPreparedStatements) {
this.poolPreparedStatements = poolPreparedStatements;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}
@@ -0,0 +1,18 @@
package com.gzzn.omms.adminapi.config;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SecondaryDataSourceConfig {
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource()
{
return new DataSource();
}
}
+12 -13
View File
@@ -6,19 +6,18 @@ spring:
username: cdairport
password: 123456
url: jdbc:mysql://130.120.3.158:3306/cdairport?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true
tomcat:
max-active: 30
test-on-borrow: true
validation-query: SELECT 1
initial-size: 10
min-idle: 1
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-return: true
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
max-active: 30
test-on-borrow: true
validation-query: SELECT 1
initialSize: 5
min-idle: 1
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-return: true
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
secondary:
username: ommsxc
password: ommsxc