Examples of distributed systems using GRPC Protobuf in the Java class library

Use GRPC Protobuf to implement the Java class library for distributed systems GRPC is a high -performance, open source and general remote process call (RPC) framework, which enables developers to build a distributed system more easily.Protobuf (Protocol Buffers) is a language structure format that has nothing to do with language, scalable and efficient serialized data structure.Combined with GRPC and Protobuf, fast, reliable, and type secure communication between different services can be achieved. The following is an example code that demonstrates how to use the GRPC Protobuf to implement the Java class library of a distributed system. 1. First, a proto file needs to be defined, which describes the message type and service interface of communication used.The following is a simple example: syntax = "proto3"; package com.example.grpc; message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } service HelloService { rpc sayHello(HelloRequest) returns (HelloResponse); } 2. Generate the Java class in the project, you can use the Protobuf compiler.Use the following commands to generate the Java class: protoc --java_out=. hello.proto This will generate the corresponding Java file in the current directory. 3. Implement the server code.The following is a simple example: import io.grpc.Server; import io.grpc.ServerBuilder; import io.grpc.stub.StreamObserver; import com.example.grpc.HelloRequest; import com.example.grpc.HelloResponse; import com.example.grpc.HelloServiceGrpc; import java.io.IOException; public class HelloServer { private Server server; public static void main(String[] args) throws IOException, InterruptedException { HelloServer helloServer = new HelloServer(); helloServer.start(); helloServer.blockUntilShutdown(); } private void start() throws IOException { int port = 50051; server = ServerBuilder.forPort(port) .addService(new HelloServiceImpl()) .build() .start(); System.out.println("Server started, listening on " + port); Runtime.getRuntime().addShutdownHook(new Thread(() -> { System.err.println("Shutting down gRPC server..."); HelloServer.this.stop(); System.err.println("Server shut down successfully."); })); } private void stop() { if (server != null) { server.shutdown(); } } private void blockUntilShutdown() throws InterruptedException { if (server != null) { server.awaitTermination(); } } private static class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase { @Override public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { String name = request.getName(); String message = "Hello, " + name + "!"; HelloResponse response = HelloResponse.newBuilder().setMessage(message).build(); responseObserver.onNext(response); responseObserver.onCompleted(); } } } 4. Implement client code.The following is a simple example: import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.stub.StreamObserver; import com.example.grpc.HelloRequest; import com.example.grpc.HelloResponse; import com.example.grpc.HelloServiceGrpc; public class HelloClient { private final ManagedChannel channel; private final HelloServiceGrpc.HelloServiceBlockingStub blockingStub; private final HelloServiceGrpc.HelloServiceStub asyncStub; public HelloClient(String host, int port) { channel = ManagedChannelBuilder.forAddress(host, port) .usePlaintext() .build(); blockingStub = HelloServiceGrpc.newBlockingStub(channel); asyncStub = HelloServiceGrpc.newStub(channel); } public void sayHello(String name) { HelloRequest request = HelloRequest.newBuilder().setName(name).build(); HelloResponse response = blockingStub.sayHello(request); System.out.println("Response: " + response.getMessage()); } public void sayHelloAsync(String name) { HelloRequest request = HelloRequest.newBuilder().setName(name).build(); asyncStub.sayHello(request, new StreamObserver<HelloResponse>() { @Override public void onNext(HelloResponse response) { System.out.println("Response: " + response.getMessage()); } @Override public void onError(Throwable t) { t.printStackTrace(); } @Override public void onCompleted() { System.out.println("Request completed."); } }); } public void shutdown() throws InterruptedException { channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); } public static void main(String[] args) throws InterruptedException { HelloClient helloClient = new HelloClient("localhost", 50051); helloClient.sayHello("Alice"); helloClient.sayHelloAsync("Bob"); helloClient.shutdown(); } } In the above code example, we realize a simple Helloservice service.HelloServer implements the server logic, including starting the server, processing requests, and closing the server.HelloClient implements client logic, including creating channels, calling services, and closing channels. Before running the code, you need to ensure that the Java plug -in of GRPC and Protobuf has been installed, and the correct dependencies are set in the construction configuration of the project. By using GRPC Protobuf, you can easily build a distributed system and achieve fast, reliable and type secure communication.I hope this article can help you understand how to use the Java class library to implement the GRPC Protobuf in a distributed system.