gRPC之初体验

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

前言

经常看到说gRPC怎么好的文章,实际工作中也没有体验过,这次看了一下它的HelloWorld程序,记录一下这个过程。 RPC是Remote Produce Call 的缩写, 就是远程调用,调用远程的代码像本地一样。Java里面比较有名的RPC框架Dubbo,但它只支持Java。 gRPC 是google开源的RPC框架,使用HTTP2, 支持很多种语言:Java,GO,.Net Core,C,它都有对应的支持。 这篇初体验就打算使用Java做服务端,分别使用Java和GO作为客户端。

Java服务端和客户端

首先我们来建一个Java服务端。
使用gRPC最基础的一步是protobuf文件,这里我们直接使用HelloWorld里面的文件,首先新建一个helloworld.proto在source目录下面,参考了这篇文章https://www.cnblogs.com/liugh/p/7505533.html

syntax = "proto3";

option java_multiple_files = true;
option java_package = "ken.grpc.examples.helloworld";
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;
}

定义了一个SayHello方法,接收HelloRequest的消息,返回HelloReply

我们用proto文件生成相应的Java代码
这里我们直接使用maven插件protobuf-maven-plugin来完成,

 <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <id>enforce</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireUpperBoundDeps/>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

这样编译之后
image
就会生成对应的Java文件在target目录下面
image

然后就是新建一个HelloWorldServer来作为服务端,这里代码省略,可以直接使用
https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java

IDEA会出现找不到类的错误,重启一下IEDA可以修复

POM文件需要添加grpc-netty-shaded,grpc-protobuf,grpc-stub,protobuf-java-util,gson 参考grpc的sample就可以了,客户端也是这样的配置

客户端代码同样也是需要用Propo文件生成Java类,然后添加POM文件,然后编写客户端代码
https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java

运行服务端Java类, 控制台输出
INFO: Server started, listening on 50051
然后运行客户端代码,
一月 16, 2023 8:00:13 下午 ken.grpc.examples.helloworld.HelloWorldClient greet
INFO: Will try to greet world1 ...
一月 16, 2023 8:00:13 下午 ken.grpc.examples.helloworld.HelloWorldClient greet
INFO: Greeting: Hello 1world1
就可以在客户端看到对应的调用。

Go语言客户端

首先按照https://grpc.io/docs/languages/go/quickstart/里面的Prerequisites安装go, 下载Protocol buffer 编译器, protoc,文档里面的是Linux的指引,我用的是Window的系统,所以要下载到对应的Window版本,然后配置到Path里面。参考了这篇文章
https://www.geeksforgeeks.org/how-to-install-protocol-buffers-on-windows/
https://github.com/protocolbuffers/protobuf/releases/tag/v21.12
执行命令
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
安装插件。

基本的环境就可以了,就可以编码了。
首先用go mod init grpcdemo/hello新建一个项目
然后和Java相同的添加.proto文件,这里需要在proto文件中添加一行
option go_package = "grpcdemo/hello/helloworld";
这样才可以生成go语言的类,执行命令
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative helloworld/helloworld.proto
我们可以在目录下面看到生成的类
image

然后编写grpc-client/main.go,这里代码也是复制example里面的代码
https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_client/main.go
需要修改的地方是引用的包修改成你自己的
pb "grpcdemo/hello/helloworld/helloworld"

运行这个main.go,控制台输出
2023/01/16 16:18:31 Greeting: Hello 1world
我们调用了Java服务端的服务使用GO语言。

总结

体验gRPC整体感觉还是很容易,不管是服务端还是客户端都是以proto文件作为协议的基础, 查了下实际的引用中,这个文件的存放位置也比较有它的方法。这里有篇文章专门说这个事情https://blog.csdn.net/kevin_tech/article/details/122834090
如何编写proto,如何写好grpc的代码还有很多需要学习,这里只是体验一下,暂未深入。后期继续在深入学习一下。 同时也打算做些对比试验,比如它跟rest服务对比下到底能快多少,和Dubbo对比一下,哪个更好用。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6