前言
提示:这里可以添加本文要记录的大概内容:
本文中主要讲解,不同数据源,系统如何自动辨认本地端和服务端,并灵活的切换,不需要写拦截器,在项目启动的时候就根据当前系统自动配置
提示:以下是本篇文章正文内容,下面案例可供参考
使用步骤
1.导入maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
2.启动类配置
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@Import({DynamicDataSourceConfig.class})
@MapperScan(basePackages = "com.view.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.yml配置文件
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
local:
url: jdbc:mysql://localhost:3306/localDB?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username: xxxx1
password: xxxx2
driverClassName: com.mysql.cj.jdbc.Driver
prod:
url: jdbc:mysql://localhost:3306/serverDB?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username: xxxx1
password: xxxx2
driverClassName: com.mysql.cj.jdbc.Driver
4.继承AbstractRoutingDataSource
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
super.setDefaultTargetDataSource(defaultTargetDataSource);
super.setTargetDataSources(targetDataSources);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
return getDataSource();
}
public static String getDataSource() {
return contextHolder.get();
}
}
5.编写数据源配置
主要使用了 System.getProperty(“os.name”) 在Windows环境下面 会输出 Windows10 在Linux环境下也是服务端 会输出Linux 利用这个机制,我们在不同环境下使用不同的数据源
@Configuration
@Component
public class DynamicDataSourceConfig {
public static final Map<String,String> systemMap = new HashMap<>();
public Map<String,String> getSystemMap(){
systemMap.put("linux","prod");
systemMap.put("windows","local");
return systemMap;
}
public DataSource getDataSource(Map<Object, Object> targetDataSources,DataSource localDataSource, DataSource prodDataSource){
String sysName = System.getProperty("os.name").toLowerCase();
for (Map.Entry<String, String> entry : getSystemMap().entrySet()) {
if (sysName.contains(entry.getKey().toLowerCase())){
switch (entry.getKey()){
case "linux":
targetDataSources.put(entry.getValue(),prodDataSource);
return prodDataSource;
case "windows":
targetDataSources.put(entry.getValue(),localDataSource);
return localDataSource;
}
}
}
return null;
}
@Bean
@ConfigurationProperties("spring.datasource.druid.local")
public DataSource localDataSource(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.prod")
public DataSource prodDataSource(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DynamicDataSource dataSource(DataSource localDataSource, DataSource prodDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
DataSource dataSource = getDataSource(targetDataSources, localDataSource, prodDataSource);
if (dataSource != null) {
return new DynamicDataSource(dataSource, targetDataSources);
}
return new DynamicDataSource(localDataSource, targetDataSources);
}
}
总结
- 根据当前不同的系统环境可以区别不同的一个数据源使用,我们可以当Windows环境就是本地环境,而Linux环境就是线上环境,
- 通过System.getProperty(“os.name”);来进行区分,我们也可以通过System.getProperty(“os.version”); 获取当前系统版本号来进行区分
|