首页 > 解决方案 > dispatch.yaml 在实践中是如何工作的

问题描述

我理解的作用dispatch.yaml是将请求重定向到合适的微服务。

下面是我的 dispatch.yaml:

dispatch:
  - url: "*/favicon.ico"
    service: default

  - url: "*/tweet/*"
    service: tweet

该项目有两个服务:defaulttweet。使用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应该采取以下步骤:

  1. 注意到/tweet路径并调用tweet微服务来处理它。
  2. 在内部tweet.pythe @app.route('/tweet')应该已经调用了句柄并返回响应。

请让我知道我在哪里误解了。

标签: pythongoogle-app-enginegoogle-cloud-platformmicroservices

解决方案


首先,您的示例中有一些问题。

在你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 错误。

检查您的真实代码(提供的代码不完全正确,我想不止这两个端点)和日志,找到您的崩溃原因并修复它。


推荐阅读