连接失败

连接失败

问题描述:

producer模块可以启动成功也可以连接,但是消费者却连接失败

相关代码:

package com.yada.consumer.conreoller;


import com.yada.consumer.entity.CourseAndPrice;
import com.yada.consumer.entity.CoursePrice;
import com.yada.consumer.service.CoursePriceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 概述:     CoursePrice控制器
 */
@RestController
public class CoursePriceController {

    @Resource
    private CoursePriceService coursePriceService;


    /**
     * 根据图书id获取对应的图书价格
     * @param courseId
     * @return
     */
    @GetMapping("/price")
    public Integer getCoursePrice(Integer courseId) {
        CoursePrice coursePrice = coursePriceService.getCoursePrice(courseId);
        return coursePrice.getPrice();

    }


    /**
     * 获取图书列表和对应价格
     * @return
     */
    @GetMapping("/coursesIAndPrice")
    public List<CourseAndPrice> getCoursesIAndPrice(){
        List<CourseAndPrice> courseAndPriceList = coursePriceService.getCourseAndPrice();
        return courseAndPriceList;
    }




}

相关代码:

package com.yada.consumer.service.impl;


import com.yada.consumer.dao.CoursePriceMapper;
import com.yada.consumer.entity.CourseAndPrice;
import com.yada.consumer.entity.CoursePrice;
import com.yada.consumer.service.CoursePriceService;
import com.yada.producer.eneity.Course;
import com.yada.producer.service.CourseListService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 概述:     CoursePrice服务接口实现类
 */
@Service
public class CoursePriceServiceImpl implements CoursePriceService {

    @Resource
    private CoursePriceMapper coursePriceMapper;

    //用这个注解直接引入需要模块的服务接口,也是在pom文件添加moudle依赖
    @Reference(version = "${demo.service.version}")
    CourseListService courseListService;


    /**
     * 根据图书Id查询出图书价格
     * @param courseId
     * @return
     */
    @Override
    public CoursePrice getCoursePrice(Integer courseId) {
        CoursePrice coursePrice = coursePriceMapper.findCoursePrice(courseId);
        return coursePrice;
    }

    /**
     * 获取图书列表和对应图书的价格
     * @return
     */
    @Override
    public List<CourseAndPrice> getCourseAndPrice(){
        List<CourseAndPrice> courseAndPriceList = new ArrayList<>();
        //先通过FeignClient拿到图书列表
        List<Course> courseList = courseListService.getCourseList();
        for (int i = 0; i < courseList.size(); i++) {
            Course course = courseList.get(i);
            if (course != null){
                CoursePrice coursePrice = getCoursePrice(course.getCourseId());

                CourseAndPrice courseAndPrice = new CourseAndPrice();
                courseAndPrice.setPrice(coursePrice.getPrice());
                courseAndPrice.setId(course.getId());
                courseAndPrice.setCourseId(course.getCourseId());
                courseAndPrice.setName(course.getName());
                courseAndPriceList.add(courseAndPrice);
            }
        }
        return courseAndPriceList;
    }

}

相关代码:

server.port=8084
spring.application.name=course-price
demo.service.version=1.0.0

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring_cloud?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=yd515628


logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}

#dubbo协议
dubbo.protocol.name=dubbo
#端口号可以随机
dubbo.protocol.port=-1
#dubbo注册
dubbo.registry.address=zookeeper://192.168.10.137:2181
dubbo.registry.file=${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache

问题描述:控制台错误

相关代码:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
16:12:44.706 -ERROR 18564 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coursePriceController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coursePriceServiceImpl': Injection of @Reference dependencies is failed; nested exception is java.lang.IllegalStateException: zookeeper not connected
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:325) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:847) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE]
    at com.yada.consumer.ConsumerApplication.main(ConsumerApplication.java:9) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coursePriceServiceImpl': Injection of @Reference dependencies is failed; nested exception is java.lang.IllegalStateException: zookeeper not connected
    at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.postProcessPropertyValues(AnnotationInjectedBeanPostProcessor.java:150) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1409) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1255) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1175) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:521) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:497) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:637) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:322) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.IllegalStateException: zookeeper not connected
    at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:83) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.createZookeeperClient(CuratorZookeeperTransporter.java:26) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter.connect(AbstractZookeeperTransporter.java:68) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfiguration.<init>(ZookeeperDynamicConfiguration.java:62) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfigurationFactory.createDynamicConfiguration(ZookeeperDynamicConfigurationFactory.java:37) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.configcenter.AbstractDynamicConfigurationFactory.getDynamicConfiguration(AbstractDynamicConfigurationFactory.java:33) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.AbstractInterfaceConfig.getDynamicConfiguration(AbstractInterfaceConfig.java:315) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.AbstractInterfaceConfig.prepareEnvironment(AbstractInterfaceConfig.java:290) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.AbstractInterfaceConfig.startConfigCenter(AbstractInterfaceConfig.java:280) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.AbstractInterfaceConfig.lambda$null$7(AbstractInterfaceConfig.java:636) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at java.util.Optional.orElseGet(Optional.java:267) ~[na:1.8.0_301]
    at org.apache.dubbo.config.AbstractInterfaceConfig.lambda$useRegistryForConfigIfNecessary$8(AbstractInterfaceConfig.java:620) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at java.util.Optional.ifPresent(Optional.java:159) ~[na:1.8.0_301]
    at org.apache.dubbo.config.AbstractInterfaceConfig.useRegistryForConfigIfNecessary(AbstractInterfaceConfig.java:618) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.AbstractInterfaceConfig.checkRegistry(AbstractInterfaceConfig.java:208) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:378) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:329) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:250) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.getOrCreateProxy(ReferenceAnnotationBeanPostProcessor.java:246) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:143) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.getInjectedObject(AnnotationInjectedBeanPostProcessor.java:359) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement.inject(AnnotationInjectedBeanPostProcessor.java:539) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
    at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.postProcessPropertyValues(AnnotationInjectedBeanPostProcessor.java:146) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    ... 33 common frames omitted
Caused by: java.lang.IllegalStateException: zookeeper not connected
    at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:80) ~[dubbo-2.7.4.1.jar:2.7.4.1]
    ... 57 common frames omitted


Process finished with exit code 1



正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕小脸 2021-12-03 11:17:15

同学你好,建议同学在dubbo.registry.address配置中设置一下timeout

如下:

dubbo.registry.address=zookeeper://127.0.0.1:2181?timeout=6000

https://img1.sycdn.imooc.com//climg/61a98c2009dd05f806700062.jpg

祝学习愉快~

好帮手慕小尤 2021-12-02 18:31:30

同学你好,1、测试同学代码是没有问题的,建议同学查看producer模块是否有成功开启。如下所示:

https://img1.sycdn.imooc.com//climg/61a89a8f090cb4c114450559.jpg

2、同学也可以查看producer模块的启动窗口中是否存在异常。

祝学习愉快!

  • 提问者 杨清川 #1
    开启了,producer模块可以正常开启,就这个模块连接不上Zookeeper
    2021-12-02 18:32:57
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师