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

前端之家收集整理的这篇文章主要介绍了grpc(1):Centos 安装java的grpc服务,使用haproxy进行负载均衡,nginx不支持前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1,关于grpc


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 要快很多。

2,java demo 服务端和客户端


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

  1. Syntax = "proto3";
  2. //定义包,类名称
  3. option java_multiple_files = true;
  4. option java_package = "io.grpc.examples.helloworld";
  5. option java_outer_classname = "HelloWorldProto";
  6. option objc_class_prefix = "HLW";
  7.  
  8. package helloworld;
  9.  
  10. // 定义一个grpc接口
  11. service Greeter {
  12. // Sends a greeting
  13. rpc SayHello (HelloRequest) returns (HelloReply) {}
  14. }
  15.  
  16. // 请求对象,name
  17. message HelloRequest {
  18. string name = 1;
  19. }
  20.  
  21. // 返回对象
  22. message HelloReply {
  23. string message = 1;
  24. }

3,配置pom.xml 文件


定义一个pom的xml文件,点击install 会将proto文件转换成java类。

  1. <extensions>
  2. <extension>
  3. <groupId>kr.motd.maven</groupId>
  4. <artifactId>os-maven-plugin</artifactId>
  5. <version>1.4.1.Final</version>
  6. </extension>
  7. </extensions>
  8. <plugins>
  9. <plugin>
  10. <groupId>org.xolstice.maven.plugins</groupId>
  11. <artifactId>protobuf-maven-plugin</artifactId>
  12. <version>0.5.0</version>
  13. <configuration>
  14. <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}</protocArtifact>
  15. <pluginId>grpc-java</pluginId>
  16. <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
  17. </configuration>
  18. <executions>
  19. <execution>
  20. <goals>
  21. <goal>compile</goal>
  22. <goal>compile-custom</goal>
  23. </goals>
  24. </execution>
  25. </executions>
  26. </plugin>

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

打包:

  1. <!-- 打包成一个jar 文件。-->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-assembly-plugin</artifactId>
  5. <version>2.5.5</version>
  6. <configuration>
  7. <archive>
  8. <manifest>
  9. <mainClass>io.grpc.examples.helloworld.HelloWorldServer</mainClass>
  10. </manifest>
  11. </archive>
  12. <descriptorRefs>
  13. <descriptorRef>jar-with-dependencies</descriptorRef>
  14. </descriptorRefs>
  15. </configuration>
  16. <executions>
  17. <execution>
  18. <id>make-assembly</id>
  19. <phase>package</phase>
  20. <goals>
  21. <goal>single</goal>
  22. </goals>
  23. </execution>
  24. </executions>
  25. </plugin>

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

4,启动server


启动server。

  1. public static void main(String[] args) throws IOException,InterruptedException {
  2. final HelloWorldServer server = new HelloWorldServer();
  3. server.start();
  4. server.blockUntilShutdown();
  5. }

使用client进行测试:

  1. HelloWorldClient client = new HelloWorldClient("localhost",50051);
  2. try {
  3. /* Access a service running on the local machine on port 50051 */
  4. String user = "world";
  5. if (args.length > 0) {
  6. user = args[0]; /* Use the arg as the name to greet if provided */
  7. }
  8. for (int i = 0; i < 100; i ++) {
  9. client.greet(user);
  10. }
  11. } finally {
  12. client.shutdown();
  13. }

5,不能使用Nginx进行grpc代理


虽然Nginx已经支持了http2,但是不能适应Nginx进行负载均衡。
这个地方很奇怪。
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

直接报错:

  1. WARNING: RPC Failed: Status{code=UNKNOWN,description=HTTP status code 0
  2. invalid content-type: null
  3. headers: Metadata(:status=000,server=openresty/1.11.2.2,date=Tue,28 Feb 2017 02:06:26 GMT)
  4. DATA-----------------------------
  5. ����HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 504f5354202f68656c6c6f776f726c642e47726565746572,cause=null}
  6. Feb 28,2017 10:06:27 AM io.grpc.internal.ManagedChannelImpl maybeTerminateChannel
  7. INFO: [io.grpc.internal.ManagedChannelImpl-1] Terminated

这个报错一样的。
https://github.com/grpc/grpc-java/issues/2559

@H_170_502@6,使用haproxy代理 grpc

首先要下载一个最新的haproxy。
然后配置下:vi /etc/haproxy/haproxy.cfg

  1. global
  2. maxconn 20000
  3. log 127.0.0.1 local0
  4.  
  5. frontend test-proxy
  6. bind :5000
  7. mode tcp
  8. log global
  9. option httplog
  10. option dontlognull
  11. option nolinger
  12. maxconn 8000
  13. timeout client 30s
  14. default_backend test-proxy-srv
  15.  
  16.  
  17. backend test-proxy-srv
  18. mode tcp
  19. server app1 127.0.0.1:50051 check
  20. server app1 127.0.0.1:50052 check

已经在本机跑了两个java的服务端,一个端口50051,一个50052。

  1. nohup java -jar grpc-java-demo-1.0-50051.jar > nohup-1.log 2>&1 &
  2. nohup java -jar grpc-java-demo-1.0-50052.jar > nohup-2.log 2>&1 &

客户端调用服务修改成端口 5000。即可以调用成功。
在第一次创建 http2链接的时候,会保持一个链接,以后就都是这个服务访问。
除非服务重启,或者客户端新重新连接。

7,总结


本文的原文连接是: http://www.jb51.cc/article/p-edsixtel-bew.html 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

总结下,grpc还是值得学习的。

  1. 10.0.2.2 - - [27/Feb/2017:21:06:26 -0500] "POST /helloworld.Greeter/SayHello HTTP/2.0" 009 230 "-" "grpc-java-netty/1.1.2" "-"

grpc访问的日志可以看到服务的url,方法。 在做业务逻辑处理,比较容易接受。搭建服务也非常的快速呢。 继续研究grpc。

猜你在找的CentOS相关文章