---
title: 如何利用ShardingSphere-proxy搭建openGauss分布式环境-官方技术文章-鲲鹏社区
description: 本文主要介绍如何利用ShardingSphere-proxy搭建openGauss分布式环境。
keywords: ShardingSphere,如何利用,Gauss,搭建,分布式环境,官方技术文章,鲲鹏社区,简介
url: https://www.hikunpeng.com/developer/techArticles/20230912-15?envFlag=1
section: (其他)
---

# 如何利用ShardingSphere-proxy搭建openGauss分布式环境-官方技术文章-鲲鹏社区

URL: https://www.hikunpeng.com/developer/techArticles/20230912-15?envFlag=1
描述: 本文主要介绍如何利用ShardingSphere-proxy搭建openGauss分布式环境。
关键词: ShardingSphere,如何利用,Gauss,搭建,分布式环境,官方技术文章,鲲鹏社区,简介

官方技术文章 [了解详情](https://www.hikunpeng.com/zh/developer/techArticles)

如何利用ShardingSphere-proxy搭建openGauss分布式环境

如何利用ShardingSphere-proxy搭建openGauss分布式环境

实战经验

发表于 2021/09/15


## ShardingSphere-proxy简介

ShardingSphere-proxy(以下简称为"proxy")定位为透明化的数据库代理端，提供封装了数据库二进制协议的服务端版本，用于完成对异构语言的支持。

proxy实现分布式的核心原理是，使用netty捕获客户端(gsql或jdbc)的sql语句，通过抽象语法树解析sql,根据配置的分库分片规则，改写sql语句，使其路由到对应的数据库上并聚合多个sql的返回结果，再将结果通过netty返回给客户端，这样就完成了分库分片的全流程，如下图示:

## ShardingSphere-proxy获取

为了能使proxy正常工作，需要向lib目录中增加openGauss的jdbc驱动，此驱动可以从maven中央仓库下载，坐标是:

```
<groupId>org.opengauss</groupId>

<artifactId>opengauss-jdbc</artifactId>
```

目前需要从master分支自行编译：

链接：https://github.com/apache/shardingsphere/tree/master [了解详情](https://github.com/apache/shardingsphere/tree/master)

本示例为从openGauss分支上自己编译出包。

## 搭建openGauss分布式环境

（1）解压二进制包

获取二进制包后，可以通过tar -zxvf命令进行解压，解压后的内容如下:

（2）替换为openGauss jdbc

进入到lib目录下，并且将原有的postgresql-42.2.5.jar删除，将opengauss-jdbc的jar放置在该目录下即可。

（3）修改server.yaml

进入conf目录, 该目录下已经有server.yaml文件的模板。该配置文件的主要作用是配置前端的认证数据库、用户名和密码, 以及连接相关的属性：包括分布式事务类型、sql日志等。

当然proxy还支持governance配置中心,它可以从配置中心读取配置或者永久保存配置，本次使用暂不涉及其使用。

server.yaml最简配置如下:

```
rules:
- !AUTHORITY
users:
- root@%:root
- sharding@:sharding
provider:
type: ALL_PRIVILEGES_PERMITTED

props:
max-connections-size-per-query: 1
executor-size: 16 # Infinite by default.
proxy-frontend-flush-threshold: 128 # The default value is 128.
```

server.yaml更多详细配置参考:

链接https://docs.projectcalico.org/archive/v3.25/manifests/calico.yaml [了解详情](https://docs.projectcalico.org/archive/v3.25/manifests/calico.yaml)

（4）修改config-sharding.yaml

进入conf目录，该目录下已经有config-sharding.yaml文件的模板。该文件主要作用是配置后端与openGauss数据库的连接属性，分库分表规则等。

本次分片示例为，数据分两个库，表分为3片，数据库分片键为ds_id,值按2取余，表分片键为ts_id,值按3取余。

分库后插入数据分布如下:

config-sharding.yaml极简配置如下:

```
dataSources:
ds_0:
connectionTimeoutMilliseconds: 10000
idleTimeoutMilliseconds: 10000
maintenanceIntervalMilliseconds: 10000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 200
minPoolSize: 10
password: Huawei@123
url: jdbc:opengauss://90.90.44.171:44000/ds_0?serverTimezone=UTC&useSSL=false&connectTimeout=10
username: test
ds_1:
connectionTimeoutMilliseconds: 10000
idleTimeoutMilliseconds: 10000
maintenanceIntervalMilliseconds: 10000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 200
minPoolSize: 10
password: Huawei@123
url: jdbc:opengauss://90.90.44.171:44000/ds_1?serverTimezone=UTC&useSSL=false&connectTimeout=10
username: test
rules:
- !SHARDING
defaultDatabaseStrategy:
none: null
defaultTableStrategy:
none: null
shardingAlgorithms:
ds_t1_alg:
props:
algorithm-expression: ds_${ds_id % 2}
type: INLINE
ts_t1_alg:
props:
algorithm-expression: ds_${ts_id % 3}
type: INLINE
tables:
t1:
actualDataNodes: ds_${0..1}.t1_${0..2}
databaseStrategy:
standard:
shardingAlgorithmName: ds_t1_alg
shardingColumn: ds_id
tableStrategy:
standard:
shardingAlgorithmName: ts_t1_alg
shardingColumn: ts_id
schemaName: sharding_db
```

config-sharding.yaml更多详细配置参考

链接：

（5）启动ShardingSphere-proxy

进入bin目录，以上配置完成后，使用sh start.sh即可启动proxy服务，默认绑定3307端口。可以在启动脚本时使用sh start.sh 4000修改为4000端口。

## 环境验证

要想确认proxy是否正确启动，请查看logs/stdout.log日志，当存在提示成功启动日志后即成功。

在opengauss数据库上使用gsql -d sharding_db -h $proxy_ip -p 3307 -U sharding -W sharding -r即可连接数据库。

## 分布式数据库使用

上面的部署已经确认proxy环境可用了，那么连上gsql就可以对分布式数据进行操作了，默认已经使用gsql连接上终端了。

（1）新建表

在gsql终端中执行create table t1 (id int primary key, ds_id int, ts_id int, data varchar(100));即可创建表，它的语法不需要任何修改。

（2）增删改

1）增:insert into t1 values (0, 0, 0, 'aaa')

2）删:delete from t1 where id = 0

3）改:update t1 set data = 'ccc' where ds_id = 1;

以上语法不需要任何修改即可执行。

（3）查

select * from t1 即可获取所有的数据，在proxy中简单的select语句几乎不需要修改语法即可执行。

复杂的查询语法(如二次子查询)当前支持的不是很完整，可以持续向ShardingSphere社区提交issue来更新。

已经支持和未支持的SQL请参考:

链接：

（4）事务

ShardingSphere事务使用方法与原来的方式一致，依然通过begin/commit/rollback来实现。

欢迎访问openGauss官方网站

openGauss开源社区官方网站：

https://opengauss.org/ [了解详情](https://opengauss.org)

openGauss组织仓库：

https://gitcode.com/opengauss [了解详情](https://gitcode.com/opengauss)

openGauss镜像仓库：

https://github.com/opengauss-mirror [了解详情](https://github.com/opengauss-mirror)
