目录
一、SpringCloudAlibaba集成Dubbo、Nacos
一、SpringCloudAlibaba集成Dubbo、Nacos
集成常规操作这个博主已经写的很详细了:
Spring Cloud Alibaba学习(九):Dubbo集成_IcyDate的博客-CSDN博客_dubbo集成
不过个人推荐将扫描包的配置放到配置文件:
如:
服务提供者:
server:
port: 8881
spring:
cloud:
nacos:
server-addr: localhost:8848 # nacos的服务配置
discovery:
cluster-name: Service_cluster_1 #集群
group: Service_group_1 # 分组名
username: nacos # 用户名
password: nacos # 密码
namespace: public # 环境分类
application:
name: dubbo-provider
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost:8848
scan: #dubbo的服务路径
base-packages: com.wxl.service
服务消费者:多添加一个订阅提供者名称会更规范
server:
port: 8882
spring:
cloud:
nacos:
server-addr: localhost:8848 # nacos的服务配置
discovery:
cluster-name: Service_cluster_1 #集群
group: Service_group_1 # 分组名
username: nacos # 用户名
password: nacos # 密码
namespace: public # 环境分类
application:
name: dubbo-consumer
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost:8848
cloud:
subscribed-services: dubbo-provider #订阅的提供者名称
注意:先启动服务提供者,再启动服务消费者
二、版本问题解决
我用的是比较新的版本:Hoxton.SR12系列
按照该博主的操作,理论是没有问题的,但事实是新版本还是有一定的坑的,启动即报错如下:
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.<init>(ReferenceAnnotationBeanPostProcessor.java:106)
The following method did not exist:
org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.setClassValuesAsString(Z)V
The method's class, org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor, is available from the following locations:
jar:file:/D:/apache-maven-3.8.1/maven-repository/org/apache/dubbo/dubbo/2.7.13/dubbo-2.7.13.jar!/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.class
The class hierarchy was loaded from the following locations:
org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor: file:/D:/apache-maven-3.8.1/maven-repository/org/apache/dubbo/dubbo/2.7.13/dubbo-2.7.13.jar
com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor: file:/D:/apache-maven-3.8.1/maven-repository/com/alibaba/spring/spring-context-support/1.0.10/spring-context-support-1.0.10.jar
org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter: file:/D:/apache-maven-3.8.1/maven-repository/org/springframework/spring-beans/5.2.15.RELEASE/spring-beans-5.2.15.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor
这很明显是jar包冲突问题!!!但调了一天我也没解决该问题,人都麻了,后来终于找到了一篇官方回应该问题的文章顺利解决:
升级 2.2.7 报错 · Issue #2310 · alibaba/spring-cloud-alibaba · GitHub
这是官方的回应,解决方法很简单,就是添加一个配置:
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.11</version>
</dependency>
原本系列的是1.0.10但这里要顺利集成dubbo需要升级为1.0.11。
解决后运行,发现rpc调用正常: