Dubbo服务接入

说明

  • 此篇文章是介绍 dubbo 服务接入到 Apache ShenYu 网关,Apache ShenYu 网关使用 dubbo 插件来接入Dubbo服务。
  • 当前支持 alibaba dubbo(< 2.7.x) 以及 apache dubbo (>=2.7.x)
  • 接入前,请正确启动 shenyu-admin,并开启dubbo插件,在网关端和Dubbo服务端引入相关依赖。可以参考前面的 Dubbo快速开始

应用客户端接入的相关配置请参考:客户端接入配置

数据同步的相关配置请参考:数据同步配置

在网关中引入 dubbo 插件

  • 在网关的 pom.xml 文件中增加如下依赖:

    • alibaba dubbo 用户, dubbo 版本换成你的,引入你需要的注册中心依赖,以下是参考。

      1. <!-- apache shenyu alibaba dubbo plugin start-->
      2. <dependency>
      3. <groupId>org.apache.shenyu</groupId>
      4. <artifactId>shenyu-spring-boot-starter-plugin-alibaba-dubbo</artifactId>
      5. <version>${project.version}</version>
      6. </dependency>
      7. <!-- apache shenyu alibaba dubbo plugin end-->
      8. <dependency>
      9. <groupId>com.alibaba</groupId>
      10. <artifactId>dubbo</artifactId>
      11. <version>2.6.5</version>
      12. </dependency>
      13. <dependency>
      14. <groupId>org.apache.curator</groupId>
      15. <artifactId>curator-client</artifactId>
      16. <version>4.0.1</version>
      17. </dependency>
      18. <dependency>
      19. <groupId>org.apache.curator</groupId>
      20. <artifactId>curator-framework</artifactId>
      21. <version>4.0.1</version>
      22. </dependency>
      23. <dependency>
      24. <groupId>org.apache.curator</groupId>
      25. <artifactId>curator-recipes</artifactId>
      26. <version>4.0.1</version>
      27. </dependency>
    • apache dubbo 用户,dubbo 版本换成你的,引入你需要的注册中心依赖,如下是参考。

      ```

      org.apache.shenyu shenyu-spring-boot-starter-plugin-apache-dubbo ${project.version}

  1. <dependency>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo</artifactId>
  4. <version>2.7.5</version>
  5. </dependency>
  6. <!-- Dubbo Nacos registry dependency start -->
  7. <dependency>
  8. <groupId>org.apache.dubbo</groupId>
  9. <artifactId>dubbo-registry-nacos</artifactId>
  10. <version>2.7.5</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.alibaba.nacos</groupId>
  14. <artifactId>nacos-client</artifactId>
  15. <version>1.1.4</version>
  16. </dependency>
  17. <!-- Dubbo Nacos registry dependency end-->
  18. <!-- Dubbo zookeeper registry dependency start-->
  19. <dependency>
  20. <groupId>org.apache.curator</groupId>
  21. <artifactId>curator-client</artifactId>
  22. <version>4.0.1</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.apache.curator</groupId>
  26. <artifactId>curator-framework</artifactId>
  27. <version>4.0.1</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.apache.curator</groupId>
  31. <artifactId>curator-recipes</artifactId>
  32. <version>4.0.1</version>
  33. </dependency>
  34. <!-- Dubbo zookeeper registry dependency end -->
  35. ```
  • 重启网关服务。

dubbo 服务接入网关

可以参考:shenyu-examples-dubbo

  • alibaba dubbo 用户

如果是springboot构建,引入以下依赖:

  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-spring-boot-starter-client-alibaba-dubbo</artifactId>
  4. <version>${shenyu.version}</version>
  5. </dependency>

如果是spring构建,引入以下依赖:

  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-client-alibaba-dubbo</artifactId>
  4. <version>${shenyu.version}</version>
  5. </dependency>

并在你的 bean 定义的 xml 文件中新增如下 :

  1. <bean id ="alibabaDubboServiceBeanPostProcessor" class ="org.apache.shenyu.client.alibaba.dubbo.AlibabaDubboServiceBeanPostProcessor">
  2. <constructor-arg ref="shenyuRegisterCenterConfig"/>
  3. </bean>
  4. <bean id="shenyuRegisterCenterConfig" class="org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig">
  5. <property name="registerType" value="http"/>
  6. <property name="serverList" value="http://localhost:9095"/>
  7. <property name="props">
  8. <map>
  9. <entry key="contextPath" value="/你的contextPath"/>
  10. <entry key="appName" value="你的名字"/>
  11. <entry key="ifFull" value="false"/>
  12. </map>
  13. </property>
  14. </bean>
  • apache dubbo 用户

如果是springboot构建,引入以下依赖:

  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-spring-boot-starter-client-apache-dubbo</artifactId>
  4. <version>${shenyu.version}</version>
  5. </dependency>

如果是spring构建,引入以下依赖:

  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-client-apache-dubbo</artifactId>
  4. <version>${shenyu.version}</version>
  5. </dependency>

并在你的 bean 定义的 xml 文件中新增如下 :

  1. <bean id ="apacheDubboServiceBeanPostProcessor" class ="org.apache.shenyu.client.apache.dubbo.ApacheDubboServiceBeanPostProcessor">
  2. <constructor-arg ref="shenyuRegisterCenterConfig"/>
  3. </bean>
  4. <bean id="shenyuRegisterCenterConfig" class="org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig">
  5. <property name="registerType" value="http"/>
  6. <property name="serverList" value="http://localhost:9095"/>
  7. <property name="props">
  8. <map>
  9. <entry key="contextPath" value="/你的contextPath"/>
  10. <entry key="appName" value="你的名字"/>
  11. <entry key="ifFull" value="false"/>
  12. </map>
  13. </property>
  14. </bean>

dubbo 插件设置

  • 首先在 shenyu-admin 插件管理中,把dubbo 插件设置为开启。

  • 其次在 dubbo 插件中配置你的注册地址,或者其他注册中心的地址。

  1. {"register":"zookeeper://localhost:2181"} or {"register":"nacos://localhost:8848"}

接口注册到网关

  • dubbo 服务实现类的方法上加上 @ShenyuDubboClient 注解,表示该接口方法注册到网关。

  • 启动你的提供者,成功启动后,进入后台管理系统的插件列表 -> rpc proxy -> dubbo,会看到自动注册的选择器和规则信息。

dubbo用户请求及参数说明

可以通过 http 的方式来请求你的 dubbo 服务。Apache ShenYu 网关需要有一个路由前缀,这个路由前缀就是你接入项目进行配置 contextPath

比如你有一个 order服务 它有一个接口,它的注册路径 /order/test/save 现在就是通过 post 方式请求网关:http://localhost:9195/order/test/save 其中 localhost:9195 为网关的 ip 端口,默认端口是 9195 ,/order 是你 dubbo 接入网关配置的 contextPath

  • 参数传递:

    • 通过 http协议, post 方式访问网关,通过在http body中传入json类型参数。

    • 更多参数类型传递,可以参考 shenyu-examples-dubbo 中的接口定义,以及参数传递方式。

  • 单个 java bean参数类型(默认)

  • 多参数类型支持,在网关的yaml 配置中新增如下配置:

  1. shenyu:
  2. dubbo:
  3. parameter: multi
  • 自定义实现多参数支持:

    • 在你搭建的网关项目中,新增一个类 MyDubboParamResolveService,实现 org.apache.shenyu.web.dubbo.DubboParamResolveService接口。

      ``` public interface DubboParamResolveService {

  1. /**
  2. * Build parameter pair.
  3. * this is Resolve http body to get dubbo param.
  4. *
  5. * @param body the body
  6. * @param parameterTypes the parameter types
  7. * @return the pair
  8. */
  9. Pair<String[], Object[]> buildParameter(String body, String parameterTypes);
  10. }
  11. ```
  12. - `body` 为 `http` 中 `body` 传的 `json` 字符串。
  13. - `parameterTypes`: 匹配到的方法参数类型列表,如果有多个,则使用 `,` 分割。
  14. - `Pair` 中,`left` 为参数类型,`right` 为参数值,这是 `dubbo` 泛化调用的标准
  15. - 把你的类注册成 `Spring` 的 `bean`,覆盖默认的实现。
  16. ```
  17. @Bean
  18. public DubboParamResolveService myDubboParamResolveService() {
  19. return new MyDubboParamResolveService();
  20. }
  21. ```

服务治理

  • 标签路由

    • 请求时在 header 中添加 Dubbo_Tag_Route,并设置对应的值,之后当前请求就会路由到指定 tagprovider,只对当前请求有效。
  • 服务提供者直连

    • 设置 @ShenyuDubboClient 注解中的 url 属性。
    • 修改 Admin 控制台修改元数据内的 url 属性。
    • 对所有请求有效。
  • 参数验证和自定义异常

    • 指定 validation = "shenyuValidation"

    • 在接口中抛出 ShenyuException 时,异常信息会返回,需要注意的是显式抛出 ShenyuException

      ``` @Service(validation = “shenyuValidation”) public class TestServiceImpl implements TestService {

  1. @Override
  2. @ShenyuDubboClient(path = "/test", desc = "test method")
  3. public String test(@Valid HelloServiceRequest name) throws ShenyuException {
  4. if (true){
  5. throw new ShenyuException("Param binding error.");
  6. }
  7. return "Hello " + name.getName();
  8. }
  9. }
  10. ```
  11. - 请求参数
  12. ```
  13. public class HelloServiceRequest implements Serializable {
  14. private static final long serialVersionUID = -5968745817846710197L;
  15. @NotEmpty(message = "name cannot be empty")
  16. private String name;
  17. @NotNull(message = "age cannot be null")
  18. private Integer age;
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public Integer getAge() {
  26. return age;
  27. }
  28. public void setAge(Integer age) {
  29. this.age = age;
  30. }
  31. }
  32. ```
  33. - 发送请求
  34. ```
  35. {
  36. "name": ""
  37. }
  38. ```
  39. - 返回
  40. ```
  41. {
  42. "code": 500,
  43. "message": "Internal Server Error",
  44. "data": "name cannot be empty,age cannot be null"
  45. }
  46. ```
  47. - 当按照要求传递请求参数时,会返回自定义异常的信息
  48. ```
  49. {
  50. "code": 500,
  51. "message": "Internal Server Error",
  52. "data": "Param binding error."
  53. }
  54. ```

Http —> 网关 —> Dubbo Provider

实际上就是把 http 请求,转成 dubbo 协议,内部使用 dubbo 泛化来进行调用。 dubbo 服务在接入网关的时候,加上了 @ShenyuDubboClient 注解,并设置了 path 字段来指定请求路径。 然后在yml中配置了 contextPath

假如有一个这样的方法, contextPath 配置的是 /dubbo

  1. @Override
  2. @ShenyuDubboClient(path = "/insert", desc = "插入一条数据")
  3. public DubboTest insert(final DubboTest dubboTest) {
  4. return dubboTest;
  5. }

那么请求的路径为:http://localhost:9195/dubbo/insertlocalhost:9195是网关的地址,如果你更改了,这里也要改。

请求参数: DubboTest 是一个 javabean 对象,有 2 个字段,idname ,那么我们通过 body 中传递这个对象的 json 数据就好。

  1. {"id": "1234", "name": "XIAO5y"}

如果接口中,没有参数,那么body 传值为:

  1. {}

如果接口有很多个参数,请参考上面介绍过的多参数类型支持。