ShardingJDBC:适配OceanBase

况弘新
2023-12-01

版本

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;
    }
}
 类似资料: