Springboot集成原生Dubbo 实战:
1、pom文件添加新的jar
<!--Dubbo服务-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.7</version>
<type>pom</type>
</dependency>
2、配置文件配置:
dubbo:
application:
name: nd-task-dubbo-prod #如果指定了spring应用名称,可以缺省dubbo的应用名称,这2个至少要配置1个。缺省dubbo的应用名称时默认值是spring的应用名称
qosEnable: true
qosPort: 9881
qosAcceptForeignIp: false
registry:
protocol: zookeeper
address: 127.0.8.21:2181,127.0.8.36:2181,127.0.8.49:2181 #注册中心地址
#port:2181 #端口号可以写在address中,也可以单独写。实质是从address中获取的port是null,后面设置的port覆盖了null
protocol:
name: dubbo #dubbo使用的协议、端口
port: 9462
consumer:
timeout: 30000 #超时时间设置
check: false #启动时不检查
3、默认超时时间是一秒,实际环境需求重新设置,否则在网络环境不好的情况下,容易连接调用超时,报错参考:
org.apache.dubbo.rpc.RpcException: Failed to invoke the method uploadFile in the service com.nandao.fileservice.api.CommonFileService. Tried 3 times of the providers [127.0.0.93:9700, 127.0.0.144:9700, 127.0.0.23:9700] (3/3) from the registry 127.0.0.21:2181 on the consumer 172.33.88.19 using the dubbo version 2.7.7. Last error is: Invoke remote method timeout. method: uploadFile, provider: dubbo://127.0.0.144:9700/com.nandao.fileservice.api.CommonFileService?anyhost=true&application=nd-task-dubbo-prod&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&init=false&interface=com.nandao.fileservice.api.CommonFileService&methods=uploadFile,getOSSPath&pid=1&qos.accept.foreign.ip=false&qos.enable=true&qos.port=9461®ister.ip=127.0.8.29&release=2.7.7&remote.application=dubbo-file-consumer&side=consumer&sticky=false×tamp=1645380466832, cause: org.apache.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2021-02-22 18:54:03.461, end time: 2021-02-22 18:54:04.480, client elapsed: 0 ms, server elapsed: 1019 ms, timeout: 1000 ms, request: Request [id=338, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=uploadFile, parameterTypes=[interface java.util.List, class java.lang.String, class java.lang.String, class java.lang.String], arguments=[[https://img.jinse.cn/jinse_1645526721493881230_image3.png], 20210222x6Z8zlYL, news, news], attachments={path=com.nandao.fileservice.api.CommonFileService, remote.application=nd-task-dubbo-prod, interface=com.nandao.fileservice.api.CommonFileService, version=0.0.0}]], channel: /127.0.8.29:37888 -> /127.0.0.144:9700 transId 20210222x6Z8zlYL url https://il.jinse.cn/jinse_1645526721493881230_image3.png app:nd-new-service-prod logger:c.h.c.n.s.u.ArticleUtil build_name:NOT_SET @timestamp:Feb 22, 2021 @ 18:54:04.491 tags: captain_gen:NOT_SET build_timestamp:NOT_SET date:2021-02-22 18:54:04.481 captain_seq:NOT_SET app_name:NOT_SET stack_trace: profile:prod thread:scheduling-1 build_git_version:NOT_SET level:ERROR env_name:NOT_SET hostname:NOT_SET @version:1 build_git_hash:NOT_SET _id:6f0RIX8BHFUm8KXkHp3k _type:_doc _index:applog-im-2021.02.19.21-000017 _score: -
4、有篇文章和我出现的类似:参考
5、另外一种异常:
Caused by: java.util.concurrent.ExecutionException: org.apache.dubbo.remoting.RemotingException: Data length too large: 15557929, max payload: 8388608, channel: NettyChannel
产生原因:接口返回数据量太大,Dubbo接口最大限制目前为8M。
解决办法:分批进行调取Dubbo接口,然后拼接起来再返回。