接下来开始编译安装Nginx:
cd /data/nginx/nginx-1.18.0 patch -p1 < /data/nginx/modules/nginx_upstream_check_module-master/check_1.16.1+.patch ./configure --prefix=http://www.likecs.com/data/nginx --add-module=http://www.likecs.com/data/nginx/modules/nginx_upstream_check_module-master --add-module=http://www.likecs.com/data/nginx/modules/nginx-upsync-module-2.1.2 make make install上面的编译和安装过程无论怎么调整,都会出现部分依赖缺失导致make异常,估计是这两个模块并不支持太高版本的Nginx。(生产上用了一个版本比较低的OpenResty,这里想复原一下使用相对新版本Nginx的踩坑过程)于是尝试降级进行编译,下面是参考多个Issue后得到的相对比较新的可用版本组合:
nginx-1.14.2.tar.gz
xiaokai-wang/nginx_upstream_check_module,使用补丁check_1.12.1+.patch
nginx-upsync-module:release:v2.1.2
# 提前把/data/nginx下除了之前下载过的modules目录外的所有文件删除 cd /data/nginx wget tar -zxvf nginx-1.14.2.tar.gz开始编译安装:
cd /data/nginx/nginx-1.14.2 patch -p1 < /data/nginx/modules/nginx_upstream_check_module-master/check_1.12.1+.patch ./configure --prefix=http://www.likecs.com/data/nginx --add-module=http://www.likecs.com/data/nginx/modules/nginx_upstream_check_module-master --add-module=http://www.likecs.com/data/nginx/modules/nginx-upsync-module-2.1.2 make && make install安装完成后通过/data/nginx/sbin/nginx命令启动即可。
启用动态负载均和健康检查首先编写一个简易的HTTP服务,因为Java比较重量级,这里选用Golang,代码如下:
package main import ( "flag" "fmt" "net/http" ) func main() { var host string var port int flag.StringVar(&host, "h", "127.0.0.1", "IP地址") flag.IntVar(&port, "p", 9000, "端口") flag.Parse() address := fmt.Sprintf("%s:%d", host, port) http.HandleFunc("/ping", func(writer http.ResponseWriter, request *http.Request) { _, _ = fmt.Fprintln(writer, fmt.Sprintf("%s by %s", "pong", address)) }) http.HandleFunc("http://www.likecs.com/", func(writer http.ResponseWriter, request *http.Request) { _, _ = fmt.Fprintln(writer, fmt.Sprintf("%s by %s", "hello world", address)) }) err := http.ListenAndServe(address, nil) if nil != err { panic(err) } }编译:
cd src set GOARCH=amd64 set GOOS=linux go build -o ../bin/app app.go这样子在项目的bin目录下就得到一个Linux下可执行的二级制文件app,分别在端口9000和9001启动两个服务实例:
# 记得先给app文件的执行权限chmod 773 app nohup ./app -p 9000 >/dev/null 2>&1 & nohup ./app -p 9001 >/dev/null 2>&1 &修改一下Nginx的配置,添加upstream:
# /data/nginx/conf/nginx.conf部分片段 http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream app { # 这里是consul的leader节点的HTTP端点 upsync 192.168.56.200:8510/v1/kv/upstreams/app/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off; # consul访问不了的时候的备用配置 upsync_dump_path /data/nginx/app.conf; # 这里是为了兼容Nginx的语法检查 include /data/nginx/app.conf; # 下面三个配置是健康检查的配置 check interval=1000 rise=2 fall=2 timeout=3000 type=http default_down=false; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } server { listen 80; server_name localhost; location / { proxy_pass ; } # 健康检查 - 查看负载均衡的列表 location /upstream_list { upstream_show; } # 健康检查 - 查看负载均衡的状态 location /upstream_status { check_status; access_log off; } } } # /data/nginx/app.conf server 127.0.0.1:9000 weight=1 fail_timeout=10 max_fails=3; server 127.0.0.1:9001 weight=1 fail_timeout=10 max_fails=3;手动添加两个HTTP服务进去Consul中:
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' :8510/v1/kv/upstreams/app/127.0.0.1:9000 curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' :8510/v1/kv/upstreams/app/127.0.0.1:9001最后重新加载Nginx的配置即可。
动态负载均衡测试前置工作准备好,现在尝试动态负载均衡,先从Consul下线9000端口的服务实例:
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10, "down":1}' :8510/v1/kv/upstreams/app/127.0.0.1:9000