grpc(1):Centos 安装java的grpc服务,使用haproxy进行负载均
1,关于grpcGRPC 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc,grpc-java,grpc-go. 其中 C 版本支持 C,C++,Node.js,Python,Ruby,Objective-C,PHP 和 C# 支持。 2,java demo 服务端和客户端代码已经放到github上面了。就几个文件。这里就不黏贴代码了。 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文件。 打包: <!-- 打包成一个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函数。 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进行负载均衡。 直接报错: 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
这个报错一样的。 首先要下载一个最新的haproxy。 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。即可以调用成功。 7,总结本文的原文连接是: http://www.jb51.cc/article/p-edsixtel-bew.html 未经博主允许不得转载。 总结下,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。 (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |