Develop RPC Server and Client with Lightweight Java SDK

Develop RPC Server and Client with Lightweight Java SDK

This example demonstrates how to use the lightweight Dubbo SDK to develop RPC Server and Client. The example uses Java Interface to define, publish, and access RPC services, with Triple protocol communication at the core. For the complete code of this example, please refer to dubbo-samples.

Based on the Triple protocol defined by Dubbo, you can easily write browser and gRPC-compatible RPC services that can run on both HTTP/1 and HTTP/2. The Dubbo Java SDK supports defining services using IDL or language-specific methods and provides a lightweight API for publishing or invoking these services.

Maven Dependency

Before coding based on Dubbo RPC, you only need to add a very lightweight dubbo dependency to your project. Here’s an example in Maven:

  1. <dependency>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo</artifactId>
  4. <version>3.3.0</version>
  5. </dependency>
  6. <!-- To avoid Netty dependency conflicts, you can also choose to use the dubbo-shaded version! -->
  7. <!--
  8. <dependency>
  9. <groupId>org.apache.dubbo</groupId>
  10. <artifactId>dubbo-shaded</artifactId>
  11. <version>3.3.0</version>
  12. </dependency>
  13. -->

Define Service

Define a standard Java interface named DemoService as the Dubbo service (Dubbo also supports IDL-based service definition).

  1. public interface DemoService {
  2. String sayHello(String name);
  3. }

Implement the DemoService interface and write the business logic code.

  1. public class DemoServiceImpl implements DemoService {
  2. @Override
  3. public String sayHello(String name) {
  4. return "Hello " + name + ", response from provider.";
  5. }
  6. }

Register Service and Start Server

Start the server and listen for RPC requests on the specified port. Before that, we registered the following information with the server:

  • Use Triple as the communication RPC protocol and listen on port 50051
  • Register Dubbo service to DemoService server
  1. public class Application {
  2. public static void main(String[] args) {
  3. DubboBootstrap.getInstance()
  4. .protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
  5. .service(ServiceBuilder.newBuilder().interfaceClass(DemoService.class).ref(new DemoServiceImpl()).build())
  6. .start()
  7. .await();
  8. }
  9. }

Access Service

The simplest way is to use HTTP/1.1 POST requests to access the service, with parameters passed in standard JSON format as HTTP payload. Here’s an example of accessing using cURL:

  1. curl \
  2. --header "Content-Type: application/json" \
  3. --data '["Dubbo"]' \
  4. http://localhost:50051/org.apache.dubbo.demo.DemoService/sayHello

Parameters must be passed in array format; if there are multiple parameters, the format looks like ["param1", {"param2-field": "param2-value"}, ...]. Please refer to the triple protocol specification for details.

Next, you can also use a standard Dubbo client to request services by specifying the server address to initiate an RPC call in the format protocol://ip:host.

  1. public class Application {
  2. public static void main(String[] args) {
  3. DemoService demoService =
  4. ReferenceBuilder.newBuilder()
  5. .interfaceClass(DemoService.class)
  6. .url("tri://localhost:50051")
  7. .build()
  8. .get();
  9. String message = demoService.sayHello("dubbo");
  10. System.out.println(message);
  11. }
  12. }

Congratulations! You have now learned the basic usage of Dubbo Java RPC communication! 🎉

More Content

Feedback

Was this page helpful?

Yes No

Last modified October 22, 2024: fixed unspecified `interfaceClass` (#3052) (bcab21bd8a6)