Generic Implementation

Generic implementation for providing and publishing services when the provider does not have an API (SDK)

Note

Please distinguish between the previous document on Generic Invocation, which is for consumers, and Generic Implementation, which is for providers.

The generic interface implementation method is mainly used in scenarios where the server side does not have API interfaces and model class metadata. All POJOs in parameters and return values are represented using Map, usually for framework integration. For example, to implement a generic remote service Mock framework, you can handle all service requests by implementing the GenericService interface.

Use Cases

  • Service Registration: The service provider registers services in the service registry, such as Zookeeper, which stores information about the service, such as its interfaces, implementation classes, and addresses.

  • Service Deployment: The service provider deploys the service on a server and makes it available to consumers.

  • Service Invocation: Users call services generated by the service registry proxy, forwarding requests to the service provider, which executes the service and sends responses back to the consumer.

  • Service Monitoring: Providers and consumers can use the Dubbo framework to monitor services, allowing them to view service execution and make adjustments as needed.

Usage

Refer to the complete source code for this example at dubbo-samples-generic-impl.

Implement the GenericService interface in Java code

  1. package com.foo;
  2. public class MyGenericService implements GenericService {
  3. public Object $invoke(String methodName, String[] parameterTypes, Object[] args) throws GenericException {
  4. if ("sayHello".equals(methodName)) {
  5. return "Welcome " + args[0];
  6. }
  7. }
  8. }

Exposing Generic Implementation through Spring

Declare the service implementation in Spring XML configuration

  1. <bean id="genericService" class="com.foo.MyGenericService" />
  2. <dubbo:service interface="com.foo.BarService" ref="genericService" />

Exposing Generic Implementation through API

  1. ...
  2. // Use org.apache.dubbo.rpc.service.GenericService instead of all interface implementations
  3. GenericService xxxService = new XxxGenericService();
  4. // This instance is heavyweight, encapsulating all connections to the registry and service provider; please cache it
  5. ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
  6. // Weak type interface name
  7. service.setInterface("com.xxx.XxxService");
  8. // If you need to set a different version for the service
  9. service.setVersion("1.0.0");
  10. // Point to a generic service implementation
  11. service.setRef(xxxService);
  12. // Expose and register the service
  13. service.export();
  1. When setting ServiceConfig, use setGeneric("true") to enable generic invocation
  2. When setting ServiceConfig, use setRef to specify the implementation class, and set an object of GenericService, not the actual service implementation class object
  3. Other settings are consistent with normal API service starts

Feedback

Was this page helpful?

Yes No

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