springboot结合mysql主从来实现读写分离的方法示例
程序员文章站
2022-06-24 08:14:18
1.实现的功能 基于springboot框架,application.yml配置多个数据源,使用aop以及abstractrootingdatasource、threadlocal来实现多数据源切换,...
1.实现的功能
基于springboot框架,application.yml配置多个数据源,使用aop以及abstractrootingdatasource、threadlocal来实现多数据源切换,以实现读写分离。mysql的主从数据库需要进行设置数据之间的同步。
2.代码实现
application.properties中的配置
spring.datasource.druid.master.driver-class-name=com.mysql.jdbc.driver spring.datasource.druid.master.url=jdbc:mysql://127.0.0.1:3306/node2?useunicode=true&characterencoding=utf-8&allowmultiqueries=true&autoreconnect=true&usessl=false spring.datasource.druid.master.username=root spring.datasource.druid.master.password=123456 spring.datasource.druid.slave.driver-class-name=com.mysql.jdbc.driver spring.datasource.druid.slave.url=jdbc:mysql://127.0.0.1:3306/node1?useunicode=true&characterencoding=utf-8&allowmultiqueries=true&autoreconnect=true&usessl=false spring.datasource.druid.slave.username=root spring.datasource.druid.slave.password=123456
写一个datasourceconfig.java来注入两个bean
@bean @configurationproperties("spring.datasource.druid.master") public datasource masterdatasource() { logger.info("select master data source"); return druiddatasourcebuilder.create().build(); } @bean @configurationproperties("spring.datasource.druid.slave") public datasource slavedatasource() { logger.info("select slave data source"); return druiddatasourcebuilder.create().build(); }
写一个enum来标识有哪些数据源
public enum dbtypeenum { master, slave; }
然后写一个threadlocal本地线程的管理类,用于设置当前线程是那一个数据源
private static final threadlocal<dbtypeenum> contextholder = new threadlocal<>(); private static final threadlocal<dbtypeenum> contextholder2 = threadlocal.withinitial(() -> dbtypeenum.master); public static void set(dbtypeenum dbtype) { contextholder.set(dbtype); } public static dbtypeenum get() { return contextholder.get(); } public static void master() { set(dbtypeenum.master); logger.info("切换到master数据源"); } public static void slave() { set(dbtypeenum.slave); logger.info("切换到slave数据源"); } public static void cleanall() { contextholder.remove(); }
然后写一个dynamicdatasource继承abstractrootingdatasource,重写它的determinecurrentlookupkey方法。
public class dynamicdatasource extends abstractroutingdatasource { private logger logger = logmanager.getlogger(dynamicdatasource.class); @override protected object determinecurrentlookupkey() { logger.info("此时数据源为{}", dbcontextholder.get()); return dbcontextholder.get(); } }
最后写一个aop来实现数据源切换
@aspect @order(1) @component public class datasourceaop { private logger logger = logmanager.getlogger(datasourceaop.class); @pointcut("(execution(* com.springboot.demo.service..*.select*(..)) " + "|| execution(* com.springboot.demo.service..*.find*(..)) " + "|| execution(* com.springboot.demo.service..*.get*(..)))") public void readpointcut() { logger.info("read only operate ,into slave db"); } @pointcut("execution(* com.springboot.demo.service..*.insert*(..)) " + "|| execution(* com.springboot.demo.service..*.update*(..)) " + "|| execution(* com.springboot.demo.service..*.delete*(..)) ") public void writepointcut() { logger.info("read or write operate ,into master db"); } @before("readpointcut()") public void read() { logger.info("read operate"); dbcontextholder.slave(); } @before("writepointcut()") public void write() { logger.info("write operate"); dbcontextholder.master(); } @after("writepointcut(),readpointcut()") public void clean() { logger.info("datasource cleanall"); dbcontextholder.cleanall(); } }
注意:这里只是使用了偷懒的方法,对于service里面的select、get、find前缀的方法都使用从库,对于insert、update和delete方法都使用主库。
可以使用注解如下来进行优化:
@retention(retentionpolicy.runtime) @target(elementtype.method) public @interface datasource { @aliasfor("datasource") dbtypeenum value() default dbtypeenum.master; dbtypeenum datasource() default dbtypeenum.master; }
使用此注解来放入到service方法上,
@datasource(dbtypeenum.slave)
然后aop方法修改为:
private static final string point = "execution (* com.springboot.demo.service.*.*(..))"; @around(point) public object datasourcearound(proceedingjoinpoint joinpoint) throws throwable { object[] args = joinpoint.getargs(); object obj; object target = joinpoint.gettarget(); string methodname = joinpoint.getsignature().getname(); class clazz = target.getclass(); class<?>[] parametertypes = ((methodsignature) joinpoint.getsignature()).getmethod().getparametertypes(); boolean isdynamicdatasourcemethod = false; try { method method = clazz.getmethod(methodname, parametertypes); datasources currentdatasource = null; if (method != null && method.isannotationpresent(datasource.class)) { isdynamicdatasourcemethod = true; currentdatasource = method.getannotation(datasource.class).value(); datasourcetypemanager.set(currentdatasource); log.info("datasourceinterceptor switch datasource to {}",currentdatasource); } obj = joinpoint.proceed(args); if (isdynamicdatasourcemethod) { log.info("datasourceinterceptor datasource {} proceed",currentdatasource); } } finally { if (isdynamicdatasourcemethod) { datasourcetypemanager.reset(); log.info("datasourceinterceptor reset datasource to {}",datasourcetypemanager.get()); } } return obj; }
到此这篇关于springboot结合mysql主从来实现读写分离的方法示例的文章就介绍到这了,更多相关springboot 读写分离内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Java中的逻辑结构详解