grpc(1):Centos 安装java的grpc服务,使用haproxy进行负载均衡,nginx不支持 - Go语言中文社区

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

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-----------------------------
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_34409822/article/details/90491918
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 12:44:41
  • 阅读 ( 1372 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢