作者: whooyun发表于: 2023-07-21 15:54
openresty | nacos都去官网下载
先做安装的前置工作,主要就是把依赖项安装下:
sudo apt-get update
sudo apt-get install libpcre3-dev libssl-dev perl make build-essential curl zlib1g-dev
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates
##配置依赖模块
./configure
##默认, --prefix=/usr/local/openresty 程序会被安装到/usr/local/openresty目录。
## 编译安装(make命令是系统的)
gmake
gmake install
##会被默认安装到 /usr/local/openresty
##可以查看安装了哪些模块,以及版本号小v
./nginx -V
## 运行openresty,运行方式跟nginx一样
/usr/local/openresty/nginx/sbin
################lua 进行upstream健康检查 ##############################
1、在nginx的conf配置中增加upstream配置
upstream nacos_cluster {
server 192.168.80.131:8848;
server 192.168.80.132:8848;
}
lua_shared_dict healthcheck 1m;
2、增加lua代码
init_worker_by_lua_block {
local hc = require "resty.upstream.healthcheck"
local upstream_name = "nacos_cluster"
local function get_status()
local primary_peers, err = hc.get_primary_peers(upstream_name)
if not primary_peers then
ngx.log(ngx.ERR, "failed to get primary peers: ", err)
return
end
for i, peer in ipairs(primary_peers) do
ngx.say("primary peer ", i, ": ", peer.name, " (", peer.id, ") status: ", peer.down and "down" or "up")
end
local backup_peers, err = hc.get_backup_peers(upstream_name)
if not backup_peers then
ngx.log(ngx.ERR, "failed to get backup peers: ", err)
return
end
for i, peer in ipairs(backup_peers) do
ngx.say("backup peer ", i, ": ", peer.name, " (", peer.id, ") status: ", peer.down and "down" or "up")
end
end
local ok, err = hc.spawn_checker{
shm = "healthcheck", -- defined by "lua_shared_dict"
upstream = upstream_name, -- defined by "upstream"
type = "http",
http_req = "GET /nacos/v1/console/server/state HTTP/1.0\r\nHost: localhost:8848\r\n\r\n",
interval = 2000, -- run the check cycle every 2 sec
timeout = 1000, -- 1 sec is the timeout for network operations
fall = 3, -- # of successive failures before turning a peer down
rise = 2, -- # of successive successes before turning a peer up
valid_statuses = {200, 302,304}, -- a list valid HTTP status code
concurrency = 10, -- concurrency level for test requests
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker for upstream ", upstream_name, ": ", err)
return
end
}
3、在nginx.conf配置中导入域名配置及lua文件
include /usr/local/openresty/nginx/conf/lua/upstream_health.lua;
include nacos_upstream.conf;