GRPC (1): Centos installation Java GRPC Service, load Balancing using Haproxy, Nginx does not support

Source: Internet
Author: User
Tags haproxy nginx load balancing
This is a creation in Article, where the information may have evolved or changed.

1, about GRPC

GRPC is a high-performance, open-source, and generic RPC framework for mobile and HTTP/2 designs. Currently available in C, Java and Go language versions: Grpc, Grpc-java, Grpc-go. The C version supports C, C + +, node. js, Python, Ruby, Objective-c, PHP and C # support.
The official website is:
http://www.grpc.io/
Where Java's version uses Netty as the server.
About HTTP2
HTTP2 is a binary protocol. And it's a long connection. Much faster than HTTP1.

2,java Demo server and Client

The code is already on GitHub. Just a few files. There's no sticking code here.
Https://github.com/freewebsys/grpc-java-demo
First, you define an IDL file, under the Src/main/proto directory.

syntax =  "Proto3" ;//define Package, class name option  java_multiple_files = true ; option  java_package =  "Io.grpc.examples.helloworld" ; option  java_outer_classname =  "Helloworldproto" ; Span class= "Hljs-keyword" >option objc_class_prefix =  "HLW" ;p ackage helloworld;//defines a GRPC interface service Greeter {//sends a greeting RPC SayHello (hellorequest) returns (helloreply) {}}//request to Like, Namemessage  hellorequest {string  name = 1 ;} Return object message  helloreply {string  message  = 1 ;} 

3. Configure the Pom.xml file

To define a pom XML file, click Install to convert the proto file into a Java class.

<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>

Automatically proto compiled and converted into several Java files.
Although this Java file is under target, it can be referenced to the SRC class.
Do not copy files into SRC, can be directly compiled through.

Packaged:

<!--packaged into a jar file. -            <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>

In Java, there are plugins that can package all jarlib packages into a single jar file. Defines the main function.
You can use it directly. Ease of service deployment. Io.grpc.examples.helloworld.HelloWorldServer
It can be started directly.

4, start the server

Start the server.

  publicstaticvoidmainthrows IOException, InterruptedException {    finalnew HelloWorldServer();    server.start();    server.blockUntilShutdown();  }

Test using the client:

Helloworldclient client = new Helloworldclient ("localhost",50051);Try{/* Access a serviceRunning  on  the LocalMachine onPort50051*/String user ="World";if(args.length>0) {user = args[0]; /* Use theArg as  the name  toGreetifProvided */} for(int i =0; I < -;      i + +) {client.greet (user);    }} finally {Client.shutdown (); }

5, can not use Nginx for GRPC agent

Although Nginx has supported the HTTP2, it cannot adapt to nginx load balancing.
This place is very strange.
Proxy_pass mainly in the time of the agent, the front end is HTTP2, but to upstream after it became http1.1 this place has a mandatory version.
Proxy_http_version 1.1;
The highest version of HTTP proxy is the 1.1 agent that does not support HTTP2.
https://trac.nginx.org/nginx/ticket/923
The above has been said very clearly. Grpc want to use Nginx proxy.
But people do not support it, and there is no plan to develop it.
"No, there is no plans."
Http://mailman.nginx.org/pipermail/nginx/2015-December/049445.html

Direct error:

WARNING:RPC Failed:status{code=unknown, description=http Status code0Invalid Content-type:nullheaders:Metadata (: status= the, server=openresty/1.11. 2. 2, Date=tue, -Feb .  Geneva: .: -GMT) DATA-----------------------------http/2Client Preface string missingorCorrupt. Hex dump for received bytes:504f5354202f68656c6c6f776f726c642e47726565746572, Cause=null}feb -, . Ten: .: -AM IO. Grpc. Internal. ManagedchannelimplMaybeterminatechannelINFO:[IO. Grpc. Internal. Managedchannelimpl-1] Terminated

This error is the same.
https://github.com/grpc/grpc-java/issues/2559

6, use Haproxy proxy grpc

The first thing to download is a new haproxy.
Then configure under: Vi/etc/haproxy/haproxy.cfg

GlobalMaxconn20000        Log             127.0. 0. 1Local0frontend Test-proxyBind: theMode TCPLog             GlobalOption httplog option dontlognull option Nolinger maxconn8000Timeout client -S default_backend test-proxy-srvBackend Test-proxy-srvMode TCP Server App1127.0. 0. 1:50051Check server App1127.0. 0. 1:50052Check

Already on this machine ran two Java server, one port 50051, one 50052.

Nohup Java-jarGrpc-java-demo-1.0-50051.Jar>Nohup-1.Log 2>&1 &Nohup Java-jarGrpc-java-demo-1.0-50052.Jar>Nohup-2.Log 2>&1 &

The client invoke service is modified to port 5000. That can be called success.
The first time you create a HTTP2 link, you'll keep a link that will be accessed by the service in the future.
Unless the service restarts, or the client is newly reconnected.

7, summary

The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/58584294 not allowed to reprint without the Bo master.
Bo main address is: Http://blog.csdn.net/freewebsys

In conclusion, GRPC is still worth learning.

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

GRPC access to the log can see the URL of the service, method.
In doing business logic processing, relatively easy to accept. The building service is also very fast.
Continue to study grpc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.