1. 程式人生 > >Spring Cloud專案中單資料來源改為多資料來源

Spring Cloud專案中單資料來源改為多資料來源

應專案的需求,要把專案改成多資料來源,將兩張表單獨抽取出來,放入另一個數據庫中,由於spring cloud 架構不熟悉,也是花了點時間才做好。

1、禁用srping boot自動配置的單資料來源,
啟動類上加上註解@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)

@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
@EnableDiscoveryClient
@EnableFeignClients
public class
UserServerApplication {
public static void main(String[] args) { SpringApplication.run(UserServerApplication.class, args); } }

2、修改配置檔案,專案中用spring cloud config統一管理配置檔案的
在datasource節點下再加一個新資料來源配置,如下所示

datasource:
  user-server:
    url: jdbc:mysql://***?characterEncoding=utf-8&autoReconnect=true&f***
username: *** password: *** driver-class-name: com.mysql.jdbc.Driver max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 validation-query: SELECT 1 test-on-borrow: false test-while-idle: true time-between-eviction-runs-millis: 18800 user-server-separate: url: jdbc:mysql://***?characterEncoding=utf-8&autoReconnect=true****
username: *** password: *** driver-class-name: com.mysql.jdbc.Driver max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 validation-query: SELECT 1 test-on-borrow: false test-while-idle: true time-between-eviction-runs-millis: 18800

3、分包  兩個資料庫對應兩個實體類的包,xml檔案也一樣,分兩個資料夾,如下圖所示
這裡寫圖片描述
domain 和 domain_separate 存放不同庫的實體類
這裡寫圖片描述
mybatis和separate存放不同庫對應的xml檔案

4、建立新的資料來源
這裡寫圖片描述
UcSeparateDataSourceConfig這是新配置的資料來源,具體內容如下

@Configuration
@MapperScan(basePackages = {"com.baojun.user_server.application.domain_separate"}, sqlSessionFactoryRef = "ucSeparateSqlSessionFactory")
public class UcSeparateDataSourceConfig {
    @Primary
    @Bean(name = "ucSeparateDataSource")
    @ConfigurationProperties(prefix = "datasource.user-server-separate")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "ucSeparateTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("ucSeparateDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "ucSeparateSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("ucSeparateDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:separate/*.xml"));
        return factoryBean.getObject();
    }

    @Bean(name = "ucSeparateSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("ucSeparateSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

這裡要注意包路徑和xml檔案的路徑,
@ConfigurationProperties(prefix = “datasource.user-server-separate”)字首與配置檔案中一致

5、在需要更改資料來源的地方,加入對應的註解
@Resource(name=”ucSeparateSqlSessionTemplate”)這個就是新配置的資料來源

@Repository
public class CarDaoImpl extends BaseDaoImpl<Car> implements CarDao {
    public CarDaoImpl() {
    super(Car.class);
    }
    @Resource(name="ucSeparateSqlSessionTemplate")
    public void sqlSessionTemplate(SqlSessionTemplate sessionTemplate) {
    setSqlSessionTemplate(sessionTemplate);
    }
}