当前位置: 首页 > ai >正文

使用ShardingSphere5.5.1实现读写分离与相关异常问题处理

环境

SpringBoot版本:2.7.1
ShardingSphere版本:5.5.1

相关配置

  1. application-read-write-split-2.yml
server:port: 9092
spring:datasource:driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriverurl: jdbc:shardingsphere:classpath:read-write-split.yaml
  1. 编写read-write-split.yaml文件
mode:type: Standalonerepository:type: JDBC
dataSources:write_ds:dataSourceClassName: com.zaxxer.hikari.HikariDataSourcedriverClassName: org.postgresql.DriverjdbcUrl: username: password: read_ds_0:dataSourceClassName: com.zaxxer.hikari.HikariDataSourcedriverClassName: org.postgresql.DriverjdbcUrl: username: password: read_ds_1:dataSourceClassName: com.zaxxer.hikari.HikariDataSourcedriverClassName: org.postgresql.DriverjdbcUrl: username: password: rules:- !SINGLEtables:- "*.*.*"defaultDataSource: write_ds # 默认数据源,仅在执行 CREATE TABLE 创建单表时有效。缺失值为空,表示随机单播路由。- !READWRITE_SPLITTINGdataSourceGroups:readwrite_ds:writeDataSourceName: write_dsreadDataSourceNames:- read_ds_0- read_ds_1transactionalReadQueryStrategy: PRIMARYloadBalancerName: roundloadBalancers:round:type: ROUND_ROBIN
props:sql-show: true

注意

  1. 请求报错:nested exception is java.lang.NoSuchMethodError: org.yaml.snakeyaml.LoaderOptions.setCodePointLimit(I)V] with root cause
2025-05-10 14:12:43.690 ERROR 16804 --- [nio-9092-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.yaml.snakeyaml.LoaderOptions.setCodePointLimit(I)V] with root causejava.lang.NoSuchMethodError: org.yaml.snakeyaml.LoaderOptions.setCodePointLimit(I)V

解决方式:
在SpringBoot中引入如下依赖:

        <dependency><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId><version>1.33</version></dependency>
  1. 请求接口后,找不到相关表异常:TableNotFoundException: Table or view ‘xxxxxxxx’ does not exist.] with root cause
2025-05-10 14:16:35.458 ERROR 33572 --- [nio-9092-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error preparing statement.  Cause: org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException: Table or view 'xxxxxxxx' does not exist.] with root causeorg.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException: Table or view 'xxxxxxxx' does not exist.

解决方式:
在读写分离配置文件中一定加入单表配置,详见上述的read-write-split.yaml配置方式

  - !SINGLEtables:- "*.*.*"defaultDataSource: write_ds # 默认数据源,仅在执行 CREATE TABLE 创建单表时有效。缺失值为空,表示随机单播路由。

验证结果

  1. 使用SpringBoot在application.yml中激活使用application-read-write-split-2.yml文件
  2. 效果截图
    在这里插入图片描述
    从截图上看,成功实现了读写分离
http://www.xdnf.cn/news/5102.html

相关文章:

  • vmware环境ORACLE RAC环境数据库节点1无法启动问题分析处理
  • BeanPostProcessor和AOP
  • 使用FastAPI和Apache Flink构建跨环境数据管道
  • AUTOSAR图解==>AUTOSAR_SWS_PlatformTypes
  • TXT编码转换工具iconv
  • WPF 性能 UI 虚拟化 软件开发人员的思考
  • 阿里云OSS+CDN自动添加文章图片水印配置指南
  • 第五天 车载系统安全(入侵检测、OTA安全) 数据加密(TLS/SSL、国密算法)
  • Kubernetes生产实战(十四):Secret高级使用模式与安全实践指南
  • 解决mybatisplus主键无法自增的问题
  • uniapp-商城-50-后台 商家信息
  • 如何用AWS Lambda构建无服务器解决方案:实战经验与场景解析
  • 第十八节:图像梯度与边缘检测-Scharr 算子
  • OpenLayers 精确经过三个点的曲线绘制
  • opencv处理图像(二)
  • 抖音视频去水印怎么操作
  • Taro 编译不平不同平台小程序
  • 1.2.2.1.4 数据安全发展技术发展历程:高级公钥加密方案——同态加密
  • Java数据结构——二叉树
  • 进程间通信--管道【Linux操作系统】
  • Maven 插件配置分层架构深度解析
  • 滚珠丝杆在工作中损耗会影响什么?
  • 【计算机视觉】3DDFA_V2中表情与姿态解耦及多任务平衡机制深度解析
  • Android Compose 框架物理动画之捕捉动画深入剖析(29)
  • 封装 RabbitMQ 消息代理交互的功能
  • mac u盘重装mac10.15Catalina系统
  • 1.短信登录
  • 数据库故障排查全攻略:从实战案例到体系化解决方案
  • expo多网络请求设定。
  • Jmeter中的BeanShell如何使用?