集群部署
在阅读本文档前,你需要先阅读部署先决条件文档来完成部署
shenyu前的环境准备工作。
本文是介绍在集群环境中快速部署ShenYu网关。
在阅读本文档时,你可以先阅读二进制包部署。
环境准备#
- 至少准备两台已经安装了JDK1.8+的服务器用于部署网关启动器。
- 准备一台已经安装了mysql、pgsql、h2和JDK1.8+的服务器用于部署网关管理端。
- 准备一台服务器用于部署Nginx。
启动 Apache ShenYu Admin#
在你的网关管理端服务器下载并解压apache-shenyu-${current.version}-admin-bin.tar.gz 。
配置你的数据库,进入
/conf目录,在application.yaml文件中修改spring.profiles.active节点为mysql,pgorh2。配置你的数据同步方式,进入
/conf目录,在application.yaml文件中修改shenyu.sync节点为websocket,http,zookeeper,etcd,consul或者nacos。进入
bin目录,启动ShenYu Admin。
> windows: start.bat
> linux: ./start.sh 启动 Apache ShenYu Boostrap#
在你的网关启动器服务器下载并解压apache-shenyu-${current.version}-bootstrap-bin.tar.gz 。
配置你的数据同步方式,进入
/conf目录,在application.yaml文件中修改shenyu.sync节点为websocket,http,zookeeper,etcd,consul或者nacos,这个配置必须与ShenyYu Admin的配置保持相同。进入
bin目录,启动ShenYu Bootstrap。
> windwos : start.bat
> linux : ./start.sh 在完成这些操作后,你将成功部署
ShenYu Boostrap集群。假如你
10.1.1.1和10.1.1.2两台服务器在将部署ShenYu Bootstrap,并且在10.1.1.3部署nginx。
启动 Nginx#
下载并安装nginx。
在
nginx.conf文件中修改upstream和server节点的配置。
upstream shenyu_gateway_cluster { ip_hash; server 10.1.1.1:9195 max_fails=3 fail_timeout=10s weight=50; server 10.1.1.2:9195 max_fails=3 fail_timeout=10s weight=50;}
server { listen 9195; location / { proxy_pass http://shenyu_gateway_cluster; proxy_set_header HOST $host; proxy_read_timeout 10s; proxy_connect_timeout 10s; }}- 启动 nginx.
> windows: ./nginx.exe
> linux: /usr/local/nginx/sbin/nginx - 验证nginx配置是否生效,在
ShenYu Bootstrap或者Nginx的日志文件中查看请求被分发到那台服务器上。
Apache Shenyu-nginx模块实现集群#
该模块提供SDK,用于通过注册中心为OpenResty自动监听Apache Shenyu可用的实例节点。 在集群模式下,Apache Shenyu支持部署多个Shenyu实例,随时可能有新的实例上线或下线。因此,Apache Shenyu引入了服务发现 OpenResty 模块来帮助客户端检测可用Shenyu实例。目前Apache Shenyu已经支持Zookeeper、Nacos、Etcd和Consul。Client或LoadBalancer 可以通过这些Service注册中心获取可用的Shenyu实例。
入门#
- 先决条件
- Luarocks
- OpenResty
从源码构建#
首先,从GitHub clone源码。
git clone https://github.com/apache/shenyu-nginx然后,从源代码构建并安装。
cd shenyu-nginxluarocks make rockspec/shenyu-nginx-main-0.rockspecEtcd开始#
修改Nginx配置,创建并初始化Shenyu register模块,连接至目标注册中心。该模块将获取在同一个集群中注册到Etcd的 所有Shenyu实例。它与Etcd客户端一样监视(基于长轮询)Shenyu实例列表。 Etcd示例:
init_worker_by_lua_block { local register = require("shenyu.register.etcd") register.init({ balancer_type = "chash", etcd_base_url = "http://127.0.0.1:2379", })}balancer_type指定负载均衡模式。它支持chash和roundrobin。etcd_base_url指定Etcd服务器。(目前不支持身份验证)。
最后,重启OpenResty。
openresty -s reload这就是一个完整的Etcd的使用示例 。
Nacos开始#
修改Nginx配置,创建并初始化Shenyu register模块,连接至目标注册中心。以下是Nacos的示例:
Nacos示例:
init_worker_by_lua_block { local register = require("shenyu.register.nacos") register.init({ shenyu_storage = ngx.shared.shenyu_storage, balancer_type = "chash", nacos_base_url = "http://127.0.0.1:8848", username = "nacos", password = "naocs", })}balancer_type指定负载均衡模式。它支持chash和roundrobin。nacos_base_url指定Nacos服务器地址。username指定登录Nacos的用户名。(仅在启用 Nacos auth 时才需要)password指定登录Nacos的密码。
修改upstream启用动态更新shenyu实例列表。本案例将Shenyu实例列表与注册中心同步。
upstream shenyu { server 0.0.0.1; -- bad balancer_by_lua_block { require("shenyu.register.nacos").pick_and_set_peer() }}最后,重启OpenResty。
openresty -s reload这就是一个完整的Nacos的使用example 。
Zookeeper开始#
修改Nginx配置,创建并初始化Shenyu register模块,连接目标注册中心。 通过 zookeeper watch 事件监听Shenyu实例列表的变化。下面是 zookeeper 配置的示例。
Zookeeper示例:
init_worker_by_lua_block { local register = require("shenyu.register.zookeeper") register.init({ servers = {"127.0.0.1:2181","127.0.0.1:2182"}, shenyu_storage = ngx.shared.shenyu_storage, balancer_type = "roundrobin" }); }serverszookeeper 集群地址。balancer_type指定负载均衡模式。它支持chash和round robin。
修改upstream启用动态更新Shenyu实例列表。本案例将Shenyu实例列表与注册中心同步。
upstream shenyu { server 0.0.0.1; balancer_by_lua_block { require("shenyu.register.zookeeper").pick_and_set_peer() } }最后,重启 OpenResty。
openresty -s reload这是一个使用 Zookeeper的完整示例 。