栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot自定义注解使用读写分离Mysql数据库的实例教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot自定义注解使用读写分离Mysql数据库的实例教程

需求场景

为了防止代码中有的SQL慢查询,影响我们线上主数据库的性能。我们需要将sql查询操作切换到从库中进行。为了使用方便,将自定义注解的形式使用。

mysql导入的依赖

		
   mysql
   mysql-connector-java
   8.0.16
  

代码实现

配置文件

application.yml

spring:
 datasource:
 master:
  username: root
  password: 123456
  jdbc-url: jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
  driver-class-name: com.mysql.cj.jdbc.Driver
 slave:
  username: root
  password: 123456
  jdbc-url: jdbc:mysql://localhost:3306/slave?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
  driver-class-name: com.mysql.cj.jdbc.Driver

DataSourceType


public enum DataSourceType {
 
 MASTER,
 
 SLAVE
}

DynamicDataSource


public class DynamicDataSource extends AbstractRoutingDataSource {

 public DynamicDataSource(DataSource defaultTargetDataSource,
  Map targetDataSources) {
  super.setDefaultTargetDataSource(defaultTargetDataSource);
  super.setTargetDataSources(targetDataSources);
  super.afterPropertiesSet();
 }

 @Override
 protected Object determineCurrentLookupKey() {
  return DynamicDataSourceContextHolder.getDateSourceType();
 }
}

DynamicDataSourceContextHolder

public class DynamicDataSourceContextHolder {

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

 
 public static void setDateSourceType(String dsType) {
  CONTEXT_HOLDER.set(dsType);
 }

 
 public static String getDateSourceType() {
  return CONTEXT_HOLDER.get();
 }

 
 public static void clearDateSourceType() {
  CONTEXT_HOLDER.remove();
 }
}

DataSource 注解


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
 
 DataSourceType value() default DataSourceType.MASTER;

}

DataSourceAspect 切面

@Aspect
@Order(1)
@Component
public class DataSourceAspect {

 @Pointcut("@annotation(com.jgame.mis.annotation.DataSource)")
 public void dsPointCut() {

 }

 @Around("dsPointCut()")
 public Object around(ProceedingJoinPoint point) throws Throwable {
  MethodSignature signature = (MethodSignature) point.getSignature();

  Method method = signature.getMethod();

  DataSource dataSource = method.getAnnotation(DataSource.class);

  if (null!=dataSource) {
   DynamicDataSourceContextHolder.
   setDateSourceType(dataSource.value().name());
  }

  try {
   return point.proceed();
  } finally {
   // 销毁数据源 在执行方法之后
   DynamicDataSourceContextHolder.clearDateSourceType();
  }
 }
}

DataSourceConfig

@Configuration
public class DataSourceConfig {

 @Bean
 @ConfigurationProperties("spring.datasource.master")
 public DataSource masterDataSource() {
  return DataSourceBuilder.create().build();
 }

 @Bean
 @ConfigurationProperties("spring.datasource.slave")
 public DataSource slaveDataSource() {
  return DataSourceBuilder.create().build();
 }

 @Bean(name = "dynamicDataSource")
 @Primary
 public DynamicDataSource dataSource()
 {
  Map targetDataSources = new HashMap<>();
  targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource());
  targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource());
  return new DynamicDataSource(masterDataSource(), targetDataSources);
 }
}

启动类上添加注解

@import({DataSourceConfig.class})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

需要使用的方法上

@DataSource(value = DataSourceType.SLAVE)

DEMO

	@Select("select * from user")
  @DataSource(value = DataSourceType.SLAVE)
  List selectUserList();

总结

到此这篇关于SpringBoot自定义注解使用读写分离Mysql数据库的文章就介绍到这了,更多相关SpringBoot自定义注解使用读写分离Mysql内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130682.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号