Dubbo快速开始

本文档演示如何将Dubbo服务接入到Apache ShenYu网关。您可以直接在工程下找到本文档的示例代码

环境准备

请参考运维部署的内容,选择一种方式启动shenyu-admin。比如,通过 本地部署 启动Apache ShenYu后台管理系统。

启动成功后,需要在基础配置->插件管理中,把dubbo 插件设置为开启,并设置你的注册地址,请确保注册中心在你本地已经开启。

Dubbo快速开始 - 图1

启动网关,如果是通过源码的方式,直接运行shenyu-bootstrap中的ShenyuBootstrapApplication

注意,在启动前,请确保网关已经引入相关依赖。

如果客户端是apache dubbo,注册中心使用zookeeper,请参考如下配置:

  1. <!-- apache shenyu apache dubbo plugin start-->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-spring-boot-starter-plugin-apache-dubbo</artifactId>
  5. <version>${project.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.dubbo</groupId>
  9. <artifactId>dubbo</artifactId>
  10. <version>2.7.5</version>
  11. </dependency>
  12. <!-- Dubbo zookeeper registry dependency start -->
  13. <dependency>
  14. <groupId>org.apache.curator</groupId>
  15. <artifactId>curator-client</artifactId>
  16. <version>4.0.1</version>
  17. <exclusions>
  18. <exclusion>
  19. <artifactId>log4j</artifactId>
  20. <groupId>log4j</groupId>
  21. </exclusion>
  22. </exclusions>
  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. <!-- apache shenyu apache dubbo plugin end-->

如果客户端是alibaba dubbo,注册中心使用zookeeper,请参考如下配置:

  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. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>dubbo</artifactId>
  10. <version>${alibaba.dubbo.version}</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.curator</groupId>
  14. <artifactId>curator-client</artifactId>
  15. <version>${curator.version}</version>
  16. <exclusions>
  17. <exclusion>
  18. <artifactId>log4j</artifactId>
  19. <groupId>log4j</groupId>
  20. </exclusion>
  21. </exclusions>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.apache.curator</groupId>
  25. <artifactId>curator-framework</artifactId>
  26. <version>${curator.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.apache.curator</groupId>
  30. <artifactId>curator-recipes</artifactId>
  31. <version>${curator.version}</version>
  32. </dependency>
  33. <!-- apache shenyu alibaba dubbo plugin end-->

运行shenyu-examples-dubbo项目

下载 shenyu-examples-dubbo .

修改 spring-dubbo.xml 中的注册地址为你本地(注意区分dubbo的版本是apache dubbo还是alibaba dubbo),如:

  1. <dubbo:registry address="zookeeper://localhost:2181"/>

运行相应的main方法启动项目,(注意区分dubbo的版本是apache dubbo还是alibaba dubbo)。

成功启动会有如下日志:

  1. 2021-02-06 20:58:01.807 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/insert","pathDesc":"Insert a row of data","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboTestService","methodName":"insert","ruleName":"/dubbo/insert","parameterTypes":"org.dromara.shenyu.examples.dubbo.api.entity.DubboTest","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  2. 2021-02-06 20:58:01.821 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findAll","pathDesc":"Get all data","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboTestService","methodName":"findAll","ruleName":"/dubbo/findAll","parameterTypes":"","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  3. 2021-02-06 20:58:01.833 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findById","pathDesc":"Query by Id","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboTestService","methodName":"findById","ruleName":"/dubbo/findById","parameterTypes":"java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  4. 2021-02-06 20:58:01.844 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByListId","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByListId","ruleName":"/dubbo/findByListId","parameterTypes":"java.util.List","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  5. 2021-02-06 20:58:01.855 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByIdsAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByIdsAndName","ruleName":"/dubbo/findByIdsAndName","parameterTypes":"java.util.List,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  6. 2021-02-06 20:58:01.866 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSave","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSave","ruleName":"/dubbo/batchSave","parameterTypes":"java.util.List","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  7. 2021-02-06 20:58:01.876 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByArrayIdsAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByArrayIdsAndName","ruleName":"/dubbo/findByArrayIdsAndName","parameterTypes":"[Ljava.lang.Integer;,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  8. 2021-02-06 20:58:01.889 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTestAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTestAndName","ruleName":"/dubbo/saveComplexBeanTestAndName","parameterTypes":"org.dromara.shenyu.examples.dubbo.api.entity.ComplexBeanTest,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  9. 2021-02-06 20:58:01.901 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSaveAndNameAndId","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSaveAndNameAndId","ruleName":"/dubbo/batchSaveAndNameAndId","parameterTypes":"java.util.List,java.lang.String,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  10. 2021-02-06 20:58:01.911 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTest","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTest","ruleName":"/dubbo/saveComplexBeanTest","parameterTypes":"org.dromara.shenyu.examples.dubbo.api.entity.ComplexBeanTest","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}
  11. 2021-02-06 20:58:01.922 INFO 3724 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByStringArray","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByStringArray","ruleName":"/dubbo/findByStringArray","parameterTypes":"[Ljava.lang.String;","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true}

注意:当您需要同时暴露多个协议时,请不要配置shenyu.client.dubbo.props.port

测试

shenyu-examples-dubbo项目成功启动之后会自动把加 @ShenyuDubboClient 注解的接口方法注册到网关。

打开插件列表 -> rpc proxy -> dubbo可以看到插件规则配置列表:

Dubbo快速开始 - 图2

下面使用postman模拟http的方式来请求你的dubbo服务:

Dubbo快速开始 - 图3

复杂多参数示例:对应接口实现类为org.apache.shenyu.examples.alibaba.dubbo.service.impl.DubboMultiParamServiceImpl#batchSaveAndNameAndId

  1. @Override
  2. @ShenyuDubboClient(path = "/batchSaveAndNameAndId")
  3. public DubboTest batchSaveAndNameAndId(List<DubboTest> dubboTestList, String id, String name) {
  4. DubboTest test = new DubboTest();
  5. test.setId(id);
  6. test.setName("hello world shenyu alibaba dubbo param batchSaveAndNameAndId :" + name + ":" + dubboTestList.stream().map(DubboTest::getName).collect(Collectors.joining("-")));
  7. return test;
  8. }

Dubbo快速开始 - 图4

当你的参数不匹配时会报如下异常:

  1. 2021-02-07 22:24:04.015 ERROR 14860 --- [:20888-thread-3] o.d.shenyu.web.handler.GlobalErrorHandler : [e47b2a2a] Resolved [ShenyuException: org.apache.dubbo.remoting.RemotingException: java.lang.IllegalArgumentException: args.length != types.length
  2. java.lang.IllegalArgumentException: args.length != types.length
  3. at org.apache.dubbo.common.utils.PojoUtils.realize(PojoUtils.java:91)
  4. at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:82)
  5. at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:81)
  6. at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
  7. at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:81)
  8. at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41)
  9. at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:81)
  10. at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:150)
  11. at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100)
  12. at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175)
  13. at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
  14. at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57)
  15. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  16. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  17. at java.lang.Thread.run(Thread.java:748)
  18. ] for HTTP POST /dubbo/batchSaveAndNameAndId