springboot druid 多主多从配置
时间: 2023-10-29 20:12:00 浏览: 95
springboot多数据源配置
在使用Spring Boot和Druid实现多主多从配置时,我们需要配置多个数据源,并使用DynamicDataSource注解来指定当前需要使用的数据源。
以下是一个示例配置:
1. 配置主数据源
```java
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.master", sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class MasterDataSourceConfig {
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DruidDataSource dataSource() {
return new DruidDataSource();
}
@Bean(name = "masterTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
}
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
2. 配置从数据源
```java
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
public class SlaveDataSourceConfig {
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DruidDataSource dataSource() {
return new DruidDataSource();
}
@Bean(name = "slaveTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
3. 配置动态数据源
```java
@Configuration
public class DynamicDataSourceConfig {
@Bean
public DataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
@Qualifier("slaveDataSource") DataSource slaveDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER, masterDataSource);
targetDataSources.put(DataSourceType.SLAVE, slaveDataSource);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(masterDataSource);
return dataSource;
}
}
```
4. 配置动态数据源注解
```java
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DynamicDataSource {
DataSourceType value() default DataSourceType.MASTER;
}
```
5. 实现动态数据源切换
```java
@Aspect
@Component
public class DynamicDataSourceAspect {
@Pointcut("@annotation(com.example.demo.config.DynamicDataSource)")
public void dataSourcePointCut() {
}
@Around("dataSourcePointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
DynamicDataSource dataSource = signature.getMethod().getDeclaredAnnotation(DynamicDataSource.class);
if (dataSource != null) {
DataSourceType dataSourceType = dataSource.value();
if (dataSourceType == DataSourceType.MASTER) {
DataSourceContextHolder.set(DataSourceType.MASTER);
} else {
DataSourceContextHolder.set(DataSourceType.SLAVE);
}
}
try {
return point.proceed();
} finally {
DataSourceContextHolder.clear();
}
}
}
```
最后,我们就可以在需要切换数据源的方法上添加DynamicDataSource注解,来实现多主多从配置了。例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@DynamicDataSource(DataSourceType.SLAVE)
public List<User> listUsers() {
return userMapper.selectAll();
}
@Override
@Transactional
@DynamicDataSource(DataSourceType.MASTER)
public void saveUser(User user) {
userMapper.insert(user);
}
}
```
阅读全文