客户端接入配置

应用客户端接入是指将你的微服务接入到Apache ShenYu网关,当前支持HttpDubboSpring CloudgRPCMotanSofaTars等协议的接入。

将应用客户端接入到Apache ShenYu网关是通过注册中心来实现的,涉及到客户端注册和服务端同步数据。注册中心支持HttpZookeeperEtcdConsulNacos

本篇文章介绍将应用客户端接入到Apache ShenYu网关,应该如何配置。相关原理请参考设计文档中的 客户端接入原理

客户端接入配置 - 图1

Http方式注册配置

shenyu-admin配置

yml文件中配置注册类型为http,配置信息如下:

  1. shenyu:
  2. register:
  3. registerType: http
  4. props:
  5. checked: true #是否开启检测
  6. zombieCheckTimes: 5 #失败几次后剔除服务
  7. scheduledTime: 10 #定时检测间隔时间 (秒)

客户端接入配置 - 图2

shenyu-client配置

下面展示的是http服务作为客户端接入到Apache ShenYu网关时,通过Http方式注册配置信息。其他客户端接入时(DubboSpring Cloud等),配置方式同理。

在微服务中的 yml文件配置注册方式设置为http,并填写shenyu-admin服务地址列表,配置信息如下:

  1. shenyu:
  2. client:
  3. registerType: http
  4. serverLists: http://localhost:9095
  5. props:
  6. contextPath: /http
  7. appName: http
  8. port: 8188
  9. isFull: false
  10. # registerType : 服务注册类型,填写 http
  11. # serverList: 为http注册类型时,填写Shenyu-Admin项目的地址,注意加上http://,多个地址用英文逗号分隔
  12. # port: 你本项目的启动端口,目前springmvc/tars/grpc需要进行填写
  13. # contextPath: 为你的这个mvc项目在shenyu网关的路由前缀, 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
  14. # appName:你的应用名称,不配置的话,会默认取 `spring.application.name` 的值
  15. # isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller;目前适用于springmvc/springcloud

客户端接入配置 - 图3

Zookeeper方式注册配置

shenyu-admin配置

  • 首先在 pom 文件中加入相关的依赖(默认已经引入):
  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-register-server-zookeeper</artifactId>
  4. <version>${project.version}</version>
  5. </dependency>

客户端接入配置 - 图4

  • 然后在yml文件中配置注册类型为zookeeper,填写zookeeper服务地址和参数,配置信息如下:
  1. shenyu:
  2. register:
  3. registerType: zookeeper
  4. serverLists: localhost:2181
  5. props:
  6. sessionTimeout: 5000
  7. connectionTimeout: 2000

客户端接入配置 - 图5

shenyu-client配置

下面展示的是http服务作为客户端接入到Apache ShenYu网关时,通过Zookeeper方式注册配置信息。其他客户端接入时(DubboSpring Cloud等),配置方式同理。

  • 首先在 pom文件中加入相关的依赖:
  1. <!-- apache shenyu zookeeper register center -->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-register-client-zookeeper</artifactId>
  5. <version>${shenyu.version}</version>
  6. </dependency>

客户端接入配置 - 图6

  • 然后在 yml 中配置注册类型为zookeeper,并填写Zookeeper服务地址和相关参数,如下:
  1. shenyu:
  2. client:
  3. registerType: zookeeper
  4. serverLists: localhost:2181
  5. props:
  6. contextPath: /http
  7. appName: http
  8. port: 8189
  9. isFull: false
  10. # registerType : 服务注册类型,填写 zookeeper
  11. # serverList: 为zookeeper注册类型时,填写zookeeper地址,多个地址用英文逗号分隔
  12. # port: 你本项目的启动端口,目前springmvc/tars/grpc需要进行填写
  13. # contextPath: 为你的这个mvc项目在shenyu网关的路由前缀, 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
  14. # appName:你的应用名称,不配置的话,会默认取 `spring.application.name` 的值
  15. # isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller;目前适用于springmvc/springcloud

客户端接入配置 - 图7

Etcd方式注册配置

shenyu-admin配置

  • 首先在 pom 文件中加入相关的依赖(默认已经引入):
  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-register-server-etcd</artifactId>
  4. <version>${project.version}</version>
  5. </dependency>

客户端接入配置 - 图8

  • 然后在 yml 配置注册类型为etcd, 填写etcd服务地址和参数,配置信息如下:
  1. shenyu:
  2. register:
  3. registerType: etcd
  4. serverLists : http://localhost:2379

客户端接入配置 - 图9

shenyu-client配置

下面展示的是http服务作为客户端接入到Apache ShenYu网关时,通过Etcd方式注册配置信息。其他客户端接入时(DubboSpring Cloud等),配置方式同理。

  • 首先在 pom 文件中加入相关的依赖:
  1. <!-- apache shenyu etcd register center -->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-register-client-etcd</artifactId>
  5. <version>${shenyu.version}</version>
  6. </dependency>

客户端接入配置 - 图10

  • 然后在 yml 中配置注册类型为etcd, 并填写etcd服务地址和相关参数,如下:
  1. shenyu:
  2. client:
  3. registerType: etcd
  4. serverLists: http://localhost:2379
  5. props:
  6. contextPath: /http
  7. appName: http
  8. port: 8189
  9. isFull: false
  10. # registerType : 服务注册类型,填写 etcd
  11. # serverList: 为etcd注册类型时,填写etcd地址,多个地址用英文逗号分隔
  12. # port: 你本项目的启动端口,目前springmvc/tars/grpc需要进行填写
  13. # contextPath: 为你的这个mvc项目在shenyu网关的路由前缀, 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
  14. # appName:你的应用名称,不配置的话,会默认取 `spring.application.name` 的值
  15. # isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller;目前适用于springmvc/springcloud

客户端接入配置 - 图11

Consul方式注册配置

shenyu-admin配置

  • 首先在 pom.xml 文件中加入相关的依赖:
  1. <!-- apache shenyu consul register start-->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-register-server-consul</artifactId>
  5. <version>${project.version}</version>
  6. </dependency>
  7. <!-- apache shenyu consul register start -->
  8. <dependency>
  9. <groupId>com.ecwid.consul</groupId>
  10. <artifactId>consul-api</artifactId>
  11. <version>${consul.api.version}</version>
  12. </dependency>
  13. <!-- apache shenyu consul register end-->
  • yml文件配置注册中心为consul, consul的特有配置在props节点下进行配置, 配置信息如下:
  1. shenyu:
  2. register:
  3. registerType: consul
  4. serverLists: localhost:8500
  5. props:
  6. delay: 1
  7. wait-time: 55
  8. name: shenyuAdmin
  9. instanceId: shenyuAdmin
  10. hostName: localhost
  11. port: 8500
  12. tags: test1,test2
  13. preferAgentAddress: false
  14. enableTagOverride: false
  15. # registerType : 服务注册类型,填写 consul
  16. # serverLists: consul client agent地址(sidecar模式部署(单机或者集群),也可以是consul server agent的地址(只能连接一个consul server agent节点,如果是集群,那么会存在单点故障问题))
  17. # delay: 对Metadata的监控每次轮询的间隔时长,单位为秒,默认1秒
  18. # wait-time: 对Metadata的监控单次请求的等待时间(长轮询机制),单位为秒,默认55秒
  19. # instanceId: consul服务必填,consul需要通过instance-id找到具体服务
  20. # name 服务注册到consul时所在的组名
  21. # hostName: 为 consul 注册类型时,填写 注册服务实例的 地址, 该注册中心注册的服务实例地址,并不会用于客户端的调用,所以该配置可以不填,port,preferAgentAddress同理
  22. # port: 为 consul 注册类型时,填写 注册服务实例的 端口
  23. # tags: 对应consul配置中的tags配置
  24. # preferAgentAddress:使用consul客户端侧的agent对应的address作为注册服务实例的address,会覆盖hostName的手动配置
  25. # enableTagOverride:对应consul配置中的enableTagOverride配置

shenyu-client配置

下面展示的是springCloud服务作为客户端接入到Apache ShenYu网关时,通过Consul方式注册配置信息(springCloud服务本身的注册中心可以随意选择,与shenyu所选择的注册中心并不会存在冲突,example中使用的是eureka)。其他客户端接入时(DubboSpring Cloud等),配置方式同理。

  • 首先在 pom 文件中加入相关的依赖:
  1. <!-- apache shenyu consul register center -->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-register-client-consul</artifactId>
  5. <version>${shenyu.version}</version>
  6. </dependency>
  • 然后在 yml文件中配置注册方式为consul, 额外还需要配置shenyu.register.props, 配置信息如下:
  1. shenyu:
  2. register:
  3. registerType: consul
  4. serverLists: localhost:8500
  5. props:
  6. name: shenyuSpringCloudExample
  7. instanceId: shenyuSpringCloudExample
  8. hostName: localhost
  9. port: 8500
  10. tags: test1,test2
  11. preferAgentAddress: false
  12. enableTagOverride: false
  13. client:
  14. springCloud:
  15. props:
  16. contextPath: /springcloud
  17. port: 8884
  18. # registerType : 服务注册类型,填写 consul
  19. # serverLists: consul client agent地址(sidecar模式部署(单机或者集群),也可以是consul server agent的地址(只能连接一个consul server agent节点,如果是集群,那么会存在单点故障问题))
  20. # shenyu.client.props.port: 你本项目的启动端口,目前springmvc/tars/grpc需要进行填写
  21. # contextPath: 为你的这个mvc项目在shenyu网关的路由前缀, 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
  22. # appName:你的应用名称,不配置的话,会默认取 `spring.application.name` 的值
  23. # isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller;目前适用于springmvc
  24. # instanceId: consul服务必填,consul需要通过instance-id找到具体服务
  25. # name 服务注册到consul时所在的组名
  26. # hostName: 为 consul 注册类型时,填写 注册服务实例的 地址, 该注册中心注册的服务实例地址,并不会用于客户端的调用,所以该配置可以不填,port,preferAgentAddress同理
  27. # port: 为 consul 注册类型时,填写 注册服务实例的 端口
  28. # tags: 对应consul配置中的tags配置
  29. # preferAgentAddress:使用consul客户端侧的agent对应的address作为注册服务实例的address,会覆盖hostName的手动配置
  30. # enableTagOverride:对应consul配置中的enableTagOverride配置

Nacos方式注册配置

shenyu-admin配置

  • 首先在 pom 文件中加入相关的依赖(默认已经引入):
  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-register-server-nacos</artifactId>
  4. <version>${project.version}</version>
  5. </dependency>

客户端接入配置 - 图12

  • 然后在 yml文件中配置注册中心为nacos, 填写相关nacos服务地址和参数,还有nacos的命名空间(需要和shenyu-client保持一致),配置信息如下:
  1. shenyu:
  2. register:
  3. registerType: nacos
  4. serverLists : localhost:8848
  5. props:
  6. nacosNameSpace: ShenyuRegisterCenter

客户端接入配置 - 图13

shenyu-client配置

下面展示的是http服务作为客户端接入到Apache ShenYu网关时,通过Nacos方式注册配置信息。其他客户端接入时(DubboSpring Cloud等),配置方式同理。

  • 首先在 pom文件中加入相关的依赖:
  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-register-client-nacos</artifactId>
  4. <version>${shenyu.version}</version>
  5. </dependency>

客户端接入配置 - 图14

  • 然后在 yml 中配置注册方式为nacos, 并填写nacos服务地址和相关参数,还需要Nacos命名空间(需要和shenyu-admin端保持一致),IP(可不填,则自动获取本机ip)和端口,配置信息如下:
  1. shenyu:
  2. client:
  3. registerType: nacos
  4. serverLists: localhost:8848
  5. props:
  6. contextPath: /http
  7. appName: http
  8. port: 8188
  9. isFull: false
  10. nacosNameSpace: ShenyuRegisterCenter
  11. # registerType : 服务注册类型,填写 nacos
  12. # serverList: 为nacos注册类型时,填写nacos地址,多个地址用英文逗号分隔
  13. # port: 你本项目的启动端口,目前springmvc/tars/grpc需要进行填写
  14. # contextPath: 为你的这个mvc项目在shenyu网关的路由前缀,比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
  15. # appName:你的应用名称,不配置的话,会默认取 `spring.application.name` 的值
  16. # isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller;目前适用于springmvc/springcloud
  17. # nacosNameSpace: nacos的命名空间

客户端接入配置 - 图15

同时注册多种服务类型

以同时注册http和dubbo服务举例。 在yml参考如下配置即可:

  1. shenyu:
  2. register:
  3. registerType: nacos
  4. serverLists: localhost:8848
  5. client:
  6. http:
  7. props:
  8. contextPath: /http
  9. appName: http
  10. port: 8188
  11. isFull: false
  12. dubbo:
  13. props:
  14. contextPath: /dubbo
  15. appName: dubbo
  16. port: 28080
  17. props:
  18. nacosNameSpace: ShenyuRegisterCenter
  19. # registerType : 服务注册类型,填写 nacos
  20. # serverList: 为nacos注册类型时,填写nacos地址,多个地址用英文逗号分隔
  21. # http.port: 你本项目的启动Http端口,目前springmvc/SpringCloud需要进行填写
  22. # http.contextPath: 为你的这个mvc项目在shenyu网关的路由前缀,比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
  23. # http.appName:你的应用名称,不配置的话,会默认取 `spring.application.name` 的值
  24. # http.isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller;目前适用于springmvc/springcloud
  25. # dubbo.contextPath: 为你的项目中对应dubbo接口的contextPath
  26. # dubbo.port: dubbo服务端口
  27. # dubbo.appName: dubbo应用名称
  28. # nacosNameSpace: nacos的命名空间

总结,本文主要介绍了如何将你的微服务(当前支持HttpDubboSpring CloudgRPCMotanSofaTars等协议)接入到Apache ShenYu网关。介绍了注册中心的原理,Apache ShenYu网关支持的注册中心有HttpZookeeperEtcdConsulNacos等方式。介绍了以http服务作为客户端接入到Apache ShenYu网关时,使用不同方式注册配置信息。