首页 > 其他分享 >gRPC之初体验

gRPC之初体验

时间:2023-01-16 20:46:32浏览次数:66  
标签:初体验 Java gRPC helloworld grpc go java 客户端

前言

经常看到说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/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]
安装插件。

基本的环境就可以了,就可以编码了。
首先用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对比一下,哪个更好用。

标签:初体验,Java,gRPC,helloworld,grpc,go,java,客户端
From: https://www.cnblogs.com/dk168/p/17056254.html

相关文章

  • YOLOv8 初体验
    简介YOLOv8模型设计快速,准确,易于使用,使其成为广泛的目标检测和图像分割任务的绝佳选择。TheYOLOv8modelisdesignedtobefast,accurate,andeasytouse,making......
  • Android开发学习之路--Camera之初体验
      顾名思义Camera就是拍照和录像的功能,像微信里面,我们想拍照传一下照片,就可以通过camera来拍照,然后存储照片,发送给好友。那么微信的app里面是不会直接通过cameraapi来......
  • Android开发学习之路--数据持久化之初体验
      上班第一天,虽然工作上处于酱油模式,但是学习上依旧不能拉下,接着学习android开发吧,这里学习数据持久化的知识。  其实数据持久化就是数据可以保存起来,一般我们保存......
  • Android开发学习之路--UI之初体验
      之前都是学习Activity,对于布局都没有做过学习,这里就简单学习下吧。下面看下AndroidStudio下有哪些控件:  这里分为Widgets,TextFields,Containers,Date&Time和Exp......
  • Android开发学习之路--Activity之初体验
      环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看an......
  • Android开发学习之路--React-Native之初体验
      近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下react-native。(......
  • Android开发学习之路--RxAndroid之初体验
      学了一段时间android,看了部分的项目代码,然后想想老是学基础也够枯燥乏味的,那么就来学习学习新东西吧,相信很多学java的都听说过RxJava,那么android下也有RxAndroid。 ......
  • Android开发学习之路--Annotation注解简化view控件之初体验
      一般我们在写androidActivity的时候总是会在onCreate方法中加上setContentView方法来加载layout,通过findViewById来实现控件的绑定,每次写这么多代码总觉得很烦躁。近......
  • gRPC入门与实操(.NET篇)
    为什么选择gRPC历史长久以来,我们在前后端交互时使用WebApi+JSON方式,后端服务之间调用同样如此(或者更久远之前的WCF+XML方式)。WebApi+JSON是优选的,很重要的一点是......
  • Overt.GrpcTemplate.Service 模板使用教程
     1.Overt.GrpcTemplate.Service .NetCore3.1版本模板名称改成 Overt.GrpcTemplateV3.Service源码地址:https://github.com/overtly/template.gitidentity:Ov......