syntax = "proto3";
// go 生成文件所在包
option go_package = "proto";
option java_multiple_files = true;
// java 生成文件所在包
option java_package = "com.bocloud.autorelease.hello";
// java 生成文件类名称
option java_outer_classname = "HelloProto";
option objc_class_prefix = "HL";
package hello;
service Hello {
// 接口 1
rpc SayHello (HelloRequest) returns (HelloResponse) {}
// 接口 2
rpc SayOperation (WatchObject) returns (HelloResponse) {}
}
// 数据结构
message WatchObject{
ServiceObject service = 1;
DeployObject deploy = 2;
string verb = 3;
}
// 数据结构
message ServiceObject{
string name = 1;
string namespace = 2;
map labels = 3;
}
// 数据结构
message DeployObject{
string name = 1;
string namespace = 2;
map labels = 3;
}
// 数据结构.
message HelloRequest {
string name = 1;
}
// 数据结构
message HelloResponse {
string message = 1;
}
1:生成代码
需要注意的地方:
protoc --go_out=plugins=grpc:./ ./hello.proto
3:测试 client.go
package main
import (
"auth-release/proto"
"context"
"google.golang.org/grpc"
"log"
)
const (
Address string = "127.0.0.1:50051"
)
func main() {
// 连接服务器
conn, err := grpc.Dial(Address, grpc.WithInsecure())
if err != nil {
log.Fatalf("net.Connect err: %v", err)
}
// 创建客户端
grpcClient := proto.NewHelloClient(conn)
req := proto.HelloRequest{
Name: "Tom"}
// 调用 Server
res, err := grpcClient.SayHello(context.Background(), &req)
if err != nil {
log.Fatalf("Call Route err: %v", err)
}
// 打印返回值
log.Println(res)
}
4:go 依赖
差点忘记说,go.mod 中的依赖
github.com/golang/protobuf v1.4.2
google.golang.org/grpc v1.28.1
google.golang.org/protobuf v1.23.0
1:添加依赖
必须添加下面这些依赖,不然生成之后会出现错误。
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-protobufartifactId>
<version>1.23.1version>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-stubartifactId>
<version>1.23.1version>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-netty-shadedartifactId>
<version>1.23.1version>
dependency>
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
dependency>
<extensions>
<extension>
<groupId>kr.motd.mavengroupId>
<artifactId>os-maven-pluginartifactId>
<version>1.5.0.Finalversion>
extension>
extensions>
<plugin>
<groupId>org.xolstice.maven.pluginsgroupId>
<artifactId>protobuf-maven-pluginartifactId>
<version>0.5.1version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}protocArtifact>
<pluginId>grpc-javapluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.23.1:exe:${os.detected.classifier}pluginArtifact>
configuration>
<executions>
<execution>
<goals>
<goal>compilegoal>
<goal>compile-customgoal>
goals>
execution>
executions>
plugin>
2:生成代码
需要注意的地方:
maven clean install
即可option java_package = "com.bocloud.autorelease.hello";
option java_outer_classname = "HelloProto";
package com.bocloud.autorelease.server;
import com.bocloud.autorelease.hello.*;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
public class HelloServer {
private Server server;
private void start() throws IOException {
/* The port on which the server should run */
int port = 50051;
server = ServerBuilder.forPort(port)
.addService(new HelloIml()) //这里可以添加多个模块
.build()
.start();
System.out.println("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
try {
HelloServer.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
public static void main(String[] args) throws IOException, InterruptedException {
final HelloServer server = new HelloServer();
server.start();
server.blockUntilShutdown();
}
private static class HelloIml extends HelloGrpc.HelloImplBase{
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
// super.sayHello(request, responseObserver);
HelloResponse helloResponse=HelloResponse.newBuilder().setMessage("Hello "+request.getName()+", I'm Java grpc Server").build();
responseObserver.onNext(helloResponse);
responseObserver.onCompleted();
}
@Override
public void sayOperation(WatchObject request,
io.grpc.stub.StreamObserver<HelloResponse> responseObserver) {
System.out.println("Deployment" +"="+ request.getDeploy().toString());
System.out.println("Service" +"="+ request.getService().toString());
System.out.println("operation" +"="+ request.getVerb());
HelloResponse helloResponse=HelloResponse.newBuilder().setMessage("Watch operation"+request.getVerb()+", I've responded").build();
responseObserver.onNext(helloResponse);
responseObserver.onCompleted();
}
}
}
1:启动 server
10:18:37.447 [main] DEBUG io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
10:18:37.447 [main] DEBUG io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
10:18:37.447 [main] DEBUG io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
Server started, listening on 50051
启动成功,并且监听在 50051
端口
2:指定 client 调用
2020/09/11 10:21:03 message:"Hello Tom, I'm Java grpc Server"
Process finished with exit code 0
执行成功。上面我写了两个接口,大家可以尝试调用另一个接口试试。