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.");
    }
}
相关推荐
helloworld工程师7 小时前
Dubbo应用开发之基于Dubbo协议的springboot规范性开发
spring boot·后端·dubbo
张人大 Renda Zhang8 小时前
Spring Cloud / Dubbo 是 2 楼,Kubernetes 是 1 楼,Service Mesh 是地下室:Java 微服务的“三层楼模型”
spring boot·spring cloud·云原生·架构·kubernetes·dubbo·service_mesh
拾忆,想起8 小时前
Dubbo服务降级全攻略:构建韧性微服务系统的守护盾
java·前端·网络·微服务·架构·dubbo
helloworld_工程师1 天前
Dubbo应用开发之基于Dubbo协议的springboot规范性开发
dubbo
拾忆,想起1 天前
Dubbo服务访问控制(ACL)完全指南:从IP黑白名单到自定义安全策略
前端·网络·网络协议·tcp/ip·微服务·php·dubbo
拾忆,想起1 天前
Dubbo序列化性能优化实战:从协议选型到极致调优
前端·微服务·性能优化·架构·dubbo·safari
渡我白衣1 天前
计算机组成原理(2):计算机硬件的基本组成
运维·服务器·网络·c++·人工智能·网络协议·dubbo
拾忆,想起2 天前
Dubbo序列化异常终结指南:从精准诊断到根治与防御
开发语言·前端·微服务·架构·php·dubbo·safari
拾忆,想起2 天前
Dubbo 监控数据采集全链路实战:构建微服务可观测性体系
前端·微服务·云原生·架构·dubbo
拾忆,想起3 天前
Dubbo灰度发布完全指南:从精准引流到全链路灰度
前端·微服务·架构·dubbo·safari