1.在pom中引入
io.grpc
grpc-netty
1.18.0
io.grpc
grpc-protobuf
1.18.0
io.grpc
grpc-stub
1.18.0
2.maven配置
kr.motd.maven
os-maven-plugin
1.5.0.Final
org.xolstice.maven.plugins
protobuf-maven-plugin
0.5.1
com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}
grpc-java
io.grpc:protoc-gen-grpc-java:1.14.0:exe:${os.detected.classifier}
compile
compile-custom
3.编写IDL文件, 因为要夸语言调用, 所以和golang的使用同一个文件,除了option之外, 所有的东西不要改!!!
syntax = "proto3";
option java_multiple_files = true;
option java_package = "cn.com.xu.grpc";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
4.生成文件
5.客户端代码
public class GrpcClient {
private final ManagedChannel channel;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
public GrpcClient(String host,int port){
channel = ManagedChannelBuilder.forAddress(host,port)
.usePlaintext(true)
.build();
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public void greet(String name){
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response = blockingStub.sayHello(request);
System.out.println("this is java client, response..." + response.getMessage());
}
public static void main(String[] args) throws InterruptedException {
GrpcClient client = new GrpcClient("localhost",50001);
client.greet("this is java client");
}
}
6.服务端代码
public class GrpcServer {
private int port = 50001;
private Server server;
private void start() throws IOException {
server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.build()
.start();
System.out.println("service start...");
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.err.println("*** shutting down gRPC server since JVM is shutting down");
GrpcServer.this.stop();
System.err.println("*** server shut down");
}
});
}
private void stop() {
if (server != null) {
server.shutdown();
}
}
// block 一直到退出程序
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
public static void main(String[] args) throws IOException, InterruptedException {
final GrpcServer server = new GrpcServer();
server.start();
server.blockUntilShutdown();
}
// 实现 定义一个实现服务接口的类
private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
public void sayHello(HelloRequest req, StreamObserver responseObserver) {
System.out.println("this is java service, request..."+req.getName());
HelloReply reply = HelloReply.newBuilder().setMessage(("this is java service")).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
}