dubbo-async

1、接口及实现类定义超时时间

public interface HelloService {
    String hello(String name,int timeToWait);
}


@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name,int timeToWait) {
        try {
            Thread.sleep(timeToWait);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello" + name;
    }
}

2、消费组配置 consumer.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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--提供应用方信息,用于计算依赖关系-->
    <dubbo:application name="service-consumer"/>
    <!--使用zookeeper注册中心暴露服务地址-->
    <dubbo:registry address="zookeeper://101.132.167.18:2181?timeout=30000"/>
    <!--使用dubbo协议在20882端口暴露服务,也可以使用 rmi 协议暴露-->
    <dubbo:reference id="helloService" interface="com.lagou.service.HelloService">
        <dubbo:method name="hello" async="true"/>
    </dubbo:reference>
</beans>

3、消费组启动类

public class XMLConsumerMain {

    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        HelloService helloService = context.getBean(HelloService.class);
        while (true){
            System.in.read();
            String hello = helloService.hello("world", 100);
            //利用future模式来获取
            Future<Object> future = RpcContext.getContext().getFuture();
            System.out.println("result:" + hello);
            System.out.println("future result:" + future.get());
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值