python - dispatch.yaml 在实践中是如何工作的
问题描述
我理解的作用dispatch.yaml
是将请求重定向到合适的微服务。
下面是我的 dispatch.yaml:
dispatch:
- url: "*/favicon.ico"
service: default
- url: "*/tweet/*"
service: tweet
该项目有两个服务:default
和tweet
。使用Flask
,处理程序配置如下:
默认服务
我的项目/default_service/default.yaml
service: default
runtime: python37
handlers:
- url: /static
static_dir: static
- url: /.*
script: auto
我的项目/default_service/main.py
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def root():
response = { 'message': "Hello from the Default service" }
return jsonify(response)
推文服务
我的项目/tweet_service/tweet.yaml
service: service-job
runtime: python37
handlers:
- url: /.*
script: auto
我的项目/tweet_service/main.py
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/tweet') # handles /tweet route
def root():
response = { 'message': "Hello from the Tweet service" }
return jsonify(response)
问题:
发出请求时https://tweet-dot-my-project.appspot.com/tweet
,响应是502 Bad Gateway
错误的。为什么是这样?dispatch.yaml
应该采取以下步骤:
- 注意到
/tweet
路径并调用tweet
微服务来处理它。 - 在内部
tweet.py
,the @app.route('/tweet')
应该已经调用了句柄并返回响应。
请让我知道我在哪里误解了。
解决方案
首先,您的示例中有一些问题。
在你my-project/tweet_service/tweet.yaml
的服务不是tweet
。问题可能出在这里。
然后,调度允许不直接查询子服务tweet-dot-my-project.appspot.com/tweet
,而是直接查询基础服务my-project.appspot.com/tweet
,并将查询重定向到正确的tweet
服务
最后,AppEngine 出现异常行为。
如果您只有 1 个端点,例如 /tweet
在这里,在您的 中dispatch.yaml
,您重定向下面的所有 url*/tweet/
以由tweet
服务处理。但是/tweet
端点没有被重定向。更新您的调度而不是最后一个/
推文
- url: "*/tweet*"
service: tweet
如果你还有其他子端点,比如 /tweet/other
这一次,你的dispatch.yaml
是有效的,有无最后一个/
这并不能解释502 Bad Gateway
错误。所以,我在测试中得到了它。看我的代码:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/tweet') # handles /tweet route
def root():
response = { 'message': "Hello from the Tweet service" }
return jsonify(response)
@app.route('/tweet/other') # handles /tweet route
def root():
response = { 'message': "Hello from the Tweet/other service" }
return jsonify(response)
我复制粘贴你的代码太快了,我有 2 个root()
功能。这会导致服务崩溃,从而导致 502 错误。
检查您的真实代码(提供的代码不完全正确,我想不止这两个端点)和日志,找到您的崩溃原因并修复它。
推荐阅读
- graphql - 将 ImageSharp 作为字段添加到 MarkdownRemark 节点(不是 frontmatter)
- r - For循环填充R中的一列
- python-3.x - 这在 Python 中是如何发生的?
- python - 函数的参数可以用作全局变量的占位符吗
- http - 根据查询的路由使用不同的请求解析器
- javascript - 动态添加到 Promise 数组时,Promise 全部提前完成
- java - 根据杰克逊中的xml文档属性在反序列化期间检测对象类型 - 没有JsonNode
- node.js - 为什么我在 Mongoose 中的 createIndex 失败了?
- c - 为什么“__tracepoint_sched_switch”未定义
- jwt - 如何使用 Google IAP 对 JupyterHub 进行身份验证?