Go使用go model需要
go get -u github.com/golang/protobuf
go get -u google.golang.org/gprc
go get -u github.com/golang/protobuf/protoc-gen-go
其中gen.sh是保存了需要执行的proto命令
#! /bin/bash
cd proto && protoc --go_out=plugins=grpc:../services hello_world.proto
protoc --go_out=plugins=grpc:../services InnerGameInfo.proto
protoc --go_out=plugins=grpc:../services Models.proto
cd ..
以下是go的main方法
lis, err := net.Listen("tcp", "127.0.0.1:8090")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
gRpcServer := grpc.NewServer()
services.RegisterInnerGameInfoServiceServer(gRpcServer, new(services.InnerGameInfoInterface))
gRpcServer.Serve(lis)
JAVA方面
需要引入的依赖如下:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.23.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.23.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.23.1</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:1.23.1:exe:${os.detected.classifier}
</pluginArtifact>
<protoSourceRoot>../java-protobuf/src/main/proto</protoSourceRoot>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
</build>
<protoSourceRoot> 这个标签特别说一下 这个标签是指定proto文件路径的,一开始我并不知道需要这个 然后一直在坑中爬
import这些依赖之后 在idea的右边Plugibs中会有一个protobuf的一览 点开先执行第一个出现build success之后在点击第三个 直到出现build success之后 在项目的traget中generated-source文件夹中的protobuf目录下会出现两个文件夹一个是grpc-java、一个是java 第一个文件夹中的文件是带有Grpc第二个文件中会出现五个相关的java文件,如果看到这些说明已经生成好了
下面介绍一个Go和Java的.proto文件
GO项目中
syntax = "proto3";
package proto;
message HelloRequest{
string request = 1;
}
message HelloResponse{
string response = 1;
}
service HelloService{
rpc HelloWorld(HelloRequest) returns (HelloResponse){}
}
这里简单介绍以下,最开始的是定义protobuf的版本 现在最新的是3.0有的一些公司可能还在使用2.0
package是包名 这个包名需要特别注意的 Go项目和Java项目的这个包名需要一致、其他的都有就是请求入参和出参中的数据结构 这个需要自己去protobuf的官方文档中查看
Java项目中的protobuf文件
syntax = "proto3";
package proto;
option java_generic_services = true;
option java_multiple_files = true;
option java_package = "com.fox.proto";
option java_outer_classname = "HelloWorldProto";
message HelloRequest{
string request = 1;
}
message HelloResponse{
string response = 1;
}
service HelloService{
rpc HelloWorld(HelloRequest) returns (HelloResponse){}
}
JAVA的启动类
public static void main(String[] args) {
ManagedChannel channel = NettyChannelBuilder.forAddress("127.0.0.1",8090)
.negotiationType(NegotiationType.PLAINTEXT)
.build();
HelloServiceGrpc.HelloServiceBlockingStub blockingStub = HelloServiceGrpc.newBlockingStub(channel);
HelloRequest helloRequest = HelloRequest.newBuilder().setRequestBytes(ByteString.copyFromUtf8("hello grpc")).build();
HelloResponse helloResponse = blockingStub.helloWorld(helloRequest);
System.out.println(helloResponse.getResponse());
}
这个Demo我是以Go为服务端Java为客户端来说明的,网上很多例子都有 我写这个主要就是有些网上有的并没有交代的很清楚或者是一些坑 作者并没有写出来