数据同步配置
本篇主要讲解如何配置数据同步策略,数据同步是指在 shenyu-admin
后台操作数据以后,使用何种策略将数据同步到 Apache ShenYu
网关。Apache ShenYu
网关当前支持ZooKeeper
、WebSocket
、Http长轮询
、Nacos
、Etcd
和 Consul
进行数据同步。

数据同步原理请参考设计文档中的 数据同步原理 。
#
WebSocket同步配置(默认方式,推荐)Apache ShenYu
网关配置首先在
pom.xml
文件中引入以下依赖:
<!-- apache shenyu data sync start use websocket--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-sync-data-websocket</artifactId> <version>${project.version}</version> </dependency>

然后在 yml
文件中进行如下配置:
shenyu: sync: websocket : # urls:是指 shenyu-admin的地址,如果有多个,请使用(,)分割。 urls: ws://localhost:9095/websocket allowOrigin: ws://localhost:9195

shenyu-admin 配置
在
yml
文件中进行如下配置:
shenyu: sync: websocket: enabled: true

当建立连接以后会全量获取一次数据,以后的数据都是增量的更新与新增,性能好。而且也支持断线重连 (默认30
秒)。推荐使用此方式进行数据同步,也是Apache ShenYu
默认的数据同步策略。
#
Zookeeper同步配置请注意,从 ShenYu 2.5.0 起将不再支持 Zookeeper 3.4.x 或更低版本。如果您已经使用了 Zookeeper 3.4.x 或更低的版本,您需要使用更高的 Zookeeper 版本并重新初始化数据。
Apache ShenYu
网关配置首先在
pom.xml
文件中引入以下依赖:<!-- apache shenyu data sync start use zookeeper--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-sync-data-zookeeper</artifactId> <version>${project.version}</version> </dependency>
然后在 yml
文件中进行如下配置:
shenyu: sync: zookeeper: url: localhost:2181 # url: 配置成你的 zookeeper 地址,集群环境请使用(,)分隔 sessionTimeout: 5000 connectionTimeout: 2000

shenyu-admin
配置
在 yml
文件中进行如下配置:
shenyu: sync: zookeeper: url: localhost:2181 # url: 配置成你的 zookeeper 地址,集群环境请使用(,)分隔 sessionTimeout: 5000 connectionTimeout: 2000

使用zookeeper
同步机制也是非常好的,时效性也高,但是要处理zookeeper
环境不稳定,集群脑裂等问题。
#
Http长轮询同步配置Apache ShenYu
网关配置
首先在 pom.xml
文件中引入以下依赖:
<!-- apache shenyu data sync start use http--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-sync-data-http</artifactId> <version>${project.version}</version> </dependency>

然后在 yml
文件中进行如下配置:
shenyu: sync: http: url: http://localhost:9095 # url: 配置成你的 shenyu-admin 的 ip 与端口地址,多个admin集群环境请使用(,)分隔。

shenyu-admin
配置
在 yml
文件中进行如下配置:
shenyu: sync: http: enabled: true

使用Http长轮询
进行数据同步,会让网关很轻量,但时效性略低。它是根据分组key
来拉取,如果数据量过大,过多,会有一定的影响。原因是一个组下面的一个小地方更改,都会拉取整个组的数据。
#
Nacos同步配置Apache ShenYu
网关配置
首先在 pom.xml
文件中引入以下依赖:
<!-- apache shenyu data sync start use nacos--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-sync-data-nacos</artifactId> <version>${project.version}</version> </dependency>

然后在 yml
文件中进行如下配置:
shenyu: sync: nacos: url: localhost:8848 # url: 配置成你的 nacos地址,集群环境请使用(,)分隔。 namespace: 1c10d748-af86-43b9-8265-75f487d20c6c username: password: acm: enabled: false endpoint: acm.aliyun.com namespace: accessKey: secretKey: # 其他参数配置,请参考 naocs官网。

shenyu-admin
配置
在 yml
文件中进行如下配置:
shenyu: sync: nacos: url: localhost:8848 # url: 配置成你的 nacos地址,集群环境请使用(,)分隔。 namespace: 1c10d748-af86-43b9-8265-75f487d20c6c username: password: acm: enabled: false endpoint: acm.aliyun.com namespace: accessKey: secretKey: # 其他参数配置,请参考 naocs官网。

#
Etcd 同步配置Apache ShenYu
网关配置
首先在 pom.xml
文件中引入以下依赖:
<!-- apache shenyu data sync start use etcd--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-sync-data-etcd</artifactId> <version>${project.version}</version> <exclusions> <exclusion> <groupId>io.grpc</groupId> <artifactId>grpc-grpclb</artifactId> </exclusion> <exclusion> <groupId>io.grpc</groupId> <artifactId>grpc-netty</artifactId> </exclusion> </exclusions> </dependency>

然后在 yml
文件中进行如下配置:
shenyu: sync: etcd: url: http://localhost:2379 # url: 配置成你的 etcd,集群环境请使用(,)分隔。

shenyu-admin
配置
在 yml
文件中进行如下配置:
shenyu: sync: etcd: url: http://localhost:2379 # url: 配置成你的 etcd,集群环境请使用(,)分隔。

#
Consul 同步配置Apache ShenYu
网关配置
首先在 pom.xml
文件中引入以下依赖:
<!-- apache shenyu data sync start use consul--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-sync-data-consul</artifactId> <version>${project.version}</version> </dependency>

然后在 yml
文件中进行如下配置:
shenyu: sync: consul: url: http://localhost:8500 waitTime: 1000 # 查询等待时间 watchDelay: 1000 # 数据同步间隔时间

shenyu-admin
配置
在 yml
文件中进行如下配置:
shenyu: sync: consul: url: http://localhost:8500

在
Apache ShenYu
网关和shenyu-admin
重新配置数据同步策略后,需要重启服务。
Apache ShenYu
网关 和shenyu-admin
必须使用相同的同步策略。
#
Apollo同步配置Apollo仅支持Java [8,17)版本,如果您要使用apollo作为数据同步中心,请确保您的JDK版本在[8,17)之间。
Apache ShenYu
网关配置
从https://repo1.maven.org/maven2/org/apache/shenyu/shenyu-spring-boot-starter-sync-data-apollo/
下载对应版本的jar包,然后将jar包放到/lib
目录下。
在yaml文件中添加如下配置:
shenyu: sync: apollo: appId: shenyu meta: http://localhost:8080 env: dev clusterName: test namespace: application
Apache ShenYu Admin
配置
从https://repo1.maven.org/maven2/org/apache/shenyu/shenyu-admin-listener-apollo/
下载对应版本的jar包,然后将jar包放到/lib
目录下。
在yaml文件中添加如下配置:
shenyu: sync: apollo: meta: http://localhost:8080 appId: shenyu portalUrl: http://localhost:8070 env: dev clusterName: test namespace: application token: 0fff5645fc74ee5e0d63a6389433c8c8afc0beea31eed0279ecc1c8961d12da9

在
Apache ShenYu
网关和shenyu-admin
重新配置数据同步策略后,需要重启服务。Apache ShenYu
网关 和shenyu-admin
必须使用相同的同步策略。