Dubbo异步调用

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

在这里插入图片描述

dubbo以2.5.x为版本

provider端
注意配置文件中的 timeout=“10000” async=“true”
timeout=“10000” 表示等待10秒,provider中两个服务提供分别沉睡了2s和4s,如果不设置timeout,默认1秒,这就会报错(下图)。

dubbo超时机制优先级:
客户端方法级>服务端方法级>客户端接口级>服务端接口级>客户端全局>服务端全局
若xml设置5s,get设置2s则以get为准
若xml设置2s,get设置5s则以xml为准
若get不设置,默认以xml为准。
若全不设置,默认1000ms。

在这里插入图片描述

public interface ServiceAPI {

    String sendMsg(String msg);

    String sendMsg02(String msg);
}

public class QuickStartServiceImpl implements ServiceAPI {
    @Override
    public String sendMsg(String msg) {

        System.out.println("msg = " + msg);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "provider提供者:" + msg;

    }

    @Override
    public String sendMsg02(String msg) {

        System.out.println("msg 02 = " + msg);

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "provider提供者:02" + msg;
    }
}
<?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:context="http://www.springframework.org/schema/context"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
		xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
		"
		>

	<bean id="quickStartService" class="com.mooc.dubbo.quickstart.QuickStartServiceImpl"/>

	<!--
	对外暴露接口
	interface:接口位置;ref:相关引用id名.
	逻辑是:对外暴露的接口是API,接口实现类通过ref可以知道是谁
	-->
	<dubbo:service
			timeout="10000"
			async="true"
			interface="com.mooc.dubbo.ServiceAPI"
			ref="quickStartService"/>
	<!--因为添加了zookeeper,所以不用这个“注册为空的”的标签了  registry="N/A"-->

	<!--添加zookeeper注册中心-->
	<dubbo:registry address="zookeeper://localhost:2181"/>

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

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

</beans>

consumer端

public class ConsumerClient {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();

        while (true){
            Scanner scanner = new Scanner(System.in);
            String inMsg = scanner.next();

            ServiceAPI serviceAPI = (ServiceAPI) context.getBean("consumerService");

            //测试异步调用
            long beginTime = System.currentTimeMillis();

            serviceAPI.sendMsg(inMsg);
            Future<String> sendFuture = RpcContext.getContext().getFuture();

            long sendEndTime = System.currentTimeMillis();

            serviceAPI.sendMsg02(inMsg);
            Future<String> sendFuture02 = RpcContext.getContext().getFuture();

            long sendEndTime2 = System.currentTimeMillis();

            System.out.println(sendFuture.get() + "," + sendFuture02.get() +
                    ",send执行时间="+(sendEndTime-beginTime)+
                    ",send02执行时间=" +(sendEndTime2 - beginTime ) );
        }
    }
}
<?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:context="http://www.springframework.org/schema/context"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
		xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
		"
		>

	<dubbo:application name="consumer" />

	<dubbo:reference
			id="consumerService"
			interface="com.mooc.dubbo.ServiceAPI"
			loadbalance="roundrobin"
	/>

	<!--url="dubbo://localhost:20880"-->

	<!--添加zookeeper注册中心-->
	<dubbo:registry address="zookeeper://localhost:2181"/>
</beans>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值