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.");
    }
}
相关推荐
武子康4 小时前
Java-194 RabbitMQ 分布式通信怎么选:SOA/Dubbo、微服务 OpenFeign、同步重试与 MQ 异步可靠性落地
大数据·分布式·微服务·消息队列·rabbitmq·dubbo·异步
拾忆,想起5 小时前
Dubbo服务依赖问题终结指南:从根因分析到系统化解决方案
微服务·性能优化·架构·dubbo·safari
拾忆,想起6 小时前
Dubbo通信协议全景指南:如何为你的微服务选择最佳通信方案?
微服务·云原生·性能优化·架构·dubbo·safari
永不停歇的蜗牛1 天前
K8S之Ctr 和 Docker的区别
docker·kubernetes·dubbo
apihz2 天前
反向DNS查询与蜘蛛验证免费API接口详细教程
android·开发语言·数据库·网络协议·tcp/ip·dubbo
拾忆,想起2 天前
Dubbo服务注册与发现深度解析:微服务架构的“通讯录”与“导航系统”
微服务·云原生·性能优化·架构·dubbo·safari
Hui Baby3 天前
Dubbo/springCloud同机房收敛
spring·spring cloud·dubbo
韩凡3 天前
【Java框架体系总结(个人)】
java·spring boot·redis·dubbo
拾忆,想起3 天前
Dubbo健康检查全攻略:构建高可观测与高可用的微服务基座
开发语言·微服务·云原生·架构·php·dubbo·safari
helloworld工程师4 天前
Dubbo应用开发之基于Dubbo协议的springboot规范性开发
spring boot·后端·dubbo