不能调用本地Dubbo服务测试原因

来源:互联网 发布:iphone 软件自动同步 编辑:程序博客网 时间:2024/05/16 07:29

Dubbo服务提供者,代码如下

package com.zhubajie.demo.schedule.DemoSchedule;import org.springframework.stereotype.Service;@com.alibaba.dubbo.config.annotation.Servicepublic class DemoScheduleImpl implements DemoSchedule {    @Override    public InvokeResult invoke(InvokeContext context){          ... // 业务实现    }}

通过注解式配置注册服务,代码如下

import com.alibaba.dubbo.config.ApplicationConfig;import com.alibaba.dubbo.config.ConsumerConfig;import com.alibaba.dubbo.config.RegistryConfig;import com.alibaba.dubbo.config.spring.AnnotationBean;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * dubbo消费者配置 * DubboConsumerConfig * @author weigang * @create 2017-08-18 **/@Configurationpublic class DubboConsumerConfig {    // 配置中心设置(公司内部使用)    // 可以直接在application.properties或者application.yml配置    @Value("${dubbo.registry.address}")    private String registryAddress;    /**     * 应用配置     *     * @return     */    @Bean    public ApplicationConfig applicationConfig() {        ApplicationConfig applicationConfig = new ApplicationConfig();        // 建议使用项目名称作为消费者或者提供者应用名称        applicationConfig.setName("current_project_name");        applicationConfig.setOwner("weigang");        applicationConfig.setOrganization("zbj");        return applicationConfig;    }    /**     * 消费者配置     *     * @return     */    @Bean    public ConsumerConfig consumerConfig() {        ConsumerConfig consumerConfig = new ConsumerConfig();        consumerConfig.setCheck(false);        consumerConfig.setTimeout(20000);        return consumerConfig;    }    /**     * zk地址     *     * @return     */    @Bean    public RegistryConfig registryConfig() {        RegistryConfig registryConfig = new RegistryConfig();        registryConfig.setAddress(registryAddress);        return registryConfig;    }    @Bean    public static AnnotationBean annotationBean() {        AnnotationBean annotationBean = new AnnotationBean();        // 设置读取包配置的路径        annotationBean.setPackage("com.zhubajie.***");        return annotationBean;    }

消费者Dubbo引用配置如下 文件名为 dubbo-consumber.xml

<?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-2.5.xsd    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <dubbo:reference interface="com.zhubajie.demo.schedule.DemoSchedule" id="demoSchedule" /></beans>

消费者注册地址配置如下 文件名为 dubbo.properties

# 消费者应用名称dubbo.application.name=current-project-name-consumer# 超时时间dubbo.reference.timeout=900000# 注册中心地址dubbo.registry.address=zookeeper://127.0.0.1:2181

备注: dubbo-consumber.xml和dubbo.properties可以合为一个xml配置,此处不作说明

消费者测试用例如下

package com.zhubajie.demo.schedule.dubbo;import com.zhubajie.schedule.InvokeContext;import com.zhubajie.demo.schedule.DemoSchedule;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author weigang * @create 2017-09-27 **/public class DemoScheduleTest {    private ApplicationContext context;    private DemoSchedule demoSchedule;    @Before    public void before(){        context = new ClassPathXmlApplicationContext("classpath:/dubbo-consumer.xml");        demoSchedule = context.getBean(DemoSchedule.class);    }    /**     * 请注释 spring-boot-devtools 再测试     */    @Test    public void testInvoke(){        DemoSchedule.invoke(new InvokeContext());    }}

跑测试用例,抛出如下异常(代理不能转换为真实的调用者)

这里写图片描述

这是因为 spring-boot-devtools 与 dubbo 冲突导致的,注释即可

    <!-- 本地通过dubbo消费者调用本地提供者的接口,需要注释如下依赖,有冲突 -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-devtools</artifactId>        <optional>true</optional>    </dependency>
原创粉丝点击