推荐 最新
复古直男

如何在nginx内部发送接口请求呢?

问题场景: 我想实现在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进行访问的? * 还有别的方式可以实现我当前的需求吗?

0
1
0
浏览量148
拽嘻嘻

nginx的健康检查机制的执行进程关系?

背景:我有两台机器,分别是backend1(主机器)backend2(backup),大致如下: upstream backend { server backend1.example.com:8080; server backend2.example.com:8080 backup; } 我想实现的是 在服务"backend1"机器如果链接超时或者5xx 那么就直接到"backend2"这台机器上,等"backend1"机器如果恢复可以正常访问"backend1"。以下是我在网上找的资料本地配置的 ... http { ... upstream nuxt_upstream { server backend1.example.com:8080 max_fails=3 fail_timeout=10s; server backend2.example.com:8080 backup; } server { listen 80; server_name api; location / { # 配置响应服务器的错误类型 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_next_upstream_timeout 5s; # 重试总共的时间 proxy_next_upstream_tries 3; # 重试几次,包含第一次 proxy_pass http://nuxt_upstream; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 配置相应服务器的超时时间 proxy_connect_timeout 2s; # 连接后台服务器的超时时间 proxy_read_timeout 2s; # 从后台服务器读取数据的超时时间 proxy_send_timeout 2s; # 向后台服务器发送数据的超时时间 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 但是我不是很清楚的有几个点: 1. 当一个请求进入nginx的时候我通过"proxy_next_upstream"捕获了当前错误的类型进行上游服务的轮询,设置的"proxy_next_upstream_timeout 5s" 具体作用在哪?包含 "proxy_xx_timeout"的时间吗? 2. 我在网上查询得知"proxy_xx_timeout"配置是跟 "upstream" 其中一个sever相关的?那么如果我在设置"backend1.example.com:8080 max_fails=3 fail_timeout=10s;" 里面的"fail_timeout" 对外面设置的"proxy_xx_timeout" 有冲突吗?他们的关系是什么? 总结 就是我在"upstream" 设置了 "fail_timeout=10s" 和在外面设置的 "proxy_next_upstream_timeout 5s"加上"proxy_xx_timeout 2s" 他们执行的顺序是什么?什么冲突吗? 辛苦相关的linux大神解答一下!

0
1
0
浏览量131
ciiiiinema

请教一下,spring boot下,如何获取加网关代理的nacos配置?

基本环境: 1. 根据手册,本地搭建一个nacos server, 版本 2.2.3, standalone 2. 利用apisix,给nacos server加一层网关代理{ "uri": "/nacos/*", "name": "local.8848", "upstream_id": "xx", "enable_websocket": true, "status": 1 } { "nodes": [ { "host": "host.docker.internal", "port": 8848, "weight": 100 } ], "type": "roundrobin", "pass_host": "pass", "name": "local.nacos" } 访问: "http://localhost:9080/nacos/index.html" 都很正常的,也可以用 curl -X GET "http://localhost:9080/nacos/v1/cs/configs?dataId=nacos.cfg.dataId" 配置了值: namespace=public dataId=nacos.cfg.dataId group=DEFAULT_GROUP value: // properties time.pattern=yyyy-mm-dd 目前问题是: 1. 用spring boot读取不到配置 spring boot里的配置就非常简单, nacos.config.server-addr=http://localhost:9080 spring boot代码: // 版本 0.2.12 @SpringBootApplication @NacosPropertySource(dataId = "nacos.cfg.dataId", type = ConfigType.PROPERTIES, autoRefreshed = true) public class NacosConfigApplication { public static void main(String[] args) { SpringApplication.run(NacosConfigApplication.class, args); } } @Controller public class ConfigController { @NacosValue(value = "${time.pattern}", autoRefreshed = true) private String value; @RequestMapping(value = "/config/get", method = GET) @ResponseBody public String get() { return value; } } 直接用sdk也不行 com.alibaba.nacos nacos-client 2.3.0 // 2.2.3, 2.2.4都试过 public static void main(String[] args) throws Exception { String serverAddr = "http://localhost:9080"; 就可以了 String dataId = "nacos.cfg.dataId"; String group = "DEFAULT_GROUP"; Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); // 换成 serverAddr=http://localhost:8848, content就是配置的值了 System.out.println(content); // content=null, }

0
1
0
浏览量25