问题场景:
我想实现在nginx的upstream服务组中获取当服务器是否轮询到了backup;如果轮询到我就在nginx的配置文件中发送一个接口请求,类似于飞书(钉钉)的消息通知 告诉当前的服务已经backup了
实现的方式:
我本地是mac的开发环境下载了openresry通过lua脚本进行请求接口,下载了resty.http
实现了请求,因为请求的地址是https的,我本地使用mkcert
生成了一个免费的SSL的证书,但是请求的时候一直报错:无法解析域名
;
下方是伪代码:
worker_processes 1;
error_log /usr/local/etc/openresty/logs/error.log;
error_log /usr/local/etc/openresty/logs/info.log info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name demo.com;
ssl_certificate /Users/yangfan/demo.com.pem;
ssl_certificate_key /Users/yangfan/demo.com-key.pem;
location / {
root html;
index index.html index.htm;
}
location /fs {
content_by_lua_file /usr/local/etc/openresty/tests/upstream.lua;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
lua的代码:
local http = require "resty.http"
local cjson = require "cjson"
local httpc = http.new()
local userData = {
msg_type = "post",
content = {
post = {
zh_cn = {
title = "222",
content = {
{
tag = "text",
text = "222222",
},
{
tag = "at",
user_id = "all",
},
},
},
},
},
}
local jsonStr = cjson.encode(userData)
local res, err = httpc:request_uri("https://open.f.xxxx.cn/open-apis/bot/v2/hook/xxxxxx", {
method = "POST",
body = jsonStr,
headers = {
["Content-Type"] = "application/json",
},
})
if res == nil then
ngx.log(ngx.INFO,"=====================>>>>>>>>>>>>>>>>>1111111111 ".. err)
else
ngx.log(ngx.INFO,"=====================>>>>>>>>>>>>>>>>>000000000000 ".. res)
ngx.log(ngx.INFO,"=====================>>>>>>>>>>>>>>>>>33333333333333 ".. err)
end
疑惑:
- 目前整个链路是对的吗?我是本地启动openresty进行访问的?
- 还有别的方式可以实现我当前的需求吗?
你应该做的是在nginx之外持续的ping primary 是不是不通。
监控系统应该游离在系统之外