Generic Call

Generic call for invoking the service when the caller does not have the API (SDK) provided by the service provider.

Note

Generic calls are suitable for older versions of the Dubbo communication protocol. If you are using the triple protocol from version 3.3 and onwards, please use the HTTP application/json capabilities provided by the triple protocol to directly initiate service calls. Relevant examples can be found in Gateway Access Instructions.

A generic call (client generic call) refers to invoking the service when the caller does not have the service provider’s API (SDK) and can still obtain the call result. The caller can invoke the corresponding interface through a generic call by knowing the fully qualified class name and method name of the service interface.

Usage Scenarios

Generic calls can initiate requests to all services through a universal GenericService interface. Typical use cases include:

  1. Gateway Service: When building a gateway service, the service gateway acts as the caller for all RPC services without relying on the service provider’s API. Therefore, it requires support for generic calls.

  2. Testing Platform: When creating a platform to test RPC calls, users can input group names, interfaces, method names, etc., to test the corresponding RPC services. Similar to the gateway, it should not depend on the service provider’s API, hence requiring support for generic calls.

Usage Method

Please refer to the complete source code of this example at dubbo-samples-generic-call.

The example includes the following Dubbo service definitions and implementations.

Service interface definition:

  1. public interface HelloService {
  2. String sayHello(String name);
  3. CompletableFuture<String> sayHelloAsync(String name);
  4. CompletableFuture<Person> sayHelloAsyncComplex(String name);
  5. CompletableFuture<GenericType<Person>> sayHelloAsyncGenericComplex(String name);
  6. }

Service implementation and publishing:

  1. @DubboService
  2. public class HelloServiceImpl implements HelloService {
  3. @Override
  4. public String sayHello(String name) {
  5. return "sayHello: " + name;
  6. }
  7. @Override
  8. public CompletableFuture<String> sayHelloAsync(String name) {
  9. // ...
  10. }
  11. @Override
  12. public CompletableFuture<Person> sayHelloAsyncComplex(String name) {
  13. // ...
  14. }
  15. @Override
  16. public CompletableFuture<GenericType<Person>> sayHelloAsyncGenericComplex(String name) {
  17. // ...
  18. }
  19. }

API Invocation Method

For the above Dubbo service, we can initiate calls directly through the generic call API.

  1. private GenericService genericService;
  2. public static void main(String[] args) throws Exception {
  3. ApplicationConfig applicationConfig = new ApplicationConfig();
  4. applicationConfig.setName("generic-call-consumer");
  5. RegistryConfig registryConfig = new RegistryConfig();
  6. registryConfig.setAddress("zookeeper://127.0.0.1:2181");
  7. ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
  8. referenceConfig.setInterface("org.apache.dubbo.samples.generic.call.api.HelloService");
  9. applicationConfig.setRegistry(registryConfig);
  10. referenceConfig.setApplication(applicationConfig);
  11. referenceConfig.setGeneric("true");
  12. // do not wait for result, 'false' by default
  13. referenceConfig.setAsync(true);
  14. referenceConfig.setTimeout(7000);
  15. genericService = referenceConfig.get();
  16. }
  17. public static void invokeSayHello() throws InterruptedException {
  18. Object result = genericService.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"world"});
  19. CountDownLatch latch = new CountDownLatch(1);
  20. CompletableFuture<String> future = RpcContext.getContext().getCompletableFuture();
  21. future.whenComplete((value, t) -> {
  22. System.err.println("invokeSayHello(whenComplete): " + value);
  23. latch.countDown();
  24. });
  25. System.err.println("invokeSayHello(return): " + result);
  26. latch.await();
  27. }
  1. When setting ReferenceConfig, use setGeneric("true") to enable generic calls.
  2. After configuring ReferenceConfig, use referenceConfig.get() to get the instance of the GenericService class.
  3. Use its $invoke method to get the result.
  4. Other settings are consistent with normal service call configuration.

Spring Invocation Method

In Spring, there are various ways to expose services and discover services, such as XML and annotations. Here, XML is used as an example.

  1. No changes are needed on the producer side.

  2. The existing dubbo:reference tag on the consumer side should have the generic=true attribute added.

  1. <dubbo:reference id="helloService" generic = "true" interface="org.apache.dubbo.samples.generic.call.api.HelloService"/>
  1. Obtain the Bean container and retrieve the GenericService instance via the Bean container.

  2. Call the $invoke method to get the result.

  1. private static GenericService genericService;
  2. public static void main(String[] args) throws Exception {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/generic-impl-consumer.xml");
  4. context.start();
  5. // The name of the service corresponding bean is determined by the id of the xml tag.
  6. genericService = context.getBean("helloService");
  7. // Obtain the result.
  8. Object result = genericService.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"world"});
  9. }

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)