基于Dubbo的动态远程调用

基于Dubbo的动态远程调用

问题:为解决实际业务,由我方提供接口定义,具体的实现交给第三方处理。然后由第三方将开发好的服务注册到他们自己的Dubbo服务上,由我方调用。问题就在于多个第三方开发具体实现,对于我方而言如果按照配置方式切入调用是无法满足这种需求。所以找寻了dubbo的根据URL远程调用服务的机制。以下是Demo

关于Zookeeper的安装&配置此次不详细介绍了。

服务调用方

定义服务接口:

package com.demo.service;

/**
 * Desc:
 * Mail:v@terminus.io
 * author:Michael Zhao
 * Date:2015-03-04.
 */
public interface DemoService {
    public String deal(String someting);
}

定义Consumer配置文件:

<?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:application name="dubbo_consumer" />
</beans>

远程调用测试类:

package com.demo.test;

import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Desc:测试Dubbo实时映射bean
 * Mail:v@terminus.io
 * author:Michael Zhao
 * Date:2015-03-04.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/spring/root-context.xml"
})
@Slf4j
public class RealReference {
    //用于将bean关系注入到当前的context中
    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void realReference() {
        String url = "dubbo://localhost:21880/com.demo.service.DemoService";//更改不同的Dubbo服务暴露的ip地址&端口

        ReferenceBean<DemoService> referenceBean = new ReferenceBean<DemoService>();
        referenceBean.setApplicationContext(applicationContext);
        referenceBean.setInterface(com.demo.service.DemoService.class);
        referenceBean.setUrl(url);

        try {
            referenceBean.afterPropertiesSet();
            DemoService demoService = referenceBean.get();
            System.out.print(demoService.deal("Tester"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


服务提供方实现接口:

实现1:

package com.dubboT.service;

import com.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * Desc:Dubbo动态加载服务测试
 * Mail:v@terminus.io
 * author:Michael Zhao
 * Date:2015-03-04.
 */
@Service
@Slf4j
public class DemoServiceImpl implements DemoService {
    @Override
    public String deal(String s) {
        return "My Name is " + s;
    }
}

实现2:

package com.dubboT.service;

import com.demo.service.DemoService;

/**
 * Desc:
 * Mail:v@terminus.io
 * author:Michael Zhao
 * Date:2015-03-05.
 */
public class DemoServiceNImpl implements DemoService {
    @Override
    public String deal(String s) {
        return "This is DemoServiceNImpl " + s;
    }
}

分别在不同的端口暴露Service的实现

<?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  
        ">

    <bean id="demoService" class="com.rabbit.service.DemoServiceImpl" />

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubboProvider"  />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />

</beans>  

DemoServiceNImpl在21800端口暴露

<?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  
        ">

    <bean id="demoService" class="com.rabbit.service.DemoServiceNImpl" />

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubboProvider"  />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <dubbo:protocol name="dubbo" port="21880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />

</beans>  


分别提供服务

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Desc:
 * Mail:v@terminus.io
 * author:Michael Zhao
 * Date:2015-03-04.
 */
public class DubboProviderMain {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/rabbit-dubbo-provider.xml"});
        context.start();

        System.in.read();
    }
}

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Desc:
 * Mail:v@terminus.io
 * author:Michael Zhao
 * Date:2015-03-04.
 */
public class DubboProvider2Main {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/dubbo-provider.xml"});
        context.start();

        System.in.read();
    }
}


启动服务后,运行RealReference实现根据URL调用远程服务

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.
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
My Name is Tester
Process finished with exit code 0


  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值