基于mycat实现项目读写分离
pom.xmlapplication.ymlDataSourceContextHolderDataSourceConfigDynamicDataSourceSwitchDataSourceAOP
主要的原理步骤是,springboot项目整合mycat后,通过aop拦截后切换数据源,然后在通过mycat路由到相应的数据库中实现读写分离,如下是一些核心文件及配置。 项目地址:springboot-mysqlcopy
pom.xml
<parent>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-parent
</artifactId
>
<version>2.0.4.RELEASE
</version
>
<relativePath
/> <!-- lookup parent from repository
-->
</parent
>
<dependencies>
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-aop
</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>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
>
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>druid
</artifactId
>
<version>1.0.23</version
>
</dependency
>
</dependencies
>
<build>
<plugins>
<plugin>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-maven
-plugin
</artifactId
>
</plugin
>
</plugins
>
</build
>
application.yml
server
:
port
: 8282
spring
:
datasource
:
###可读数据源
select
:
jdbc
-url
: jdbc
:mysql
://192.168.78.231:8066/TESTDB
driver
-class-name
: com
.mysql
.jdbc
.Driver
username
: user
password
: user
####可写数据源
update
:
jdbc
-url
: jdbc
:mysql
://192.168.78.231:8066/TESTDB
driver
-class-name
: com
.mysql
.jdbc
.Driver
username
: root
password
: root
type
: com
.alibaba
.druid
.pool
.DruidDataSource
DataSourceContextHolder
@Component
@Lazy(false)
public class DataSourceContextHolder {
private static final ThreadLocal
<String> contextHolder
= new ThreadLocal<>();
public static void setDbType(String dbType
) {
contextHolder
.set(dbType
);
}
public static String
getDbType() {
return contextHolder
.get();
}
public static void clearDbType() {
contextHolder
.remove();
}
}
DataSourceConfig
@Configuration
public class DataSourceConfig {
@Bean(name
= "selectDataSource")
@ConfigurationProperties(prefix
= "spring.datasource.select")
public DataSource
dataSource1() {
return DataSourceBuilder
.create().build();
}
@Bean(name
= "updateDataSource")
@ConfigurationProperties(prefix
= "spring.datasource.update")
public DataSource
dataSource2() {
return DataSourceBuilder
.create().build();
}
}
DynamicDataSource
@Component
@Primary
public class DynamicDataSource extends AbstractRoutingDataSource {
@Autowired
@Qualifier("selectDataSource")
private DataSource selectDataSource
;
@Autowired
@Qualifier("updateDataSource")
private DataSource updateDataSource
;
@Override
protected Object
determineCurrentLookupKey() {
System
.out
.println("DataSourceContextHolder:::" + DataSourceContextHolder
.getDbType());
return DataSourceContextHolder
.getDbType();
}
@Override
public void afterPropertiesSet() {
Map
<Object, Object> map
= new HashMap<>();
map
.put("selectDataSource", selectDataSource
);
map
.put("updateDataSource", updateDataSource
);
setTargetDataSources(map
);
setDefaultTargetDataSource(updateDataSource
);
super.afterPropertiesSet();
}
}
SwitchDataSourceAOP
@Aspect
@Component
@Lazy(false)
@Order(0)
public class SwitchDataSourceAOP {
@Before("execution(* com.mayikt.service.*.*(..))")
public void process(JoinPoint joinPoint
) {
String methodName
= joinPoint
.getSignature().getName();
if (methodName
.startsWith("get") || methodName
.startsWith("count") || methodName
.startsWith("find")
|| methodName
.startsWith("list") || methodName
.startsWith("select") || methodName
.startsWith("check")) {
DataSourceContextHolder
.setDbType("selectDataSource");
} else {
DataSourceContextHolder
.setDbType("updateDataSource");
}
}
}
以上就是项目整合mycat后实现对数据库的读写分离,自我总结学习并分享给大家! 项目地址:springboot-mysqlcopy 学习参考:蚂蚁课堂