In the Java class library, GRPC Protobuf is a guide to the microservice architecture
How to use the Java class library to integrate GRPC Protobuf to achieve microservices architecture
introduce:
Micro -service architecture is a method of disassembling large applications into small and autonomous services.GRPC is a high -performance, open source remote process call (RPC) framework, and Protobuf is a data serialization format developed by Google.By integrating GRPC and Protobuf, we can achieve efficient and reliable microservices.
This article will guide you how to use the Java class library to integrate GRPC Protobuf to build an application based on microservices architecture.
prerequisites:
1. JDK: Install the latest version of the Java development toolkit.
2. Maven: Install the latest version of Maven building tool.
3. GRPC and Protobuf: You can download and install GRPC and protobuf tools from the GRPC official website.
Step 1: Define service
First of all, we need to define our services using Protobuf to define our services.Create a file called "Hello.proto" in the project root directory, and add the following:
protobuf
syntax = "proto3";
option java_package = "com.example.grpc";
option java_outer_classname = "HelloProto";
service HelloService {
rpc sayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
Step 2: Generate code
Next, we need to use the Protobuf tool to generate Java files.Run the following command under the root directory of the project:
protoc -I . --java_out=src/main/java hello.proto
This will generate corresponding Java files in the SRC/main/java/com/exmple/GRPC directory.
Step 3: Implement service
Now, we can use the generated code to achieve our services.Create a Java file helloserviceImpl in the project, and add the following code to this class:
package com.example.grpc;
import io.grpc.stub.StreamObserver;
public 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();
}
}
In the above code, we inherited the automatic generated HelloServiceGRPC.HelloserviceIMPLBASE class and rewritten the Sayhello method.In this method, we obtain the name parameter in the request and use it to create response messages.Finally, we responded through Responseobserver.
Step 4: Start GRPC server
We need to write code to start the GRPC server and bind the service we implement to the server.Add the following code to the Main class of the project:
package com.example.grpc;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
Server server = ServerBuilder.forPort(9090)
.addService(new HelloServiceImpl())
.build();
server.start();
System.out.println("Server started");
server.awaitTermination();
}
}
In the above code, we create a GRPC server and bind it to port 9090.Then, we add HelloserviceIMPL to the server and start the server.
Step 5: Create a GRPC client
Finally, we need to write the GRPC client code to call our services.Create a Java file HelloClient in the project, and add the following code to this class:
package com.example.grpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
public class HelloClient {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090)
.usePlaintext()
.build();
HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub(channel);
HelloRequest request = HelloRequest.newBuilder().setName("Alice").build();
HelloResponse response = stub.sayHello(request);
System.out.println(response.getMessage());
channel.shutdown();
}
}
In the above code, we create a GRPC client and connect it to the server 9090.Then, we created a HelloserviceGrpc.HelloserviceBlockingstub and used it to call the service Sayhello method.Finally, we printed the response message and close the channel.
Configuration:
In the pom.xml file of the project, add the following dependencies:
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.39.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.39.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.39.0</version>
</dependency>
These dependencies will provide you with the class libraries required to integrate with GRPC and Protobuf.
Summarize:
By integrating GRPC and Protobuf and using the Java class library, we can easily build applications based on microservices.This article introduces how to define services, generate code, realize services, start GRPC servers, and create GRPC clients.By following this guide, you will be able to quickly get started with GRPC Protobuf and build an efficient microservice architecture.