dubbo复习:(12)服务器端的异步和客户端的异步调用

一、服务器端接口的定义和实现:

package cn.edu.tju.service;

import java.util.concurrent.CompletableFuture;

public interface AsyncService {
    /**
     * 同步调用方法
     */
    String invoke(String param);
    /**
     * 异步调用方法
     */
    CompletableFuture<String> asyncInvoke(String param);
}

package cn.edu.tju.service;

import org.apache.dubbo.config.annotation.DubboService;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;

@DubboService
public class AsyncServiceImpl implements AsyncService {

    @Override
    public String invoke(String param) {
        try {
            long time = ThreadLocalRandom.current().nextLong(1000);
            Thread.sleep(time);
            StringBuilder s = new StringBuilder();
            s.append("AsyncService invoke param:").append(param).append(",sleep:").append(time);
            return s.toString();
        }
        catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return null;
    }

    @Override
    public CompletableFuture<String> asyncInvoke(String param) {
        // 建议为supplyAsync提供自定义线程池
        return CompletableFuture.supplyAsync(() -> {
            try {
                // Do something
                long time = ThreadLocalRandom.current().nextLong(1000);
                Thread.sleep(time);
                StringBuilder s = new StringBuilder();
                s.append("AsyncService asyncInvoke param:").append(param).append(",sleep:").append(time);
                return s.toString();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return null;
        });
    }
}

二、客户端的异步调用:

package cn.edu.tju.service;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component
public class AsyncConsumer implements CommandLineRunner {
    @DubboReference
    private AsyncService asyncService;

    @Override
    public void run(String... args) throws Exception {
        //调用异步接口
        CompletableFuture<String> future1 = asyncService.asyncInvoke("async call request1");
        future1.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                System.out.println("AsyncTask Response-1: " + v);
            }
        });
        //两次调用并非顺序返回
        CompletableFuture<String> future2 = asyncService.asyncInvoke("async call request2");
        future2.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                System.out.println("AsyncTask Response-2: " + v);
            }
        });
        //consumer异步调用
        CompletableFuture<String> future3 =  CompletableFuture.supplyAsync(() -> {
            return asyncService.invoke("invoke call request3");
        });
        future3.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                System.out.println("AsyncTask Response-3: " + v);
            }
        });

        System.out.println("AsyncTask Executed before response return.");
    }
}
相关推荐
向阳12184 小时前
Dubbo负载均衡
java·运维·负载均衡·dubbo
一叶飘零_sweeeet1 天前
Dubbo 构建高效分布式服务架构
分布式·架构·dubbo
webfunny20205 天前
前端埋点系统之如何用heatmap.js画网页热力图
前端·javascript·dubbo
菜鸟起航ing6 天前
Apache Dubbo (RPC框架)
rpc·apache·dubbo
飞升不如收破烂~6 天前
包括 Nginx、Gateway、Nacos、Dubbo、Sentinel、RocketMQ 和 Seata 的调用链路描述:
nginx·gateway·dubbo
纳贤猫NXM.COM8 天前
从0到1,解读安卓ASO优化!
android·python·django·flask·dubbo·tornado·dash
羽羽Ci Ci9 天前
模态框登录bootstrap
前端·bootstrap·dubbo
Java Fans10 天前
使用Python自动化高德地图/百度地图导航并提取公里数
python·自动化·dubbo
百度Geek说14 天前
ClickHouse在百度MEG数据中台的落地和优化
clickhouse·百度·dubbo
TracyCoder12323 天前
Dubbo快速入门(二):第一个Dubbo程序(附源码)
dubbo