diff --git a/src/main/java/com/gzzn/omms/adminapi/config/SecondaryDataSourceConfig.java b/src/main/java/com/gzzn/omms/adminapi/config/SecondaryDataSourceConfig.java index 740f220..2959581 100644 --- a/src/main/java/com/gzzn/omms/adminapi/config/SecondaryDataSourceConfig.java +++ b/src/main/java/com/gzzn/omms/adminapi/config/SecondaryDataSourceConfig.java @@ -2,17 +2,51 @@ 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; @Configuration +@EnableConfigurationProperties(SecondaryDsProperties.class) +@ConditionalOnClass(DataSource.class) +@ConditionalOnProperty(prefix = "spring.datasource.secondary", name = "url") +@AutoConfigureBefore(DataSourceAutoConfiguration.class) public class SecondaryDataSourceConfig { + @Autowired + private SecondaryDsProperties properties; + @Bean(name = "secondaryDataSource") @ConfigurationProperties(prefix="spring.datasource.secondary") public DataSource secondaryDataSource() { - return new DataSource(); + 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; } } diff --git a/src/main/java/com/gzzn/omms/adminapi/config/SecondaryDsProperties.java b/src/main/java/com/gzzn/omms/adminapi/config/SecondaryDsProperties.java new file mode 100644 index 0000000..faa1396 --- /dev/null +++ b/src/main/java/com/gzzn/omms/adminapi/config/SecondaryDsProperties.java @@ -0,0 +1,136 @@ +package com.gzzn.omms.adminapi.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "spring.datasource.secondary") +public class SecondaryDsProperties { + 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; + } +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 84c0d31..3bde29e 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -24,19 +24,18 @@ spring: #url: jdbc:oracle:thin:@130.120.3.236:1521/xe url: jdbc:oracle:thin:@130.120.2.105:1521/orcl driver: oracle.jdbc.driver.OracleDriver - 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 + 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 sysApi: host: 130.120.3.231 port: 93