电商商城定制开发【Springboot】动态配置数据源,系统自动辨认服务端与本地端数据源

电商商城定制开发系统自动辨认服务端与电商商城定制开发本地端数据源


前言

提示:这里可以添加本文要记录的大概内容:

本文中主要讲解,不同数据源,系统如何自动辨认本地端和服务端,并灵活的切换,不需要写拦截器,在项目启动的时候就根据当前系统自动配置


提示:以下是本篇文章正文内容,下面案例可供参考

使用步骤

1.导入

        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>druid-spring-boot-starter</artifactId>            <version>1.1.10</version>        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

5.编写数据源配置

主要使用了 System.getProperty(“os.name”)
在Windows环境下面 会输出 Windows10
在Linux环境下也是服务端 会输出Linux
利用这个机制,我们在不同环境下使用不同的数据源

@Configuration@Componentpublic class DynamicDataSourceConfig {    public static final Map<String,String> systemMap = new HashMap<>();    public Map<String,String> getSystemMap(){        // 如果是Linux端 则使用prod数据源        systemMap.put("linux","prod");        // 如果是Windows端 则使用local数据源        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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

总结

  1. 根据当前不同的系统环境可以区别不同的一个数据源使用,我们可以当Windows环境就是本地环境,而Linux环境就是线上环境,
  2. 通过System.getProperty(“os.name”);来进行区分,我们也可以通过System.getProperty(“os.version”); 获取当前系统版本号来进行区分

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发