编译安装ngx_http_consul_backend_module到nginx中
最近在研究将consul内容load到nginx中
在github上看到hashicorp公司的一款nginx module
https://github.com/hashicorp/ngx_http_consul_backend_module
其着重说明了这个代码就是example purpose的目的
其表明:
1. 从C来调用Go是可行的
2. nginx直接link到consul是可行的
编译安装过程
参考其安装过程,我使用centos7.2
安装go环境 大于1.9
1. yum -yqq install build-essential curl git libpcre3 libpcre3-dev libssl-dev zlib1g-dev
2. cd /tmp
wget https://nginx.org/download/nginx-1.12.2.tar.gz
解压
3. cd /tmp
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
解压
4. cd /tmp
git clone https://github.com/hashicorp/ngx_http_consul_backend_module.git
5. Compile the Go code as a shared C library which nginx will dynamically load.
This uses CGO and binds to the nginx development kit
cd /tmp/ngx_http_consul_backend_module/src下
mkdir -p /usr/local/nginx/ext
CGO_CFLAGS="-I /tmp/ngx_devel_kit-0.3.0/src"
go build -buildmode=c-shared -o /usr/local/nginx/ext/ngx_http_consul_backend_module.so ngx_http_consul_backend_module.go
这样在/usr/local/nginx/ext下就会生成ngx_http_consul_backend_module.so文件,这个文件会在nginx运行时被重新dlopen
6. 使用这个module来编译并安装nginx
cd /tmp/nginx-1.12.2
CFLAGS="-g -O0"
默认似乎也会将nginx安装到/usr/local/nginx下
安装完成之后echo $?来查看./configure结果
如果为0,说明过程中没有错误
之后执行make命令
编译完成的nginx放在
当前文件夹是/tmp/nginx-1.12.2
这里有一个技巧,就是我们编译nginx可能会编译好多次才能成功,如果我们每一次都install,会把/usr/local/nginx下的内容都清空,比如我们之前配置的nginx.conf文件。所以这里使用链接的方式只替换掉nginx的可执行文件
具体方法是:
cd /usr/local/nginx/sbin/
mv nginx nginx_bak
cp -v /tmp/nginx-1.12.2/objs/nginx .
当然这里也可以提前先./nginx -s stop,就不需要使用后面的两个kill了
这样执行./sbin/nginx就启动刚刚编译好的nginx了
解决的问题
1. 编译时报错GOROOT路径下github.com/hashicorp/consul/api 没找到
解决方法:
直接打开/tmp/ngx_http_consul_backend_module/src
查看ngx_http_consul_backend_module.go源码
发现
于是要把github.com/hashicorp/consul/api放到GOROOT下
我这里直接把整个consul源码都放到其路径下(这应该是就是自举的方式,用自己来编译自己)
其路径是/usr/local/go/src/github.com/hashicorp/consul
2. 编译时报错:
中文:附注:需要类型‘const char *’,但实参的类型为‘u_char *’
英文:passing argument 1 of 'strlen' differ in signedness
参考:passing argument 1 of 'strlen' differ in signedness
解决方法
cd /tmp/ngx_http_consul_backend_module/src
vim ngx_http_consul_backend_module.c
将上面这句改为下面这句,OK了
3. 编译成功后,访问nginx的时候,error.log报错
go module not found
解决方法:
修改源码ngx_http_consul_backend_module.c
将其so文件路径更改为绝对路径
4. 访问nginx时报错:failed to lookup service "school-service": Get http://127.0.0.1:8500/v1/health/service/school-service?passing=1&stale=: dial tcp 127.0.0.1:8500: connect: connection refused
解决方法:修改源码ngx_http_consul_backend_module.go
设定consul的ip:port
最后效果
调用链路
consul界面
nginx配置
可以看到nginx配置中不再需要配置ip:port,如果有多个服务,也不需要配置upstream
直接使用
consul $backend school-service
proxy_pass http://$backend
简化了配置
直接访问:
http://192.168.211.112/getSchoolDetails/abcschoo
在其github上也说明了整个的flow
通过上面的flow说明,可以看出此种方法的两个问题
1. 其是random result,也就说明并没有负载均衡
2. 每一次请求都访问consul,这样并不好。应该是nginx本地缓存一份consul service list,只有当consul变更的时候才更新
3. 这种方式github上也说明,只是inspiration only,并不能用在生产环境
其也给出了可选方法
一个是weibo的nginx upstream sync
另一个consul template可能要reload nginx
接下来对这两个项目研究下