springBoot连接mysql主从配置
时间: 2023-10-26 20:06:21 浏览: 334
1. 在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
2. 在application.properties中添加以下配置:
```
# 主库配置
spring.datasource.master.url=jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.master.username=root
spring.datasource.master.password=root
spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver
# 从库配置
spring.datasource.slave.url=jdbc:mysql://localhost:3306/slave?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.slave.username=root
spring.datasource.slave.password=root
spring.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
# 配置多个从库,用逗号分隔
spring.datasource.slave2.url=jdbc:mysql://localhost:3306/slave2?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.slave2.username=root
spring.datasource.slave2.password=root
spring.datasource.slave2.driver-class-name=com.mysql.jdbc.Driver
# 配置读写分离
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.read-type=READ_ONLY
spring.datasource.master.slaves=slave,slave2
```
3. 在代码中使用@Primary注解来指定默认的数据源:
```
@Configuration
public class DataSourceConfig {
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slave2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave2")
public DataSource slave2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dynamicDataSource")
@Primary // 指定默认的数据源
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put("master", masterDataSource());
dataSourceMap.put("slave", slaveDataSource());
dataSourceMap.put("slave2", slave2DataSource());
dynamicDataSource.setTargetDataSources(dataSourceMap);
return dynamicDataSource;
}
}
```
4. 实现自定义的动态数据源:
```
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSourceType();
}
}
```
5. 创建一个数据源上下文,用于保存当前线程使用的数据源类型:
```
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
public static String getDataSourceType() {
return contextHolder.get();
}
public static void clearDataSourceType() {
contextHolder.remove();
}
}
```
6. 在service层中根据需要调用不同的数据源:
```
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> findAll() {
// 从主库读取数据
DataSourceContextHolder.setDataSourceType("master");
return userDao.findAll();
}
public User findById(Long id) {
// 从从库1读取数据
DataSourceContextHolder.setDataSourceType("slave");
return userDao.findById(id);
}
public void save(User user) {
// 写入主库
DataSourceContextHolder.setDataSourceType("master");
userDao.save(user);
}
public void deleteById(Long id) {
// 写入主库
DataSourceContextHolder.setDataSourceType("master");
userDao.deleteById(id);
}
}
```
阅读全文