java grpc 样例
下面是一个简单的Java gRPC样例:
- 首先,需要定义一个.proto文件,用于描述gRPC服务和消息格式。例如,创建一个名为
HelloWorld.proto的文件,并添加以下内容:
syntax = "proto3";
package com.example.grpc;
option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "HelloWorldProto";
service HelloWorldService {
rpc sayHello (HelloRequest) returns (HelloResponse) {}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
- 使用
protoc编译器将.proto文件编译为Java代码。在命令行中运行以下命令:
$ protoc --java_out=. HelloWorld.proto
- 创建一个Java类,用于实现gRPC服务。例如,创建一个名为
HelloWorldServer.java的文件,并添加以下内容:
package com.example.grpc;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
public class HelloWorldServer {
private Server server;
private void start() throws IOException {
int port = 50051;
server = ServerBuilder.forPort(port)
.addService(new HelloWorldServiceImpl())
.build()
.start();
System.out.println("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.err.println("*** shutting down gRPC server since JVM is shutting down");
HelloWorldServer.this.stop();
System.err.println("*** server shut down");
}));
}
private void stop() {
if (server != null) {
server.shutdown();
}
}
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
public static void main(String[] args) throws IOException, InterruptedException {
final HelloWorldServer server = new HelloWorldServer();
server.start();
server.blockUntilShutdown();
}
static class HelloWorldServiceImpl extends HelloWorldServiceGrpc.HelloWorldServiceImplBase {
@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();
}
}
}
- 创建一个Java类,用于实现gRPC客户端。例如,创建一个名为
HelloWorldClient.java的文件,并添加以下内容:
package com.example.grpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
public class HelloWorldClient {
private final ManagedChannel channel;
private final HelloWorldServiceGrpc.HelloWorldServiceBlockingStub blockingStub;
public HelloWorldClient(String host, int port) {
channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext()
.build();
blockingStub = HelloWorldServiceGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public void sayHello(String name) {
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloResponse response = blockingStub.sayHello(request);
System.out.println("Response: " + response.getMessage());
}
public static void main(String[] args) throws InterruptedException {
HelloWorldClient client = new HelloWorldClient("localhost", 50051);
client.sayHello("John Doe");
client.shutdown();
}
}
- 运行gRPC服务器和客户端代码。在命令行中分别运行以下命令:
$ java com.example.grpc.HelloWorldServer
$ java com.example.grpc.HelloWorldClient
这个样例演示了一个简单的gRPC服务和客户端的交互。服务器接收一个名字作为输入,并返回一个问候消息。客户端发送一个名字给服务器,并打印出服务器返回的问候消息
原文地址: https://www.cveoy.top/t/topic/iD5k 著作权归作者所有。请勿转载和采集!