最近在工作中遇到了需要垂直拆分数据库的场景,因此系统总结一下有关数据库拆分的有关方案。
1.使用dynamic-datasource插件来实现拆分
2.使用ShardingSphere JDBC来实现拆分
Sharding-JDBC 实战(史上最全)_40岁资深老架构师尼恩的博客-CSDN博客_sharding-jdbc
Sharding-JDBC(二)SpringBoot整合Sharding-JDBC实现分库分表_一恍过去的博客-CSDN博客
3.使用动态数据源+AOP注解来自定义实现
5、通过AOP+自定义注解切换多数据源_哔哩哔哩_bilibili
关于数据库的垂直拆分,就是将不同的业务表拆分到不同的数据库,难度主要在于多数据源的事务控制。
dynamic-datasource插件研究了一下 可以很方便的实现数据库的垂直拆分,可以先看一下官方文档:
- 支持 数据源分组 ,适用于多种场景 纯粹多库 读写分离 一主多从 混合模式。
- 支持数据库敏感配置信息 加密 ENC()。
- 支持每个数据库独立初始化表结构schema和数据库database。
- 支持无数据源启动,支持懒加载数据源(需要的时候再创建连接)。
- 支持 自定义注解 ,需继承DS(3.2.0+)。
- 提供并简化对Druid,HikariCp,BeeCp,Dbcp2的快速集成。
- 提供对Mybatis-Plus,Quartz,ShardingJdbc,P6sy,Jndi等组件的集成方案。
- 提供 自定义数据源来源 方案(如全从数据库加载)。
- 提供项目启动后 动态增加移除数据源 方案。
- 提供Mybatis环境下的 纯读写分离 方案。
- 提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。
- 支持 多层数据源嵌套切换 。(ServiceA >>> ServiceB >>> ServiceC)。
- 提供 **基于seata的分布式事务方案。
- 提供 本地多数据源事务方案。
具体实现也很简单:
1.引入dynamic-datasource依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.5.2</version> </dependency>
2.配置多数据源配置
# dynamic-datasource spring.datasource.dynamic.primary=wim-user spring.datasource.dynamic.strict=false spring.datasource.dynamic.datasource.wim-user.url = jdbc:mysql://127.0.0.1:3306/wim-user?serverTimezone=UTC&useSSL=false spring.datasource.dynamic.datasource.wim-user.username = root spring.datasource.dynamic.datasource.wim-user.password = xxxx spring.datasource.dynamic.datasource.wim-user.driver-class-name = com.mysql.cj.jdbc.Driver spring.datasource.dynamic.datasource.wim-order.url = jdbc:mysql://127.0.0.1:3306/wim-order?serverTimezone=UTC&useSSL=false spring.datasource.dynamic.datasource.wim-order.username = root spring.datasource.dynamic.datasource.wim-order.password = yyyyy spring.datasource.dynamic.datasource.wim-order.driver-class-name = com.mysql.cj.jdbc.Driver
3.使用特定注解来切换数据源以及实现多数据源事务
@DS注解标识方法或者类 与特定数据源绑定
@Mapper @DS("wim-user") public interface IUserMapper extends BaseMapper<User> { }
@Mapper @DS("wim-order") public interface IOrderMapper extends BaseMapper<Order> { }
@DSTransactional注解来实现多数据源事务
@DSTransactional public void saveUserAndOrderDsTrans() { User user = new User(); user.setName("user" + System.currentTimeMillis()); user.setAge(0); userMapper.insert(user); log.info("TransactionService saveUserAndOrderDsTrans user {}", user); Order order = new Order(); order.setName("order" + System.currentTimeMillis()); order.setDate(new Date()); orderMapper.insert(order); log.info("TransactionService saveUserAndOrderDsTrans order {}", order); // 制造异常 事务回滚 int x = 1 / 0; log.info("x"); }
测试代码新建了两个数据库 wim-user wim-order 分别创建user表和order表,使用mybatis-plus来实现DAO
通过rest方法来测试上述方法 日志结果如下:
可以观察到数据源的切换,多数据源能够回滚
2022-12-11 16:25:01.074 DEBUG 2384 --- [o-8080-Acceptor] o.apache.tomcat.util.threads.LimitLatch : Counting up[http-nio-8080-Acceptor] latch=1
2022-12-11 16:25:01.074 DEBUG 2384 --- [o-8080-Acceptor] o.apache.tomcat.util.threads.LimitLatch : Counting up[http-nio-8080-Acceptor] latch=2
2022-12-11 16:25:01.075 DEBUG 2384 --- [nio-8080-exec-5] o.a.coyote.http11.Http11InputBuffer : Before fill(): [true], parsingRequestLine: [true], parsingRequestLinePhase: [0], parsingRequestLineStart: [0], byteBuffer.position() [0]
2022-12-11 16:25:01.075 DEBUG 2384 --- [nio-8080-exec-5] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@143c6fc1:org.apache.tomcat.util.net.NioChannel@62a3ceda:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50645]], Read from buffer: [0]
2022-12-11 16:25:01.075 DEBUG 2384 --- [nio-8080-exec-5] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@143c6fc1:org.apache.tomcat.util.net.NioChannel@62a3ceda:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50645]], Read direct from socket: [720]
2022-12-11 16:25:01.075 DEBUG 2384 --- [nio-8080-exec-5] o.a.coyote.http11.Http11InputBuffer : Received [GET /mutildb/trans/saveUserAndOrderDsTrans HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
]
2022-12-11 16:25:01.076 DEBUG 2384 --- [nio-8080-exec-5] o.a.c.authenticator.AuthenticatorBase : Security checking request GET /mutildb/trans/saveUserAndOrderDsTrans
2022-12-11 16:25:01.076 DEBUG 2384 --- [nio-8080-exec-5] org.apache.catalina.realm.RealmBase : No applicable constraints defined
2022-12-11 16:25:01.076 DEBUG 2384 --- [nio-8080-exec-5] o.a.c.authenticator.AuthenticatorBase : Not subject to any constraint
2022-12-11 16:25:01.076 DEBUG 2384 --- [nio-8080-exec-5] org.apache.tomcat.util.http.Parameters : Set encoding to UTF-8
2022-12-11 16:25:01.076 DEBUG 2384 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : GET "/mutildb/trans/saveUserAndOrderDsTrans", parameters={}
2022-12-11 16:25:01.076 DEBUG 2384 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.chen.rest.TransRest#saveUserAndOrderDsTrans()
2022-12-11 16:25:01.077 DEBUG 2384 --- [nio-8080-exec-5] c.b.dynamic.datasource.tx.LocalTxUtil : dynamic-datasource start local tx [ac2bbb0d-caa7-4600-b15e-18c8627c285e]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1afb9336] was not registered for synchronization because synchronization is not active
2022-12-11 16:25:01.077 DEBUG 2384 --- [nio-8080-exec-5] o.s.jdbc.datasource.DataSourceUtils : Fetching JDBC Connection from DataSource
2022-12-11 16:25:01.077 DEBUG 2384 --- [nio-8080-exec-5] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource switch to the datasource named [wim-user]
JDBC Connection [com.baomidou.dynamic.datasource.tx.ConnectionProxy@1ec404b1] will not be managed by Spring
==> Preparing: INSERT INTO user_t ( id, name, age ) VALUES ( ?, ?, ? )
==> Parameters: 1601855559454216194(Long), user1670747101077(String), 0(Integer)
<== Updates: 1
Committing JDBC Connection [com.baomidou.dynamic.datasource.tx.ConnectionProxy@1ec404b1]
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1afb9336]
2022-12-11 16:25:01.079 INFO 2384 --- [nio-8080-exec-5] com.chen.service.TransactionService : TransactionService saveUserAndOrderDsTrans user User(id=1601855559454216194, name=user1670747101077, age=0)
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2c73a7d3] was not registered for synchronization because synchronization is not active
2022-12-11 16:25:01.079 DEBUG 2384 --- [nio-8080-exec-5] o.s.jdbc.datasource.DataSourceUtils : Fetching JDBC Connection from DataSource
2022-12-11 16:25:01.079 DEBUG 2384 --- [nio-8080-exec-5] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource switch to the datasource named [wim-order]
JDBC Connection [com.baomidou.dynamic.datasource.tx.ConnectionProxy@6a57e348] will not be managed by Spring
==> Preparing: INSERT INTO order_t ( id, name, date ) VALUES ( ?, ?, ? )
==> Parameters: 1601855559454216195(Long), order1670747101079(String), 2022-12-11 16:25:01.079(Timestamp)
<== Updates: 1
Committing JDBC Connection [com.baomidou.dynamic.datasource.tx.ConnectionProxy@6a57e348]
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2c73a7d3]
2022-12-11 16:25:01.080 INFO 2384 --- [nio-8080-exec-5] com.chen.service.TransactionService : TransactionService saveUserAndOrderDsTrans order Order(id=1601855559454216195, name=order1670747101079, date=Sun Dec 11 16:25:01 CST 2022)
2022-12-11 16:25:01.082 DEBUG 2384 --- [nio-8080-exec-5] com.zaxxer.hikari.pool.PoolBase : wim-order - Reset (autoCommit) on connection com.mysql.cj.jdbc.ConnectionImpl@26e412ef
2022-12-11 16:25:01.083 DEBUG 2384 --- [nio-8080-exec-5] com.zaxxer.hikari.pool.PoolBase : wim-user - Reset (autoCommit) on connection com.mysql.cj.jdbc.ConnectionImpl@2bac9ba
2022-12-11 16:25:01.083 DEBUG 2384 --- [nio-8080-exec-5] c.b.dynamic.datasource.tx.LocalTxUtil : dynamic-datasource rollback local tx [ac2bbb0d-caa7-4600-b15e-18c8627c285e]
2022-12-11 16:25:01.083 DEBUG 2384 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Failed to complete request: java.lang.ArithmeticException: / by zero
2022-12-11 16:25:01.084 ERROR 2384 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/mutildb] threw exception [Request processing failed; nested exception is java.lang.ArithmeticException: / by zero] with root cause
java.lang.ArithmeticException: / by zero
at com.chen.service.TransactionService.saveUserAndOrderDsTrans(TransactionService.java:59) ~[classes/:na]
at com.chen.service.TransactionService$$FastClassBySpringCGLIB$$4d99c3a3.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at com.baomidou.dynamic.datasource.aop.DynamicLocalTransactionInterceptor.invoke(DynamicLocalTransactionInterceptor.java:40) ~[dynamic-datasource-spring-boot-starter-3.5.2.jar:3.5.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at com.chen.service.TransactionService$$EnhancerBySpringCGLIB$$fe2b6d4.saveUserAndOrderDsTrans(<generated>) ~[classes/:na]
at com.chen.rest.TransRest.saveUserAndOrderDsTrans(TransRest.java:27) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_272]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_272]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_272]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_272]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.41.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.41.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597) [tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.41.jar:9.0.41]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_272]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_272]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.41.jar:9.0.41]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_272]
2022-12-11 16:25:01.084 DEBUG 2384 --- [nio-8080-exec-5] o.a.c.c.C.[Tomcat].[localhost] : Processing ErrorPage[errorCode=0, location=/error]
2022-12-11 16:25:01.084 DEBUG 2384 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/mutildb/error", parameters={}
2022-12-11 16:25:01.085 DEBUG 2384 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-12-11 16:25:01.086 DEBUG 2384 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-12-11 16:25:01.086 DEBUG 2384 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 500
2022-12-11 16:25:01.086 DEBUG 2384 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[.[dispatcherServlet] : Disabling the response for further output
2022-12-11 16:25:01.087 DEBUG 2384 --- [nio-8080-exec-5] o.apache.coyote.http11.Http11Processor : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@143c6fc1:org.apache.tomcat.util.net.NioChannel@62a3ceda:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50645]], Status in: [OPEN_READ], State out: [CLOSED]
2022-12-11 16:25:01.087 DEBUG 2384 --- [nio-8080-exec-5] o.apache.tomcat.util.threads.LimitLatch : Counting down[http-nio-8080-exec-5] latch=2
2022-12-11 16:25:01.087 DEBUG 2384 --- [nio-8080-exec-5] org.apache.tomcat.util.net.NioEndpoint : Calling [org.apache.tomcat.util.net.NioEndpoint@5ed9e298].closeSocket([org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@143c6fc1:org.apache.tomcat.util.net.NioChannel@62a3ceda:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50645]])
总结一下使用dynamic-datasource的特点:
与现有的springboot+mybatis来集成很简单,需要保证每个mapper都是单库操作,mapper或者方法与数据源绑定,事务注解需要使用特定注解,对于已有代码有一部分改动量。该插件功能还有很多,通过与数据源的绑定可以来实现读写分离等,属于代码层面的多数据源实现插件。