springboot mysql多数据库_Springboot中使用Mysql多数据源

2ff34e647e2e3cdfd8dca593e17d9b0a.png

随着业务的发展,很可能需要在一个项目里面同时使用多个数据源。

大致看了网上的多数据源Demo,发现无非有两种:一种是自己封装多个JdbcTemplate,然后调用对应的数据库就使用对应的JdbcTemplate一种是通过注解的方式来实现,在需要切换数据源的方法上添加一个自己封装的注解便可以完成切换。

考虑了一下以后的扩展性和通用性,便决定采用基于注解的多数据源方式

分析

看了下官网的介绍,大致了解了在Spring中使用多数据源的一个关键类是AbstractRoutingDataSource。

先看下这个类的结构

5dde2535444d6.png

在这个类里面,只需要关注一下几个变量或者方法即可。private Map targetDataSources;

private Object defaultTargetDataSource;

protected Object determineCurrentLookupKey()

targetDataSources

这个变量是一个存储数据源的Map,其实一般在使用的时候,更加像是Map这样的,其中key表示的是这个数据源的名称,而value则是表示这个DataSource的信息(例如url,username等)

defaultTargetDataSource

该变量表示的是一个默认的数据源,非空,必须设置

determineCurrentLookupKey

该方法返回的一个targetDataSources里面的键,用于选择某一个数据源。其中多数据源的切换就是控制该方法的返回值来实现。该方法返回的是targetDataSources里面的键,从而HikariPool可以直接切换数据源

思路

首先是通过读取配置文件,将其转为DataSource,然后再将dataSource存入targetDataSources

代码

配置文件1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17spring:

application:

name: multi-resource

datasource:

mysql1:

driver-class-name: com.mysql.cj.jdbc.Driver

jdbc-url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

username: root

password: 123456

mysql2:

driver-class-name: com.mysql.cj.jdbc.Driver

jdbc-url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

username: root

password: 123456

aop:

auto: true

proxy-target-class: true

获取配置文件信息1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

204j

@Configuration

public class{

private static final Logger LOGGER = LoggerFactory.getLogger(MysqlMultiProperties.class);

@Bean("mysql1")

@ConfigurationProperties("spring.datasource.mysql1")

public DataSource mysql1Source(){

log.info("正在初始化Mysql_DB1");

return DataSourceBuilder.create().build();

}

@Bean("mysql2")

@ConfigurationProperties("spring.datasource.mysql2")

public DataSource mysql2Source(){

log.info("正在初始化Mysql_DB2");

return DataSourceBuilder.create().build();

}

}

实现AbstractRoutingDataSource1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

204j

public class DataSourceRouter extends AbstractRoutingDataSource{

private static final ThreadLocal contextHolder = new ThreadLocal<>();

@Override

protected Object determineCurrentLookupKey(){

return getDataSource();

}

public static void setDataSource(String dataSource){

contextHolder.set(dataSource);

}

public static String getDataSource(){

return contextHolder.get();

}

}

实例化Bean1

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@Configuration

public class MysqlConfig{

@Autowired

@Qualifier("mysql1")

DataSource mysql1;

@Autowired

@Qualifier("mysql2")

DataSource mysql2;

@Bean

@Primary

public DataSourceRouter generateRouter(){

DataSourceRouter router =new DataSourceRouter();

Map targetMap = new HashMap();

targetMap.put("mysql1",mysql1);

targetMap.put("mysql2",mysql2);

router.setTargetDataSources(targetMap);

router.setDefaultTargetDataSource(mysql1);

router.afterPropertiesSet();

return router;

}

}

AOP切面

在使用切面的时候遇到了一些坑,这个有空再说

新建一个注解1

2

3

4

5

6@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface UseDataSource {

String name();

}

新建一个切面类1

2

3

4

5

6

7

8

9

10

114j

@Component

@Aspect

@Order(-1)

public class MysqlSourceAspect{

@Before("@annotation(useDataSource)")

public void changeMysqlSource(UseDataSource useDataSource){

DataSourceRouter.setDataSource(useDataSource.name());

}

}

至此大部分功能都已经实现了,在Springboot的启动类上添加或修改如下注解1

2

3

4

5

6

7

8@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

@MapperScan(basePackages = "com.somersames.dao")

@Import({MysqlConfig.class})

public class ServiceApplication{

public static void main(String[] args){

SpringApplication.run(ServiceApplication.class);

}

}

使用

只需要在切换数据源的地方添加@UseDataSource注解即可。

总结

这次在编写AOP部分的时候需要了一点小坑,有空会整理出来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个示例代码,使用Spring Boot和Druid连接MySQL多数据源,并且对数据库密码进行了加解密: 1. 添加依赖 在 pom.xml 文件添加以下依赖: ``` <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Spring Boot Starter JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency> <!-- Jasypt --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> </dependencies> ``` 2. 配置数据源 在 application.yml 文件添加以下配置: ``` # 默认数据源 spring.datasource.url=jdbc:mysql://localhost:3306/db1 spring.datasource.username=root spring.datasource.password=ENC(加密后的密码) spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 第二个数据源 datasource2.url=jdbc:mysql://localhost:3306/db2 datasource2.username=root datasource2.password=ENC(加密后的密码) datasource2.driver-class-name=com.mysql.jdbc.Driver ``` 其,密码字段使用 Jasypt 进行加密,格式为 `ENC(加密后的密码)`。 3. 配置Druid数据源 在配置文件添加以下配置: ``` # Druid数据源配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 配置第一个数据源 spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-active=20 spring.datasource.druid.max-wait=60000 spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.filters=stat,wall,log4j # 配置第二个数据源 datasource2.druid.initial-size=5 datasource2.druid.min-idle=5 datasource2.druid.max-active=20 datasource2.druid.max-wait=60000 datasource2.druid.time-between-eviction-runs-millis=60000 datasource2.druid.min-evictable-idle-time-millis=300000 datasource2.druid.validation-query=SELECT 1 FROM DUAL datasource2.druid.test-while-idle=true datasource2.druid.test-on-borrow=false datasource2.druid.test-on-return=false datasource2.druid.pool-prepared-statements=true datasource2.druid.max-pool-prepared-statement-per-connection-size=20 datasource2.druid.filters=stat,wall,log4j ``` 4. 配置数据源连接池 在配置类添加以下代码: ``` @Configuration public class DataSourceConfig { @Bean(name = "dataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource") public DruidDataSource dataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "dataSource2") @ConfigurationProperties(prefix = "datasource2") public DruidDataSource dataSource2() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "dataSource1TransactionManager") @Primary public DataSourceTransactionManager dataSourceTransactionManager() { return new DataSourceTransactionManager(dataSource()); } @Bean(name = "dataSource2TransactionManager") public DataSourceTransactionManager dataSource2TransactionManager() { return new DataSourceTransactionManager(dataSource2()); } @Bean(name = "dataSource1SqlSessionFactory") @Primary public SqlSessionFactory dataSourceSqlSessionFactory(@Qualifier("dataSource") DruidDataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); return sessionFactory.getObject(); } @Bean(name = "dataSource2SqlSessionFactory") public SqlSessionFactory dataSource2SqlSessionFactory(@Qualifier("dataSource2") DruidDataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*.xml")); return sessionFactory.getObject(); } } ``` 其,`@Primary` 注解表示默认数据源,`@ConfigurationProperties` 注解表示从配置文件读取配置。 5. 配置MyBatis 在 `application.yml` 文件添加以下配置: ``` mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.entity ``` 6. 编写DAO和Mapper 在 `com.example.dao` 包编写DAO和Mapper,例如: ``` @Repository public interface UserDAO { @Select("SELECT * FROM user") @Results(id = "userResultMap", value = { @Result(property = "id", column = "id", id = true), @Result(property = "name", column = "name"), @Result(property = "email", column = "email"), @Result(property = "phone", column = "phone"), @Result(property = "createTime", column = "create_time") }) List<User> list(); } @Mapper public interface UserMapper { @Select("SELECT * FROM user") @ResultMap("userResultMap") List<User> list(); } ``` 7. 使用数据源 在Service使用数据源,例如: ``` @Service public class UserService { @Autowired private UserDAO userDAO; @Autowired private UserMapper userMapper; @Transactional(transactionManager = "dataSource1TransactionManager") public List<User> list() { return userDAO.list(); } @Transactional(transactionManager = "dataSource2TransactionManager") public List<User> list2() { return userMapper.list(); } } ``` 其,`@Transactional` 注解表示开启事务,`transactionManager` 参数指定使用的数据源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值