grpc1: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目录下面。

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

6,使用haproxy代理 grpc

首先要下载一个最新的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。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


gRPC 前言 为什么使用gRPC 传输协议 传输效率 性能消耗 gRPC入门 gRPC流 证书认证 使用根证书 gRPC实现token认证 和Web服务共存 验证器 REST接口 grpcurl工具
参考文章: 1.&#160;https://www.cnblogs.com/kaixinyufeng/p/9651513.html 2.&#160;http://jia-shun.cn/2018/08
今天给大家翻译一篇由ASP.NET首席开发工程师 &quot;James Newton King&quot; 前几天发表的一篇博客,文中带来了一个实验性的产品gRPC Web。大家可以点击文末的讨论帖
上一篇文章我带着大家体验了一把《 &quot;ASP.NET Core 3.0 上的gRPC服务模板初体验(多图)&quot; 》,如果有兴趣的可以点击链接进行查看,相信跟着做的你,也是可以跑起来的。
早就听说ASP.NET Core 3.0中引入了gRPC的服务模板,正好趁着家里电脑刚做了新系统,然后装了VS2019的功夫来体验一把。同时记录体验的过程。如果你也想按照本文的步骤体验的话,那你得先安
这篇笔记主要是记录学习历程而不是怎么用~,以及protobuffers 和 gprc 各种文档的地址,等过上大半年后通过这篇笔记帮助自己快速重新掌握这个技术点 一、Protocolbuffers 关于
最近GRPC很火,感觉整RPC不用GRPC都快跟不上时髦了。 gRPC设计 gRPC是一种与语言无关的高性能远程过程调用 (RPC) 框架。刚好需要使用一个的RPC应用系统,自然而然就盯上了它,但是它
   gRPC是google开源提供的一个RPC软件框架,它的特点是极大简化了传统RPC的开发流程和代码量,使用户可以免除许多陷阱并聚焦于实际应用逻辑中。作为一种google的最新RPC解决方案,gRPC具备了以下这些强项: 1、gRPC在HTTP/2协议上用protobuf取代了json实现了最佳效率 2、用IDL(Interface Definition Language),一种简单的描述语言
  接着上期讨论的gRPC unary服务我们跟着介绍gRPC streaming,包括: Server-Streaming, Client-Streaming及Bidirectional-Streaming。我们首先在.proto文件里用IDL描述Server-Streaming服务: /* * responding stream of increment results */ servi
我已经设法通过GRPC使用流媒体模式的服务帐户为我的 Android应用程序运行Google Cloud Speech.但是,根据我所读到的内容,出于安全原因,我不应该在其中部署具有这些凭据的Android应用程序(当前存储为资源中的JSON文件).正确的是创建一个API密钥,如下所述: https://cloud.google.com/speech/docs/common/auth 这允许我限制
  安装protobuf go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/protoc-gen-go 此时会生成protoc-gen-go,protoc一般是获取已经编译好的可执行文件(https://github.com/google/protobuf/releases)   安装gR
一、grpc安装 将 https://github.com/google/go-genproto 放到 $GOPATH/src/google.golang.org/genproto 将 https://github.com/grpc/grpc-go 放到 $GOPATH/src/google.golang.org/grpc 将 https://github.com/golang/t
参考URL: https://segmentfault.com/a/1190000015220713?utm_source=channel-hottest gRPC 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, P
我试图在电子应用程序中要求grpc,但我收到以下错误: Error: dlopen(/srv/node_modules/grpc/src/node/extension_binary/grpc_node.node, 1): Symbol not found: _GENERAL_NAME_free Referenced from: /srv/node_modules/grpc/src/node/e
我试图调用GRPC端点,但我想提供客户身份验证标头.我在哪里指定这个? var client = new proto.Publisher('127.0.0.1:50051', grpc.credentials.createInsecure()); var customHeader = { 'authorization': 'secret' } client.publish(d
我正在尝试创建一个 java grpc客户端来与go中的服务器通信.我是grpc的新手所以遵循本教程 gRPC Java Tutorial.在这些示例中,它们指的是阻塞和非阻塞存根,它们似乎是从 github的其他地方导入的. import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideBlockingStub; import io.gr
我正在尝试做类似下面的事情(即使用流式grpc调用从客户端向服务器发送数据).代码参考取自官方网站上给出的grpc示例,用于解释目的: 客户端代码: ClientContext context; context.AddMetadata("authorization", "abcd"); context.set_deadline(...); std::unique_ptr<ClientWriter
什么是gRPC gRPC是google开源的一个高性能、跨语言的RPC框架,基于HTTP2协议,采用ProtoBuf 定义的IDL。 gRPC 的主要优点是: 现代高性能轻量级 RPC 框架。 协定优先 API 开发,默认使用协议缓冲区,允许与语言无关的实现。 可用于多种语言的工具,以生成强类型服务器和客户端。 支持客户端、服务器和双向流式处理调用。 使用 Protobuf 二进制序列化减少对网络
一.简介 gRPC 是一个由Google开源的,跨语言的,高性能的远程过程调用(RPC)框架。 gRPC使客户端和服务端应用程序可以透明地进行通信,并简化了连接系统的构建。它使用HTTP/2作为通信协议,使用 Protocol Buffers 作为序列化协议。 它的主要优点: 现代高性能轻量级 RPC 框架。 约定优先的 API 开发,默认使用 Protocol Buffers 作为描述语言,允许
目录 ASP.NET Core 3.0 使用gRPC ASP.NET Core 3.0 gRPC 双向流 ASP.NET Core 3.0 gRPC 认证授权 一.前言 在前一文 《ASP.NET Core 3.0 使用gRPC》中有提到 gRPC 支持双向流调用,支持实时推送消息,这也是 gRPC的一大特点,且 gRPC 在对双向流的控制支持上也是非常强大的。 二. 什么是 gRPC 流 gRP