zl程序教程

您现在的位置是:首页 >  后端

当前栏目

grpc(1):Centos 安装java的grpc服务,使用haproxy进行负载均衡,nginx不支持

2023-09-11 14:21:06 时间

GRPC 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C# 支持。
官方网站是:
http://www.grpc.io/
其中java的版本使用netty作为服务器。
关于http2
http2是一个二进制协议。而且是一个长连接。比http1 要快很多。


代码已经放到github上面了。就几个文件。这里就不黏贴代码了。
https://github.com/freewebsys/grpc-java-demo
首先要定义一个idl文件,在src/main/proto目录下面。


option java_package = "io.grpc.examples.helloworld"; option java_outer_classname = "HelloWorldProto"; option objc_class_prefix = "HLW"; package helloworld; // 定义一个grpc接口 service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} // 请求对象,name message HelloRequest { string name = 1; // 返回对象 message HelloReply { string message = 1; }
groupId org.xolstice.maven.plugins /groupId artifactId protobuf-maven-plugin /artifactId version 0.5.0 /version configuration protocArtifact com.google.protobuf:protoc:3.2.0: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

自动进行proto编译,转换成几个java文件。
这个java文件虽然在target下面,但是可以引用到src类里面的。
不用拷贝文件到src里面,可以直接编译通过。

打包:


groupId org.apache.maven.plugins /groupId artifactId maven-assembly-plugin /artifactId version 2.5.5 /version configuration archive manifest mainClass io.grpc.examples.helloworld.HelloWorldServer /mainClass /manifest /archive descriptorRefs descriptorRef jar-with-dependencies /descriptorRef /descriptorRefs /configuration executions execution id make-assembly /id phase package /phase goals goal single /goal /goals /execution /executions /plugin

在java中,有插件可以将所有的jarlib包,都打包成一个jar文件。定义main函数。
就可以直接使用了。方便服务部署。 io.grpc.examples.helloworld.HelloWorldServer
直接启动就可以了。


 public static void main(String[] args) throws IOException, InterruptedException {

 final HelloWorldServer server = new HelloWorldServer();

 server.start();

 server.blockUntilShutdown();

 }

使用client进行测试:


/* Access a service running on the local machine on port 50051 */ String user = "world"; if (args.length 0) { user = args[0]; /* Use the arg as the name to greet if provided */ for (int i = 0; i 100; i ++) { client.greet(user); } finally { client.shutdown(); }
这个地方很奇怪。
proxy_pass 主要是在进行代理的时候,前端是 http2,但是到 upstream 之后就变成了http1.1 这个地方有个强制版本。
proxy_http_version 1.1;
进行http代理的最高版本就是 1.1 不支持http2 的代理。
https://trac.nginx.org/nginx/ticket/923
上面已经说的很清楚了。grpc想使用nginx做代理。
但是人家不支持,并且也没有计划开发。
【No, there are no plans.】
http://mailman.nginx.org/pipermail/nginx/2015-December/049445.html

直接报错:


WARNING: RPC failed: Status{code=UNKNOWN, description=HTTP status code 0

invalid content-type: null

headers: Metadata(:status=000,server=openresty/1.11.2.2,date=Tue, 28 Feb 2017 02:06:26 GMT)

DATA-----------------------------


前端可以了解一下:nginx负载均衡 直接封面图可以发现:最左侧的便是客户机,中间的就是负载均衡的机器,最右侧的便是通过负载后真正访问的服务器,我这里只画了两台服务器,实际情况根据需求可能还会不断增加。