gRPC+Nginx架构的部署方法

本文介绍使用Nginx管理gRPC流量,即gRPC+Nginx架构的部署方法。

说明:本文介绍的gRPC搭建示例是针对C++语言的。

1 环境信息

本文示例使用的环境信息如下:

软件名称版本信息
操作系统CentOS Linux release 7.2
gRPC1.10.1-pre1
protobuf3.5.0
NGINX1.13.10

说明:

  1. 根据Nginx官网信息,Nginx从“1.13.10”开始原生支持gRPC,所以必须使用“1.13.10”以上的版本进行NGINX+gRPC框架的部署。

2 安装gRPC和protobuf

2.1 安装开发编译工具包

1. 首先执行下面的命令安装开发编译工具包:

yum groupinstall "Development Tools"

2. 安装完成后,通过rpm命令查看“autoconf”、“libtool”、“pkgconfig”软件包是否安装成功了:

[root@pay0 ~]# rpm -qa|grep autoconf
autoconf-2.69-11.el7.noarch
[root@pay0 ~]# rpm -qa|grep libtool
libtool-2.4.2-22.el7_3.x86_64
[root@pay0 ~]# rpm -qa|grep pkgconfig
pkgconfig-0.27.1-4.el7.x86_64
[root@pay0 ~]#

需要确保上述软件包都已经安装成功了。

2.2 编译安装gRPC

1. 使用git工具下载gRPC代码:

[root@pay0 yaowang]# pwd
/opt/yaowang
[root@pay0 yaowang]# git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc

2. 进入下载下来的gRPC代码目录下,更新gRPC需要的第三方插件:

[root@pay0 yaowang]# cd grpc
[root@pay0 grpc]# pwd
/opt/yaowang/grpc
[root@pay0 grpc]# git submodule update --init

3. 编译gRPC:

[root@pay0 grpc]# make

4. 安装gRPC:

[root@pay0 grpc]# make install

5. 查看gRPC是否安装成功:

[root@pay0 ~]# whereis grpc_cpp_plugin
grpc_cpp_plugin: /usr/local/bin/grpc_cpp_plugin
[root@pay0 ~]#

如果能够查询到系统中已经安装了gRPC的相关插件,说明gRPC安装成功了。

2.3 编译安装protobuf

正常情况下,在安装gRPC的过程中,会自动安装protobuf。protobuf在gRPC的“third_party”目录下:

[root@pay0 protobuf]# pwd
/opt/yaowang/grpc/third_party/protobuf
[root@pay0 protobuf]#

可以通过如下命令查询protobuf是否安装成功了:

[root@pay0 protobuf]# protoc --version
libprotoc 3.5.0
[root@pay0 protobuf]#

如果能够查询到protocbuf的版本号,则说明protobuf安装成功了。如果protobuf没有安装,那么需要执行以下操作进行手动安装:

[root@pay0 grpc]# cd third_party/protobuf/
[root@pay0 protobuf]# pwd
/opt/yaowang/grpc/third_party/protobuf
[root@pay0 protobuf]# make
[root@pay0 protobuf]# make install

执行上述安装操作后,可以再次查询protobuf是否安装成功。

3 构建gRPC示例

3.1 编译生成gRPC的示例程序

在gRPC和protobuf安装正常的情况下,进入到“example”目录中,编译生成gRPC的示例客户端和服务端,命令如下:

[root@pay0 grpc]# cd examples/cpp/helloworld/
[root@pay0 helloworld]# make

正常情况下,运行上述make命令后,会在”helloworld”目录下生成gRPC的示例程序服务端(greeter_server)及客户端(greeter_client)。

说明:如果此处make出现如下错误:

[root@pay0 helloworld]# make
g++ -std=c++11 `pkg-config --cflags protobuf grpc`  -c -o helloworld.grpc.pb.o helloworld.grpc.pb.cc
Package grpc was not found in the pkg-config search path.
Perhaps you should add the directory containing `grpc.pc'
to the PKG_CONFIG_PATH environment variable
No package 'grpc' found

出现上述错误,说明grpc没有添加到pkg-config的搜索路径中,需要先找到”grpc.pc”文件,然后将该文件拷贝到pkg-config的搜索路径下即可,如下:

[root@pay0 grpc]# cp ./libs/opt/pkgconfig/grpc.pc /usr/lib64/pkgconfig/

3.2 运行示例程序

1. 打开一个终端,运行服务端程序:

[root@pay0 helloworld]# ./greeter_server 
Server listening on 0.0.0.0:50051

2. 打开另外一个终端,运行客户端程序:

[root@pay0 helloworld]# ./greeter_client 
Greeter received: Hello world
[root@pay0 helloworld]#

如果在客户端终端中显示上述信息“Greeter received: Hello world”,则说明gRPC的客户端-服务端架构搭建成功了。

注意:如果在运行greeter_server或greeter_client过程中,提示如下错误:

[root@pay0 helloworld]# ./greeter_server 
./greeter_server: error while loading shared libraries: libgrpc++.so.1: cannot open shared object file: No such file or directory
[root@pay0 helloworld]#

这个错误是因为应用程序“greeter_server”找不到需要的共享库“libgrpc++.so.1”,需要先找到错误提示中的共享库,然后将该共享库的路径添加到共享库环境变量中即可,例如:

[root@pay0 helloworld]# export LD_LIBRARY_PATH=/opt/yaowang/grpc/libs/opt/

4 安装Nginx

通过下面步骤安装Nginx:

1. 从Nginx官网上获取版本号不低于“1.13.10”的源码包,本文使用的版本为“1.13.10”;

2. 将Nginx源码包拷贝到OS中,并进行解压,得到Nginx的源码;

3. 进入Nginx源码目录中,执行如下的configure命令:

[root@pay0 nginx-1.13.10]# ./configure --with-http_ssl_module --with-http_v2_module

注意:http_ssl和http_v2模块是Nginx支持gRPC必需的。

4. 编译并安装Nginx:

[root@pay0 nginx-1.13.10]# make
[root@pay0 nginx-1.13.10]# make install

默认情况下,Nginx会安装到“/usr/local/nginx/”目录。

5. 运行以下命令,检查Nginx是否成功安装了:

[root@pay0 nginx]# pwd
/usr/local/nginx
[root@pay0 nginx]# ./sbin/nginx -v
nginx version: nginx/1.13.10
[root@pay0 nginx]#

上述结果显示Nginx安装成功,安装的Nginx版本号为“1.13.10”。

5. 构建Nginx示例

在Nginx安装正常的情况下,执行以下操作构建Nginx示例:
1. 进入到Nginx的安装目录下,检查Nginx配置文件是否正确:

[root@pay0 nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@pay0 nginx]#

2. 如果配置文件检查正常,运行Nginx程序:

[root@pay0 nginx]# ./sbin/nginx

3. 检查Nginx运行状态及端口占用情况:

[root@pay0 nginx]# ps -ef|grep nginx
root     27242     1  0 16:33 ?        00:00:00 nginx: master process ./sbin/nginx
nobody   27243 27242  0 16:33 ?        00:00:00 nginx: worker process
root     27251 20758  0 16:34 pts/6    00:00:00 grep --color=auto nginx
[root@pay0 nginx]# 
[root@pay0 nginx]# netstat -anpo|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      27242/nginx: master  off (0.00/0/0)

从上述结果能够看出,Nginx运行状态正常,并且其监听的端口号为80(默认配置)。

4. 在远端机器上打开一个Web网页,输入Nginx服务的IP和端口号,测试Nginx是否能够正常提供Web服务。正常情况下,远端机器会显示如下的Web页面:

如果出现上图,说明Nginx的示例构建成功。

6. 搭建Nginx+gRPC框架

1. 修改Nginx的配置文件(本文中为“/usr/local/nginx/conf/nginx.conf”),使Nginx能够监听gRPC发送的请求消息,并将该消息转发到gRPC的服务端上。相关的配置如下:

2. 重新编译gRPC的客户端greeter_client,使其通信地址指向Nginx的IP和端口(上一步的配置);
3. 运行greeter_client,能够看到终端会显示如下信息:

[root@pay0 helloworld]# ./greeter_client 
Greeter received: Hello world
[root@pay0 helloworld]#

这说明gRPC的客户端与服务端通信正常。
4. 再观察Nginx的日志,能够看到如下信息:

[root@pay0 logs]# tail -f host.access.log 

127.0.0.1 - - [30/Mar/2018:16:58:50 +0800] "POST /helloworld.Greeter/SayHello HTTP/2.0" 200 18 "-" "grpc-c++/1.10.1-pre1 grpc-c/6.0.0-pre1 (linux; chttp2; glamorous)" "-"

上述日志信息说明,Nginx获取并转发了来自gRPC的消息,即实现了通过Nginx管理gRPC服务的目的。

至此,一个最简单的gRPC+Nginx的架构就搭建完成了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liitdar

赠人玫瑰,手有余香,君与吾共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值