| New file |
| | |
| | | package com.zy.common.config.ds; |
| | | |
| | | import com.zaxxer.hikari.HikariConfig; |
| | | import com.zaxxer.hikari.HikariDataSource; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.boot.jdbc.DataSourceBuilder; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.context.annotation.Primary; |
| | | |
| | | import javax.sql.DataSource; |
| | | |
| | | |
| | | @Configuration |
| | | public class DataSourceConfig { |
| | | |
| | | @Value("${other.datasource.jdbc-url}") |
| | | private String url; |
| | | @Value("${other.datasource.username}") |
| | | private String username; |
| | | @Value("${other.datasource.password}") |
| | | private String password; |
| | | @Value("${other.datasource.driver-class-name}") |
| | | private String driverClassName; |
| | | |
| | | // 主数据源 |
| | | @Bean(name = "dataSource") |
| | | @Primary |
| | | @ConfigurationProperties(prefix = "spring.datasource") |
| | | public DataSource primaryDataSource() { |
| | | return DataSourceBuilder.create().build(); |
| | | } |
| | | |
| | | // 次数据源 |
| | | @Bean(name = "slaveDataSource") |
| | | public DataSource secondaryDataSource() { |
| | | HikariConfig config = new HikariConfig(); |
| | | config.setJdbcUrl(url); |
| | | config.setUsername(username); |
| | | config.setPassword(password); |
| | | config.setDriverClassName(driverClassName); |
| | | config.setInitializationFailTimeout(0L); |
| | | return new HikariDataSource( config); |
| | | // return DataSourceBuilder.create() |
| | | // .driverClassName(driverClassName) |
| | | // .url(url) |
| | | // .username(username) |
| | | // .password(password) |
| | | // .build(); |
| | | |
| | | } |
| | | |
| | | } |