- 一.创建 maven 项目
- 版本信息
- pom.xml
- 二.HelloWorld 快速上手
- 1.编写 protocol buffers 定义接口
- 2.maven 编译项目
- 3.编写服务端代码
- 4.编写客户端代码
- 5.测试
pom.xml1.8 UTF-8 1.34.1 3.12.0 3.12.0 1.8 1.8
二.HelloWorld 快速上手 1.编写 protocol buffers 定义接口4.0.0 a.b.c grpc-java-learning 1.0-SNAPSHOT 1.8 UTF-8 1.34.1 3.12.0 3.12.0 1.8 1.8 io.grpc grpc-bom ${grpc.version} pom import io.grpc grpc-netty-shaded runtime io.grpc grpc-protobuf io.grpc grpc-stub com.google.protobuf protobuf-java-util ${protobuf.version} kr.motd.maven os-maven-plugin 1.6.2 org.xolstice.maven.plugins protobuf-maven-plugin 0.6.1 com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} grpc-java io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} src/main/proto compile compile-custom org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8
创建.proto 文件 src/main/proto/helloworld.proto,定义服务输入和输出的数据结构
syntax = "proto3";
option java_multiple_files = true;
// 编译生成的 java 包名
option java_package = "com.grpc.examples.helloworld";
// 编译生成的 java 类名
option java_outer_classname = "HelloWorldProto";
// 定义一个服务
service HelloWordServer {
// 定义两个服务的输入和输出数据结构
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// 请求的数据结构
message HelloRequest {
string name = 3;
}
// 相应的数据结构
message HelloReply {
string message = 1;
}
2.maven 编译项目
在target/generated-sources/protobuf/grpc-java/com/grpc/examples/helloworld/HelloWordServerGrpc.java目录下生成com.grpc.examples.helloworld.HelloWordServerGrpc类
3.编写服务端代码要求实现继承 HelloWordServerGrpc.HelloWordServerImplbase ,重写 sayHello 方法
package grpc.examples.helloworld;
import com.grpc.examples.helloworld.HelloReply;
import com.grpc.examples.helloworld.HelloRequest;
import com.grpc.examples.helloworld.HelloWordServerGrpc;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
public class HelloWordServer extends HelloWordServerGrpc.HelloWordServerImplbase {
@Override
public void sayHello(HelloRequest request, StreamObserver responseObserver) {
String name = request.getName();
String message = syaHello(name);
responseObserver.onNext(HelloReply.newBuilder().setMessage(message).build());
responseObserver.onCompleted();
}
private String syaHello(String name) {
return "hello " + name + ", I am HelloWordServer.";
}
public static void main(String[] args) throws Exception {
ServerBuilder.forPort(9999)
.addService(new HelloWordServer())
.build()
.start();
System.out.println("server start at 9999");
Thread.sleep(9999999);
}
}
4.编写客户端代码
要求 ip 和 port 和服务端一致
package grpc.examples.helloworld;
import com.grpc.examples.helloworld.HelloReply;
import com.grpc.examples.helloworld.HelloRequest;
import com.grpc.examples.helloworld.HelloWordServerGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
public class HelloWorldClient {
HelloWordServerGrpc.HelloWordServerBlockingStub stub;
ManagedChannel channel;
public static void main(String[] args) {
HelloWorldClient client = new HelloWorldClient();
HelloReply reply = client.stub.sayHello(HelloRequest.newBuilder().setName("张三").build());
System.out.println(reply.getMessage());
}
public HelloWorldClient(){
channel = ManagedChannelBuilder
.forAddress("127.0.0.1",9999)
.usePlaintext()
.build();
stub =
HelloWordServerGrpc.newBlockingStub(channel);
}
}
5.测试
启动服务端代码,输出server start at 9999
启动客户端代码,输出hello 张三, I am HelloWordServer.



