Dubbo asynchronous calls can be divided into Provider-side asynchronous calls and Consumer-side asynchronous calls.
Below is a working example diagram of consumer asynchronous calls:
Provider-side asynchronous execution and Consumer-side asynchronous calls are mutually independent, and you can combine the configurations of both sides in any orthogonal manner.
Please refer to the complete example source code demonstrated in this document:
Interface Definition:
Service Implementation:
By returning CompletableFuture.supplyAsync(), the business execution has switched from the Dubbo thread to the business thread, avoiding blocking the Dubbo thread pool.
Dubbo provides an asynchronous interface AsyncContext similar to Servlet 3.0, which can also achieve Provider-side asynchronous execution without a CompletableFuture signature interface.
Interface Definition:
Service Implementation:
Configure in the annotation:
You can also specify method-level asynchronous configurations:
The subsequent calls will be asynchronous:
Or, you can also do asynchronous calls this way
Asynchronous calls never wait for a return, and you can also set whether to wait for the message to be sent
sent="true"
waits for the message to be sent; an exception will be thrown if the message sending fails.sent="false"
does not wait for the message to be sent, puts the message into the IO queue, and returns immediately.If you just want to be asynchronous and completely ignore the return value, you can configure return="false"
to reduce the creation and management cost of Future objects