shardingsphere如何支持utf8mb4

我们在使用mysql的过程中,由于业务需求,在创建database中有时候需要支持特殊表情符号存储,比较直接简单的方式就是设置database的编码格式为utf8mb4

那么在使用shardingsphere进行分表分库的时候,如何让shardingsphere也支持utf8mb4呢?

查看shardingsphere的源码,我们可以看到shardingsphere对于datasource进行了二次封装,目前使用的shardingsphere-4.0.0-RC1这个版本中,如果需要对于utf8mb4需要我们对源码进行二次开发

1、创建springboot工程

可以直接使用Spring Initializr生成项目

2、添加依赖关系

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>

3、在application.properties文件中配置添加如下:

spring.shardingsphere.datasource.ds0.connection-init-sqls = set names utf8mb4;

4、重写org.apache.shardingsphere.shardingjdbc.spring.boot.util.DataSourceUtil

我们先把shardingsphere的DataSourceUtil的源码贴出来

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.shardingjdbc.spring.boot.util;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Sets;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
impor
### ShardingSphere 5.0 连接 NACVT 的解决方案 当遇到 ShardingSphere 5.0 连接 NACVT 数据库出现问题的情况时,可以从多个方面排查并解决问题。 #### 1. 检查依赖版本兼容性 确保所使用的 ShardingSphere 版本与 NACVT 数据库驱动程序完全兼容。不同版本之间可能存在不兼容的问题,建议查阅官方文档确认支持的数据库及其对应驱动版本[^1]。 #### 2. 配置数据源属性 对于某些特定类型的数据库,在配置 `DataSource` 属性时可能需要额外设置一些参数来适应其特性。例如调整连接超时时间、字符集编码等,这些都可以在应用项目的 `application.yml` 文件中进行相应修改[^3]: ```yaml spring: datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: org.nacvt.jdbc.Driver url: jdbc:nacvt://<host>:<port>/<database> username: root password: password connectionTimeout: 30000 idleTimeout: 600000 maxLifetime: 1800000 characterEncoding: utf8mb4 ``` #### 3. 自定义 SQL 解析规则 如果发现部分 SQL 查询语句无法正常解析或执行效率低下,则可以考虑通过自定义 SQL 解析器来进行优化处理。这通常涉及到编写 Java 类扩展默认行为,并将其注册到 Spring Boot 应用上下文中作为 Bean 实例[^2]。 #### 4. 调整缓存策略缓解首次查询延迟 针对初次访问速度较慢的现象,可以通过启用客户端侧的结果集缓存机制加以改善。具体做法是在应用程序层面引入合适的第三方组件(如 Ehcache 或 Caffeine),并将常用的数据对象存储于内存之中以便快速检索使用[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值