在SpringCloud中会有多个微服务,各个服务之间如果要进行通信,要进行调用的话,使用的就是dubbo或者openfeign。
一、dubbo的使用大体步骤
1.生产方和消费方pom.xml引入依赖;
<!--常规依赖-->
<dependency>
<groupId>***.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
本案例项目使用的是下面的依赖包;
2.生产方和消费方启动类加入标注“@EnableDubbo”;
3.配置application.yml;(参考格式如下)
# 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖
dubbo:
application:
logger: slf4j
# 元数据中心 local 本地 remote 远程 这里使用远程便于其他服务获取
metadataType: remote
# 可选值 interface、instance、all,默认是 all,即接口级地址、应用级地址都注册
register-mode: instance
service-discovery:
# FORCE_INTERFACE,只消费接口级地址,如无地址则报错,单订阅 2.x 地址
# APPLICATION_FIRST,智能决策接口级/应用级地址,双订阅
# FORCE_APPLICATION,只消费应用级地址,如无地址则报错,单订阅 3.x 地址
migration: FORCE_APPLICATION
# 注册中心配置
registry:
address: nacos://${spring.cloud.nacos.server-addr}
group: DUBBO_GROUP
parameters:
namespace: ${spring.profiles.active}
# 消费者相关配置
consumer:
# 结果缓存(LRU算法)
# 会有数据不一致问题 建议在注解局部开启
cache: false
# 支持校验注解
validation: jvalidationNew
# 调用重试 不包括第一次 0为不需要重试
retries: 0
# 初始化检查
check: false
dubbo:
application:
# 关闭qos端口避免单机多生产者端口冲突 如需使用自行开启
qos-enable: false
protocol:
# 如需使用 Triple 3.0 新协议 可查看官方文档
# 使用 dubbo 协议通信
name: dubbo
# dubbo 协议端口(-1表示自增端口,从20880开始)
port: -1
# 指定dubbo协议注册ip
# host: 192.168.0.100
# 消费者相关配置
consumer:
# 超时时间
timeout: 3000
scan:
# 接口实现类扫描
base-packages: org.sharetek.**.dubbo
# 自定义配置
custom:
# 全局请求log
request-log: true
# info 基础信息 param 参数信息 full 全部
log-level: info
4.生产方对外公开服务实现类用注解"@DubboService"
5.Dubbo正常使用
二、错误场景演示及原因分析
{
"code": 500,
"msg": "Failed to invoke the method getSecurityUserInfo in the service org.sharetek.system.api.RemoteSecurityUserService. Tried 1 times of the providers [192.168.72.1:20880] (1/1) from the registry 127.0.0.1:8848 on the consumer 192.168.72.1 using the dubbo version 3.2.7. Last error is: Failed to invoke remote method: getSecurityUserInfo, provider: DefaultServiceInstance{serviceName='sharetek-system', host='192.168.72.1', port=20880, enabled=true, healthy=true, metadata={dubbo.endpoints=[{\"port\":20880,\"protocol\":\"dubbo\"}], dubbo.metadata.revision=2156754ed82fcb6bf50a8f846acf3287, dubbo.metadata.storage-type=remote, timestamp=1708243528944}}, service{name='org.sharetek.system.api.RemoteSecurityUserService',group='null',version='null',protocol='dubbo',port='20880',params={side=provider, release=3.2.7, methods=getSecurityUserInfo, logger=slf4j, deprecated=false, dubbo=2.0.2, interface=org.sharetek.system.api.RemoteSecurityUserService, service-name-mapping=true, generic=false, metadata-type=remote, application=sharetek-system, prefer.serialization=fastjson2,hessian2, dynamic=true},}, cause: org.apache.dubbo.remoting.RemotingException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.sharetek.***mon.dubbo.properties.DubboCustomProperties' available\r\norg.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.sharetek.***mon.dubbo.properties.DubboCustomProperties' available\r\n\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:341)\r\n\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)\r\n\tat ***.hutool.extra.spring.SpringUtil.getBean(SpringUtil.java:122)\r\n\tat org.sharetek.***mon.dubbo.filter.DubboRequestFilter.invoke(DubboRequestFilter.java:24)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.ClassLoaderCallbackFilter.invoke(ClassLoaderCallbackFilter.java:38)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat ***.alibaba.dubbo.rpc.Invoker$***patibleInvoker.invoke(Invoker.java:76)\r\n\tat io.seata.integration.dubbo.alibaba.AlibabaDubboTransactionPropagationFilter.invoke(AlibabaDubboTransactionPropagationFilter.java:45)\r\n\tat ***.alibaba.dubbo.rpc.Filter.invoke(Filter.java:34)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:80)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:45)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat ***.alibaba.csp.sentinel.adapter.dubbo3.SentinelDubboProviderFilter.invoke(SentinelDubboProviderFilter.java:81)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.seata.SeataTransactionPropagationProviderFilter.invoke(SeataTransactionPropagationProviderFilter.java:66)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:108)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:55)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.A***essLogFilter.invoke(A***essLogFilter.java:119)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.metrics.observation.ObservationReceiverFilter.invoke(ObservationReceiverFilter.java:56)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:206)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat ***.dev33.satoken.context.dubbo3.filter.SaTokenDubbo3ProviderFilter.invoke(SaTokenDubbo3ProviderFilter.java:47)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:54)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.metrics.filter.MetricsFilter.invoke(MetricsFilter.java:76)\r\n\tat org.apache.dubbo.metrics.filter.MetricsProviderFilter.invoke(MetricsProviderFilter.java:36)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.ProfilerServerFilter.invoke(ProfilerServerFilter.java:64)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:144)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:334)\r\n\tat org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CallbackRegistrationInvoker.invoke(FilterChainBuilder.java:196)\r\n\tat org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:156)\r\n\tat org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:110)\r\n\tat org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:200)\r\n\tat org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:53)\r\n\tat org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:62)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)\r\n\tat org.apache.dubbo.***mon.threadlocal.InternalRunnable.run(InternalRunnable.java:41)\r\n\tat java.base/java.lang.Thread.run(Thread.java:842)\r\n",
"data": null
}
错因分析:
1.生产方和消费方必须在同一个命名空间,同一分组,同一个集群;(好像不是这个原因导致,只生效过一次)
2.redis中存在脏数据,需要清理;(这个的可能性比较大,开始一直掉不通,后来一直好着)
// redis客户端执行
del keys *
flushall