读写分离样例

This commit is contained in:
fxb 2018-09-08 22:22:05 +08:00
parent dc79677f97
commit e0454693e4
17 changed files with 608 additions and 0 deletions

73
dxfl/pom.xml Normal file
View File

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>dxfl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dxfl</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,14 @@
package com.example.dxfl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement(order = 10)
public class DxflApplication {
public static void main(String[] args) {
SpringApplication.run(DxflApplication.class, args);
}
}

View File

@ -0,0 +1,37 @@
package com.example.dxfl.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Description 这里切换读/写模式
* 原理是利用ThreadLocal保存当前线程是否处于读模式通过开始READ_ONLY注解在开始操作前设置模式为读模式
* 操作结束后清除该数据避免内存泄漏同时也为了后续在该线程进行写操作时任然为读模式
* @author fxb
* @date 2018-08-31
*/
public class DbContextHolder {
private static Logger log = LoggerFactory.getLogger(DbContextHolder.class);
public static final String WRITE = "write";
public static final String READ = "read";
private static ThreadLocal<String> contextHolder= new ThreadLocal<>();
public static void setDbType(String dbType) {
if (dbType == null) {
log.error("dbType为空");
throw new NullPointerException();
}
log.info("设置dbType为{}",dbType);
contextHolder.set(dbType);
}
public static String getDbType() {
return contextHolder.get() == null ? WRITE : contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}

View File

@ -0,0 +1,34 @@
package com.example.dxfl.config;
import com.example.dxfl.util.NumberUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* Description
*
* @author fxb
* @date 2018-09-03
*/
public class MyAbstractRoutingDataSource extends AbstractRoutingDataSource {
@Value("${mysql.datasource.num}")
private int num;
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Override
protected Object determineCurrentLookupKey() {
String typeKey = DbContextHolder.getDbType();
if (typeKey == DbContextHolder.WRITE) {
log.info("使用了写库");
return typeKey;
}
//使用随机数决定使用哪个读库
int sum = NumberUtil.getRandom(1, num);
log.info("使用了读库{}", sum);
return DbContextHolder.READ + sum;
}
}

View File

@ -0,0 +1,20 @@
package com.example.dxfl.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Description 通过该接口注释的service使用读模式其他使用写模式
*
* 接口注释只是一种办法如果项目已经有代码了通过注释可以不修改任何业务代码加持读写分离
* 也可以通过切面根据方法开头来设置读写模式例如getXXX()使用读模式其他使用写模式
*
* @author fxb
* @date 2018-08-31
*/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReadOnly {
}

View File

@ -0,0 +1,37 @@
package com.example.dxfl.config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* Description
*
* @author fxb
* @date 2018-08-31
*/
@Aspect
@Component
public class ReadOnlyInterceptor implements Ordered {
private static final Logger log= LoggerFactory.getLogger(ReadOnlyInterceptor.class);
@Around("@annotation(readOnly)")
public Object setRead(ProceedingJoinPoint joinPoint,ReadOnly readOnly) throws Throwable{
try{
DbContextHolder.setDbType(DbContextHolder.READ);
return joinPoint.proceed();
}finally {
DbContextHolder.clearDbType();
log.info("清除threadLocal");
}
}
@Override
public int getOrder() {
return 0;
}
}

View File

@ -0,0 +1,103 @@
package com.example.dxfl.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* Description
*
* @author fxb
* @date 2018-09-03
*/
@Configuration
@MapperScan(basePackages = "com.example.dxfl.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class WriteOrReadDatabaseConfig {
@Value("${mysql.datasource.type-aliases-package}")
private String typeAliasesPackage;
@Value("${mysql.datasource.mapper-locations}")
private String mapperLocation;
@Value("${mysql.datasource.config-location}")
private String configLocation;
/**
* 写数据源
*
* @Primary 标志这个 Bean 如果在多个同类 Bean 候选时 Bean 优先被考虑
* 多数据源配置的时候注意必须要有一个主数据源 @Primary 标志该 Bean
*/
@Primary
@Bean
@ConfigurationProperties(prefix = "mysql.datasource.write")
public DataSource writeDataSource() {
return new DruidDataSource();
}
/**
* 读数据源
*/
@Bean
@ConfigurationProperties(prefix = "mysql.datasource.read")
public DataSource read1() {
return new DruidDataSource();
}
/**
* 多数据源需要自己设置sqlSessionFactory
*/
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(routingDataSource());
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 实体类对应的位置
bean.setTypeAliasesPackage(typeAliasesPackage);
// mybatis的XML的配置
bean.setMapperLocations(resolver.getResources(mapperLocation));
bean.setConfigLocation(resolver.getResource(configLocation));
return bean.getObject();
}
/**
* 设置事务事务需要知道当前使用的是哪个数据源才能进行事务处理
*/
@Bean
public DataSourceTransactionManager dataSourceTransactionManager() {
return new DataSourceTransactionManager(routingDataSource());
}
/**
* 设置数据源路由通过该类中的determineCurrentLookupKey决定使用哪个数据源
*/
@Bean
public AbstractRoutingDataSource routingDataSource() {
MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource();
Map<Object, Object> targetDataSources = new HashMap<>(2);
targetDataSources.put(DbContextHolder.WRITE, writeDataSource());
targetDataSources.put(DbContextHolder.READ+"1", read1());
proxy.setDefaultTargetDataSource(writeDataSource());
proxy.setTargetDataSources(targetDataSources);
return proxy;
}
}

View File

@ -0,0 +1,44 @@
package com.example.dxfl.controller;
import com.example.dxfl.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Description
*
* @author fxb
* @date 2018-09-04
*/
@RestController
public class TestController {
private final TestService testService;
@Autowired
public TestController(TestService testService) {
this.testService = testService;
}
@GetMapping("/getAll")
public Object getAll(){
return testService.getAll();
}
@GetMapping("insertOne")
public void insertOne(){
testService.insertOne();
}
@GetMapping("readAndWrite")
public void readAndWrite(){
testService.testReadAndWrite();
}
@GetMapping("transInsert")
public void transInsert(){
testService.transInsert();
}
}

View File

@ -0,0 +1,28 @@
package com.example.dxfl.entity;
/**
* Description
*
* @author fxb
* @date 2018-09-04
*/
public class Test {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,26 @@
package com.example.dxfl.mapper;
import com.example.dxfl.entity.Test;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Description
*
* @author fxb
* @date 2018-09-04
*/
public interface TestMapper {
/**
* 获取全部数据
* @return List<Test>
*/
List<Test> getAll();
/**
* 插入一条数据
* @param test 数据
*/
void insertOne(@Param("test") Test test);
}

View File

@ -0,0 +1,63 @@
package com.example.dxfl.service;
import com.example.dxfl.config.ReadOnly;
import com.example.dxfl.entity.Test;
import com.example.dxfl.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Description
*
* @author fxb
* @date 2018-09-04
*/
@Service
public class TestService {
private final TestMapper testMapper;
@Autowired
public TestService(TestMapper testMapper) {
this.testMapper = testMapper;
}
/**
* 测试读取应该使用读库
*/
@ReadOnly
public List<Test> getAll() {
return testMapper.getAll();
}
/**
* 测试读取和插入,应该使用写库
*/
public void testReadAndWrite() {
this.getAll();
this.insertOne();
}
/**
* 测试插入数据,应该使用写库
*/
public void insertOne() {
Test test = new Test();
test.setName("fxb");
testMapper.insertOne(test);
}
/**
* 测试事务能否正常工作
*/
@Transactional(rollbackFor = RuntimeException.class)
public void transInsert(){
Test test = new Test();
test.setName("heiheihei");
testMapper.insertOne(test);
throw new RuntimeException("测试事务");
}
}

View File

@ -0,0 +1,14 @@
package com.example.dxfl.util;
/**
* Description
*
* @author fxb
* @date 2018-09-07
*/
public class NumberUtil {
public static int getRandom(int min, int max) {
return (int) Math.floor(Math.random() * (max - min + 1)) + min;
}
}

View File

@ -0,0 +1,17 @@
mysql:
datasource:
#读库数目
num: 1
type-aliases-package: com.example.dxfl.dao
mapper-locations: classpath:/mapper/*.xml
config-location: classpath:/mybatis-config.xml
write:
url: jdbc:mysql://192.168.226.5:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
read:
url: jdbc:mysql://192.168.226.6:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.dxfl.mapper.TestMapper">
<select id="getAll" resultType="com.example.dxfl.entity.Test">
select * from test
</select>
<insert id="insertOne" parameterType="com.example.dxfl.entity.Test">
insert into test value (#{test.id},#{test.name})
</insert>
</mapper>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 使全局的映射器启用或禁用缓存。 -->
<setting name="cacheEnabled" value="true" />
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
<setting name="aggressiveLazyLoading" value="true"/>
<!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
<setting name="useColumnLabel" value="true" />
<!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true这个设置将强制使用被生成的主键有一些驱动器不兼容不过仍然可以执行。 default:false -->
<setting name="useGeneratedKeys" value="false" />
<!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE不隐射 PARTIAL:部分 FULL:全部 -->
<setting name="autoMappingBehavior" value="PARTIAL" />
<!-- 这是默认的执行类型 SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句BATCH: 执行器可以重复执行语句和批量更新) -->
<setting name="defaultExecutorType" value="SIMPLE" />
<setting name="defaultStatementTimeout" value="25" />
<setting name="defaultFetchSize" value="100" />
<setting name="safeRowBoundsEnabled" value="false" />
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) default:session -->
<setting name="localCacheScope" value="SESSION" />
<!-- 默认为OTHER,为了解决oracle插入null报错的问题要设置为NULL -->
<setting name="jdbcTypeForNull" value="NULL" />
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
</settings>
</configuration>

View File

@ -0,0 +1,16 @@
package com.example.dxfl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DxflApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@ -0,0 +1,32 @@
package com.example.dxfl;
import com.example.dxfl.service.TestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Description
*
* @author fxb
* @date 2018-09-05
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestServiceTest {
@Autowired
private TestService testService;
@Test
public void insertOne(){
testService.insertOne();
}
@Test
public void getAll(){
testService.getAll();
}
}