ShardingJDBC:适配OceanBase

导读:本篇文章讲解 ShardingJDBC:适配OceanBase,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

前言

项目本来是基于MySQL开发的,后要适配OceanBase,经研究其语法基本一致,已经集成sharding-jdbc的项目需要考虑支持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;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/93668.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!