• ShardingJDBC:适配OceanBase


    版本

    sharding-jdbc版本

    <dependency>
        <groupId>org.apache.shardingspheregroupId>
        <artifactId>sharding-jdbc-spring-boot-starterartifactId>
        <version>4.0.0-RC1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    public enum DatabaseType {
        H2("H2"),
        MySQL("MySQL"),
        PostgreSQL("PostgreSQL"),
        Oracle("Oracle"),
        SQLServer("Microsoft SQL Server");
        ***
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

    oceanbase驱动版本

    <dependency>
        <groupId>com.alipay.oceanbasegroupId>
        <artifactId>oceanbase-clientartifactId>
        <version>1.1.7version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    适配

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    如何获取淘宝店铺详情数据接口
    Nginx可用参数
    2023年中国人力资源咨询发展历程及市场规模前景分析[图]
    TPCH_Q4 的分析优化,对子查询中的 Semi-join 优化
    VScode远程连接主机
    搭建自己的语义分割平台deeplabV3+
    测试只能干到35岁?35岁+的测试就会失业?
    微信小程序链接快速生成方法
    MySQL【事务】与【索引】:
    静态代理模式
  • 原文地址:https://blog.csdn.net/weixin_43859729/article/details/127431405