【发布时间】:2019-08-22 07:53:48
【问题描述】:
我为我的应用程序创建了一个 API 网关,它将充当其他微服务的前端控制器。 在我的生产设置中,我使用 Nginx 作为网关的反向代理
API 网关在 8080 端口上运行
Nginx 配置如下:
网关-api.conf:
server {
listen 80;
server_name api.example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://localhost:30010/;
keepalive_timeout 500s;
}
keepalive_timeout 500s;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
}
nginx.conf 中的超时设置:
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
Spring 云网关 gradle 文件:
compile('org.springframework.cloud:spring-cloud-starter-gateway')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
compile("org.springframework.boot:spring-boot-starter-actuator")
compile('org.springframework.boot:spring-boot-starter-security')
springBootVersion=2.0.3.RELEASE
springDMPVersion=1.0.4.RELEASE
springPlatformBomVersion=Cairo-SR2
springCloudVersion=Finchley.RELEASE
网关应用:
@SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
@EntityScan(basePackages = {"com.example"})
@EnableFeignClients(basePackages = "com.example")
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
问题陈述:
在我的一个微服务中,一个 REST API 需要 3 多分钟才能完成。
如果我通过nginx(api.example.com) 调用此 API,它会在恰好在 1 分钟后失败并给出 HTTP 状态 504。
卷曲:
curl --request GET \
--url http://api.example.com/hellomicroservice/api/take/moretime
错误:
504 Timeout while reading the response from Server
nginx 或 API 网关中没有错误日志。
从 nginx 访问日志:
203.129.213.102 - - [01/Apr/2019:08:14:33 +0000] "GET hellomicroservice/api/take/moretime HTTP/1.1" 499 0 "-" "PostmanRuntime/7.3.0"
但是当我直接调用同一个 API 到网关(在网关端口 8080 上)时,请求被成功处理。
curl 与网关端口:
curl --request GET \
--url http://api.example.com:8080/hellomicroservice/api/take/moretime
编辑:
如果我将 Nginx 超时设置应用为小于 60 秒(例如 30 秒),则请求会在指定的时间间隔内超时。但是如果我将 Nginx 超时设置为超过 60 秒,我们设置为 300 秒,请求在 60 秒后超时。
【问题讨论】:
-
当你
curl通过反向代理时,API网关上的错误日志和nginx错误日志是什么? -
nginx和网关没有错误日志,添加访问日志
-
可以试试加proxy_read_timeout 300s;在服务器块服务器 { proxy_read_timeout 300s;
-
@RadhaMohanMaheshwari 试过了,没用
-
你可以尝试用 api.example.com:8080 替换 nginx 设置中的 localhost:8080 吗?
标签: java nginx spring-cloud nginx-reverse-proxy spring-cloud-gateway