Dubbo异步调用

24 篇文章 0 订阅
21 篇文章 0 订阅

dubbo提供基于NIO的非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小。
消费提供者代码:

package com.yncp.dubbo.service;

public interface IDubboAnsycService {
    public int add(Integer x, Integer y);

    public int multi(Integer x, Integer y);

}
package com.yncp.dubbo.service.impl;

import com.yncp.dubbo.service.IDubboAnsycService;

public class DubboAnsycServiceImpl implements IDubboAnsycService {

    public int add(Integer x, Integer y) { 
        try{
            Thread.sleep(3000);
        }catch (Exception e) { 
            e.printStackTrace();
        }
        return x+y;
    }

    public int multi(Integer x, Integer y) { 
        try{
            Thread.sleep(5000);
        }catch (Exception e) { 
            e.printStackTrace();
        }
        return x*y;
    }


}

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://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">

    <!-- 指定web服务名字 -->
    <dubbo:application name="DubboAnsyc"/>
    <!-- 声明服务注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!-- 指定传输层通信协议 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 暴露你的服务地址 -->
    <dubbo:service 
        ref="dubboAnsycService" 
        interface="com.yncp.dubbo.service.IDubboAnsycService"
        protocol="dubbo"
    />
 </beans>

消费者测试:

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.rpc.RpcContext;
import com.yncp.dubbo.service.IDubboAnsycService;


public class DubboStart {
    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        IDubboAnsycService demoService=(IDubboAnsycService) ctx.getBean("dubboAnsycService");
        long start=System.currentTimeMillis();
        demoService.add(1, 2);
        Future<Integer> sumRes = RpcContext.getContext().getFuture();
        demoService.multi(12, 13);
        Future<Integer> multiRes = RpcContext.getContext().getFuture();
        System.out.println("sum:"+sumRes.get()+",multi:"+multiRes.get());
        long end=System.currentTimeMillis();
        System.out.println("总共耗时:"+(end-start)/1000+"秒");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值