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目录下面。
- Syntax = "proto3";
- //定义包,类名称
- option java_multiple_files = true;
- 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;
- }
3,配置pom.xml 文件
定义一个pom的xml文件,点击install 会将proto文件转换成java类。
- <extensions>
- <extension>
- <groupId>kr.motd.maven</groupId>
- <artifactId>os-maven-plugin</artifactId>
- <version>1.4.1.Final</version>
- </extension>
- </extensions>
- <plugins>
- <plugin>
- <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里面,可以直接编译通过。
打包:
- <!-- 打包成一个jar 文件。-->
- <plugin>
- <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
直接启动就可以了。
4,启动server
启动server。
- public static void main(String[] args) throws IOException,InterruptedException {
- final HelloWorldServer server = new HelloWorldServer();
- server.start();
- server.blockUntilShutdown();
- }
使用client进行测试:
- HelloWorldClient client = new HelloWorldClient("localhost",50051);
- try {
- /* 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();
- }
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
直接报错:
- 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-----------------------------
- ����HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 504f5354202f68656c6c6f776f726c642e47726565746572,cause=null}
- Feb 28,2017 10:06:27 AM io.grpc.internal.ManagedChannelImpl maybeTerminateChannel
- INFO: [io.grpc.internal.ManagedChannelImpl-1] Terminated
这个报错一样的。
https://github.com/grpc/grpc-java/issues/2559
首先要下载一个最新的haproxy。
然后配置下:vi /etc/haproxy/haproxy.cfg
- global
- maxconn 20000
- log 127.0.0.1 local0
-
- frontend test-proxy
- bind :5000
- mode tcp
- log global
- option httplog
- option dontlognull
- option nolinger
- maxconn 8000
- timeout client 30s
- default_backend test-proxy-srv
-
-
- backend test-proxy-srv
- mode tcp
- server app1 127.0.0.1:50051 check
- server app1 127.0.0.1:50052 check
已经在本机跑了两个java的服务端,一个端口50051,一个50052。
- nohup java -jar grpc-java-demo-1.0-50051.jar > nohup-1.log 2>&1 &
- 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还是值得学习的。
- 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。