grpc(1):Centos 安裝java的grpc服務,使用haproxy進行負載平衡,nginx不支援

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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) {}}// 請求對象,namemessage 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 0invalid content-type: nullheaders: 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 maybeTerminateChannelINFO: [io.grpc.internal.ManagedChannelImpl-1] Terminated

這個報錯一樣的。
https://github.com/grpc/grpc-java/issues/2559

6,使用haproxy代理 grpc

首先要下載一個最新的haproxy。
然後配置下:vi /etc/haproxy/haproxy.cfg

global        maxconn         20000        log             127.0.0.1 local0frontend test-proxy        bind            :5000        mode           tcp         log             global        option          httplog        option          dontlognull        option          nolinger        maxconn         8000        timeout client  30s        default_backend test-proxy-srvbackend 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://blog.csdn.net/freewebsys/article/details/58584294 未經博主允許不得轉載。
博主地址是: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。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.