dubbo服务中在consumer中通过autowired方式注入bean失败

dubbo服务中生产者我已经跑起来了,但在consumer中通过autowired方式注入bean失败,网上找了很久以下三点我确定已经做了
1.在实现类上加入@Service
2.在配置文件中加入scan扫描
下面代码我只要注释掉这个UserService的Autowired就能跑起来,感觉就是这个语句的问题,但是不注入UserService就无法调用getUserAddressList,感觉没搭成功,大神帮忙看看,困扰我好久了,谢谢啊

package com.atguigu.gmall.service.impl;

import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;
import com.atguigu.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    UserService userService;

    @Override
    public void initOrder(String id){
        //查询用户收货地址
        System.out.printf(id);
        List<UserAddress> addressList =  userService.getUserAddressList("1");
        System.out.println(addressList);
    }
}

消费者配置文件如下

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:component-scan base-package="com.atguigu.gmall.service.impl"></context:component-scan>


    <dubbo:application name="user-service-consumer"></dubbo:application>


    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>


    <!-- 声明需要调用远程服务的接口 -->
    <dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService"></dubbo:reference>

</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://dubbo.apache.org/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="user-service-provider"></dubbo:application>

    <!-- 2、指定注册中心的位置 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>

    <!-- 3、指定通信规则(通信协议?通信端口) -->
    <dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>

    <!-- 4、暴露服务   ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="com.atguigu.gmall.service.UserService"
                   ref="userServiceImpl" timeout="1000" version="1.0.0">
        <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
    </dubbo:service>

    <!--统一设置服务提供方的规则  -->
    <dubbo:provider timeout="1000"></dubbo:provider>


    <!-- 服务的实现 -->
    <bean id="userServiceImpl" class="com.atguigu.gmall.service.impl.UserServiceImpl"></bean>




    <!-- 连接监控中心 -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>

</beans>

具体报错如下

F:\Java\jdk1.8.0_241\bin\java.exe "-javaagent:F:\Java\IntelliJ IDEA 2019.3.4\lib\idea_rt.jar=6493:F:\Java\IntelliJ IDEA 2019.3.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Java\jdk1.8.0_241\jre\lib\charsets.jar;F:\Java\jdk1.8.0_241\jre\lib\deploy.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\cldrdata.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\dnsns.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\jaccess.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\jfxrt.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\localedata.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\nashorn.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\sunec.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\sunjce_provider.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\sunmscapi.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\sunpkcs11.jar;F:\Java\jdk1.8.0_241\jre\lib\ext\zipfs.jar;F:\Java\jdk1.8.0_241\jre\lib\javaws.jar;F:\Java\jdk1.8.0_241\jre\lib\jce.jar;F:\Java\jdk1.8.0_241\jre\lib\jfr.jar;F:\Java\jdk1.8.0_241\jre\lib\jfxswt.jar;F:\Java\jdk1.8.0_241\jre\lib\jsse.jar;F:\Java\jdk1.8.0_241\jre\lib\management-agent.jar;F:\Java\jdk1.8.0_241\jre\lib\plugin.jar;F:\Java\jdk1.8.0_241\jre\lib\resources.jar;F:\Java\jdk1.8.0_241\jre\lib\rt.jar;F:\Java\IdeaProjects\LearnDubbo\user-service-consumer\target\classes;F:\Java\IdeaProjects\LearnDubbo\gmall-interface\target\classes;F:\apache-maven-3.6.3\myrepo\com\alibaba\dubbo\2.6.2\dubbo-2.6.2.jar;F:\apache-maven-3.6.3\myrepo\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;F:\apache-maven-3.6.3\myrepo\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;F:\apache-maven-3.6.3\myrepo\org\apache\curator\curator-framework\2.12.0\curator-framework-2.12.0.jar;F:\apache-maven-3.6.3\myrepo\org\apache\curator\curator-client\2.12.0\curator-client-2.12.0.jar;F:\apache-maven-3.6.3\myrepo\org\apache\zookeeper\zookeeper\3.4.8\zookeeper-3.4.8.jar;F:\apache-maven-3.6.3\myrepo\log4j\log4j\1.2.16\log4j-1.2.16.jar;F:\apache-maven-3.6.3\myrepo\jline\jline\0.9.94\jline-0.9.94.jar;F:\apache-maven-3.6.3\myrepo\io\netty\netty\3.7.0.Final\netty-3.7.0.Final.jar;F:\apache-maven-3.6.3\myrepo\com\google\guava\guava\16.0.1\guava-16.0.1.jar;F:\apache-maven-3.6.3\myrepo\org\slf4j\slf4j-api\1.7.6\slf4j-api-1.7.6.jar;F:\apache-maven-3.6.3\myrepo\org\springframework\spring-context\4.3.16.RELEASE\spring-context-4.3.16.RELEASE.jar;F:\apache-maven-3.6.3\myrepo\org\springframework\spring-aop\4.3.16.RELEASE\spring-aop-4.3.16.RELEASE.jar;F:\apache-maven-3.6.3\myrepo\org\springframework\spring-beans\4.3.16.RELEASE\spring-beans-4.3.16.RELEASE.jar;F:\apache-maven-3.6.3\myrepo\org\springframework\spring-core\4.3.16.RELEASE\spring-core-4.3.16.RELEASE.jar;F:\apache-maven-3.6.3\myrepo\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;F:\apache-maven-3.6.3\myrepo\org\springframework\spring-expression\4.3.16.RELEASE\spring-expression-4.3.16.RELEASE.jar;F:\Java\IdeaProjects\LearnDubbo\user-service-provider\target\classes com.atguigu.gmall.service.MainApplication
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'OrderService': 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 java.lang.IllegalStateException: Failed to check the status of the service com.atguigu.gmall.service.UserService. No provider available for the service com.atguigu.gmall.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-consumer&dubbo=2.6.2&interface=com.atguigu.gmall.service.UserService&methods=getUserAddressList&pid=9012&register.ip=192.168.1.4&side=consumer&timestamp=1587052183628 to the consumer 192.168.1.4 use dubbo version 2.6.2
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.atguigu.gmall.service.MainApplication.main(MainApplication.java:11)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.atguigu.gmall.service.UserService. No provider available for the service com.atguigu.gmall.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-consumer&dubbo=2.6.2&interface=com.atguigu.gmall.service.UserService&methods=getUserAddressList&pid=9012&register.ip=192.168.1.4&side=consumer&timestamp=1587052183628 to the consumer 192.168.1.4 use dubbo version 2.6.2
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:177)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1640)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1316)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1282)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 15 more
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.atguigu.gmall.service.UserService. No provider available for the service com.atguigu.gmall.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-consumer&dubbo=2.6.2&interface=com.atguigu.gmall.service.UserService&methods=getUserAddressList&pid=9012&register.ip=192.168.1.4&side=consumer&timestamp=1587052183628 to the consumer 192.168.1.4 use dubbo version 2.6.2
    at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422)
    at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333)
    at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163)
    at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:66)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:170)
    ... 25 more

4个回答

1.看一下provider启动有没有报错。
2.可以提供者消费者都加一个version,看一下能不能找到

qq_38833870
Hikaru0704 确实时provider中配置了暴露服务的版本号,但是在consumer中没有配置导致没消费失败,并不是autowire的问题,一开始方向就错了,感谢提供思路
4 个月之前 回复

Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.atguigu.gmall.service.UserService. No provider available for the service com.atguigu.gmall.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-consumer&dubbo=2.6.2&interface=com.atguigu.gmall.service.UserService&methods=getUserAddressList&pid=9012&register.ip=192.168.1.4&side=consumer&timestamp=1587052183628 to the consumer 192.168.1.4 use dubbo version 2.6.2

你这一坨服务没有启来,单个测试下看看

No provider available for the service com.atguigu.gmall.service.UserService。
provider端先启动了吗

你的客户端和服务端的服务版本号不一致,即使你起来了也调用不到。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐