ShardingJDBC:适配OceanBase

版本

sharding-jdbc版本

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

这个版本支持的数据库在org.apache.shardingsphere.core.constant.DatabaseType有标明:

public enum DatabaseType {
    H2("H2"),
    MySQL("MySQL"),
    PostgreSQL("PostgreSQL"),
    Oracle("Oracle"),
    SQLServer("Microsoft SQL Server");
    ***

注意,不同版本的shardingsphere适配方式是不一样的,因为据我观察每个版本确定数据库类型的流程有差异

oceanbase驱动版本

<dependency>
    <groupId>com.alipay.oceanbase</groupId>
    <artifactId>oceanbase-client</artifactId>
    <version>1.1.7</version>
</dependency>

适配

org.apache.shardingsphere.core.metadata.datasource.dialect.MySQLDataSourceMetaData

项目启动时,会由这个类去匹配数据源的数据库类型

原代码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.apache.shardingsphere.core.metadata.datasource.dialect;

import com.google.common.base.Strings;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.metadata.datasource.DataSourceMetaData;

public final class MySQLDataSourceMetaData implements DataSourceMetaData {
    private static final int DEFAULT_PORT = 3306;
    private final String hostName;
    private final int port;
    private final String schemaName;
    private final Pattern pattern = Pattern.compile("jdbc:mysql:(\\w*:)?//([\\w\\-\\.]+):?([0-9]*)/([\\w\\-]+);?\\S*", 2);

    public MySQLDataSourceMetaData(String url) {
        Matcher matcher = this.pattern.matcher(url);
        if (matcher.find()) {
            this.hostName = matcher.group(2);
            this.port = Strings.isNullOrEmpty(matcher.group(3)) ? 3306 : Integer.valueOf(matcher.group(3));
            this.schemaName = matcher.group(4);
        } else {
            throw new ShardingException("The URL of JDBC is not supported. Please refer to this pattern: %s.", new Object[]{this.pattern.pattern()});
        }
    }

    public boolean isInSameDatabaseInstance(DataSourceMetaData dataSourceMetaData) {
        return this.hostName.equals(dataSourceMetaData.getHostName()) && this.port == dataSourceMetaData.getPort();
    }

    public String getHostName() {
        return this.hostName;
    }

    public int getPort() {
        return this.port;
    }

    public String getSchemaName() {
        return this.schemaName;
    }

    public Pattern getPattern() {
        return this.pattern;
    }
}

pattern增加oceanbase

构造函数将各个下标增1

这样就能让程序将oceanbase当作mysql处理

复制一份到项目路径下,修改后,启动时会覆盖jar中此类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.apache.shardingsphere.core.metadata.datasource.dialect;

import com.google.common.base.Strings;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.metadata.datasource.DataSourceMetaData;

public final class MySQLDataSourceMetaData implements DataSourceMetaData {
    private static final int DEFAULT_PORT = 3306;
    private final String hostName;
    private final int port;
    private final String schemaName;
    private final Pattern pattern = Pattern.compile("jdbc:(mysql|oceanbase):(\\w*:)?//([\\w\\-\\.]+):?([0-9]*)/([\\w\\-]+);?\\S*", 2);

    public MySQLDataSourceMetaData(String url) {
        Matcher matcher = this.pattern.matcher(url);
        if (matcher.find()) {
            this.hostName = matcher.group(3);
            this.port = Strings.isNullOrEmpty(matcher.group(4)) ? 3306 : Integer.valueOf(matcher.group(4));
            this.schemaName = matcher.group(5);
        } else {
            throw new ShardingException("The URL of JDBC is not supported. Please refer to this pattern: %s.", new Object[]{this.pattern.pattern()});
        }
    }

    public boolean isInSameDatabaseInstance(DataSourceMetaData dataSourceMetaData) {
        return this.hostName.equals(dataSourceMetaData.getHostName()) && this.port == dataSourceMetaData.getPort();
    }

    public String getHostName() {
        return this.hostName;
    }

    public int getPort() {
        return this.port;
    }

    public String getSchemaName() {
        return this.schemaName;
    }

    public Pattern getPattern() {
        return this.pattern;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值