十二:Dubbo高级特性(二)服务提供者异步

Apache Dubbo服务提供者异步

  1. Apache Dubbo在新版加入了服务提供者异步
  2. 服务提供者异步在提升性能和响应时长没有任何用处
  3. 服务提供者异步旨在尽量规避Dubbo任务处理的瓶颈

图示:

在这里插入图片描述

代码演示

provider端接口实现

public class ServiceDemoImpl implements ServiceDemo {
    private Executor executor = new ThreadPoolExecutor(2, 4, 1,
            TimeUnit.MINUTES, new ArrayBlockingQueue<>(1000), new NamedThreadFactory("wwy-"),
            new ThreadPoolExecutor.CallerRunsPolicy());

    /**
     * dubbo 高级特性之 异步执行
     * @param context
     * @return
     */
    @Override
    public String getSelf(String context) {
        AsyncContext asyncContext = RpcContext.startAsync();
        executor.execute(() -> {
        	//使得当前的子线程也可以使用rpc中的context信息
        	asyncContext.signalContextSwitch();
            String result = "Provider response : Hello " + context + ", Thread name is " + Thread.currentThread().getName();
            asyncContext.write(result);
        });
        return null;
    }
}

consumer端dubbo调用

@SpringBootApplication
@ImportResource(locations = {"classpath:springContext-dubbo.xml"})
public class DubboDemoApplication {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ConfigurableApplicationContext context = SpringApplication.run(DubboDemoApplication.class, args);
        ServiceDemo serviceDemo = context.getBean("serviceDemo", ServiceDemo.class);
        //调用
        String result = serviceDemo.getSelf("hello wwy");
        System.out.println(result);
    }

}

输出结果:

onReturn : Provider response : Hello hello wwy, Thread name is wwy--thread-1

源码解析

	//org.apache.dubbo.rpc.RpcContext#startAsync
    public static AsyncContext startAsync() throws IllegalStateException {
    	//先获取当前上下文
        RpcContext currentContext = getContext();
        //如果不存在asyncContext 则创建并赋值
        if (currentContext.asyncContext == null) {
            currentContext.asyncContext = new AsyncContextImpl();
        }
        //开启
        currentContext.asyncContext.start();
        return currentContext.asyncContext;
    }
    public AsyncContextImpl() {
    	//保存rpc的基本信息到该对象中
        this.storedContext = RpcContext.getContext();
        this.storedServerContext = RpcContext.getServerContext();
    }
    
    @Override
    public void start() {
    	//创建CompletableFuture
        if (this.started.compareAndSet(false, true)) {
            this.future = new CompletableFuture<>();
        }
    }
    
    @Override
    public void signalContextSwitch() {
        RpcContext.restoreContext(storedContext);
        RpcContext.restoreServerContext(storedServerContext);
        // Restore any other contexts in here if necessary.
    }
    
    @Override
    public void write(Object value) {
    	//判断是否已开启,并且设置为停止状态成功
        if (isAsyncStarted() && stop()) {
            if (value instanceof Throwable) {
                Throwable bizExe = (Throwable) value;
                future.completeExceptionally(bizExe);
            } else {
                future.complete(value);
            }
        } else {
            throw new IllegalStateException("The async response has probably been wrote back by another thread, or the asyncContext has been closed.");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值