对服务 URL 但对版本 url 的请求失败
Requests failing when made to service URL, but not to version url
我正在尝试在 Google App Engine 上设置服务,但我无法让 XmlHttp 与其一致地工作。
部署后,可以从 2 个不同的 url 访问网站:service-dot-project.appspot
和 version-dot-service-dot-project.appspot
,由于某种原因,两者之间存在不一致。
这是一些演示代码,可验证给我带来了麻烦。
# routes.py
from flask import render_template
from . import app
@app.route("/test", methods=["GET"])
def test():
return render_template("test.html")
@app.route("/api/test", methods=["GET"])
def api_test():
return "It Works!"
# templates/test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Debug</title>
</head>
<body>
<div id="out"></div>
<button type="button" onclick="run()">
Test the thing.
</button>
<script>
function run() {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = () => {
if (xmlHttp.readyState === 4 && xmlHttp.status === 200)
document.getElementById("out").innerText = xmlHttp.responseText;
}
xmlHttp.open("GET", "/api/test", true);
xmlHttp.send(null);
}
</script>
</body>
</html>
# service.yaml
runtime: python38
service: name
automatic_scaling:
min_idle_instances: 1
instance_class: F4
entrypoint: gunicorn -b :$PORT main:app
env_variables:
...
当我尝试按下版本 url 上的按钮时,它按预期工作,并且“它有效!”打印到按钮上方的 div,但在服务 url(未指定版本)上,页面本身会加载,但按下按钮会导致请求在打印前挂起几秒钟这到控制台:
GET https://service-dot-project.appspot.com/api/test [HTTP/2 404 Not Found 7912ms]
使用本地flask调试环境测试时,没有出现该问题
有什么 Google App Engine 做的我应该知道的事情可能导致了这个问题的发生吗? /api
是保留端点吗?我的其余端点在服务 url 上工作,只有 api 端点中断。我唯一的 app.before_request
方法失败并返回 403,而不是 404,所以这不是原因。
如果你去https://console.cloud.google.com/appengine/versions
和select您的服务有问题,是否有其他版本正在接收流量而不是您想要的版本?
此外,尝试转到日志,找到 404 条目,展开它并查看哪个版本引发该错误,在 protoPayload
> versionId
下
问题似乎是由我们项目中的一项其他服务 运行 引起的。
我们的默认服务在其 dispatch.yaml
中定义
dispatch:
- url: "/api*"
module: otherservice
拦截对 myservice-dot-project
的所有请求并将它们重定向到 otherservice-dot-project
为什么版本 url 不是这种情况可能是因为没有具有相同版本号的默认服务版本。
解决方法是更改默认服务的分派 url,或更改新服务的 API 端点的 url。
我正在尝试在 Google App Engine 上设置服务,但我无法让 XmlHttp 与其一致地工作。
部署后,可以从 2 个不同的 url 访问网站:service-dot-project.appspot
和 version-dot-service-dot-project.appspot
,由于某种原因,两者之间存在不一致。
这是一些演示代码,可验证给我带来了麻烦。
# routes.py
from flask import render_template
from . import app
@app.route("/test", methods=["GET"])
def test():
return render_template("test.html")
@app.route("/api/test", methods=["GET"])
def api_test():
return "It Works!"
# templates/test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Debug</title>
</head>
<body>
<div id="out"></div>
<button type="button" onclick="run()">
Test the thing.
</button>
<script>
function run() {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = () => {
if (xmlHttp.readyState === 4 && xmlHttp.status === 200)
document.getElementById("out").innerText = xmlHttp.responseText;
}
xmlHttp.open("GET", "/api/test", true);
xmlHttp.send(null);
}
</script>
</body>
</html>
# service.yaml
runtime: python38
service: name
automatic_scaling:
min_idle_instances: 1
instance_class: F4
entrypoint: gunicorn -b :$PORT main:app
env_variables:
...
当我尝试按下版本 url 上的按钮时,它按预期工作,并且“它有效!”打印到按钮上方的 div,但在服务 url(未指定版本)上,页面本身会加载,但按下按钮会导致请求在打印前挂起几秒钟这到控制台:
GET https://service-dot-project.appspot.com/api/test [HTTP/2 404 Not Found 7912ms]
使用本地flask调试环境测试时,没有出现该问题
有什么 Google App Engine 做的我应该知道的事情可能导致了这个问题的发生吗? /api
是保留端点吗?我的其余端点在服务 url 上工作,只有 api 端点中断。我唯一的 app.before_request
方法失败并返回 403,而不是 404,所以这不是原因。
如果你去https://console.cloud.google.com/appengine/versions
和select您的服务有问题,是否有其他版本正在接收流量而不是您想要的版本?
此外,尝试转到日志,找到 404 条目,展开它并查看哪个版本引发该错误,在 protoPayload
> versionId
问题似乎是由我们项目中的一项其他服务 运行 引起的。
我们的默认服务在其 dispatch.yaml
中定义dispatch:
- url: "/api*"
module: otherservice
拦截对 myservice-dot-project
的所有请求并将它们重定向到 otherservice-dot-project
为什么版本 url 不是这种情况可能是因为没有具有相同版本号的默认服务版本。
解决方法是更改默认服务的分派 url,或更改新服务的 API 端点的 url。