学习dubbo直连方式遇到的报错,记录一下
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘userService’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userService’: FactoryBean threw exception on object creation; nested exception is com.alibaba.dubbo.rpc.RpcException: Fail to create remoting client for service(dubbo://localhost:20880/com.bjpowernode.service.UserService?application=02-link-consumer&codec=dubbo&dubbo=2.0.2&heartbeat=60000&interface=com.bjpowernode.service.UserService&methods=queryUserById&pid=10644®ister.ip=169.254.5.104&revision=1.0-SNAPSHOT&side=consumer×tamp=1649505593278): client(url: dubbo://localhost:20880/com.bjpowernode.service.UserService?application=02-link-consumer&codec=dubbo&dubbo=2.0.2&heartbeat=60000&interface=com.bjpowernode.service.UserService&methods=queryUserById&pid=10644®ister.ip=169.254.5.104&revision=1.0-SNAPSHOT&side=consumer×tamp=1649505593278) failed to connect to server localhost/127.0.0.1:20880, error message is:connection timed out
提供者的dubbo配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--服务提供者声明名称:必须保证服务名称的唯一性,它的名称是dubbo内部使用的唯一标识-->
<dubbo:application name="01-link-userservice-provier"/>
<!--访问服务协议的名称及端口号,dubbo官方推荐使用的是dubbo协议,端口号默认为20880-->
<!--
name:指定协议的名称
port:指定协议的端口号(默认为20880)
-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露接口
interface:暴露服务接口的全限定名称
ref:接口实现类在spring容器中的标识
registry:如果不使用注册中心,则值为N/A
-->
<dubbo:service interface="com.service.UserService" ref="userServiceImpl" registry="N/A"/>
<bean id="userServiceImpl" class="com.service.impl.UserServiceImpl"/>
</beans>
消费者的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--服务提供者声明名称:必须保证服务名称的唯一性,它的名称是dubbo内部使用的唯一标识-->
<dubbo:application name="02-link-consumer" />
<!--引用远程服务接口
id:远程服务接口的对象名称
interface:调用远程接口的全限定名称
url:访问服务器接口的地址
registry:不适用注册中心,值为:"N/A"
-->
<dubbo:reference id="userService"
interface="com.service.UserService"
url="dubbo://localhost:20880"
registry="N/A"/>
</beans>
消费者依赖提供者
需要提供者生成jar包到仓库
<dependency>
<groupId>com</groupId>
<artifactId>01-link-userservice-provier</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
提供者一个tomcat
消费者一个tomcat
报错原因:没有打开提供者的tomcat
解决方法:需要把提供者和消费者的tomcat按顺序开启