puoygae
puoygae

Reputation: 573

How dispatch.yaml works in practice

I understand that the role of dispatch.yaml is to redirect requests to the suitable microservice.

Below is my dispatch.yaml:

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

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

The project has two services: default and tweet. Using Flask, the handlers are configured like this:

Default Service

my-project/default_service/default.yaml

service: default
runtime: python37

handlers:

- url: /static
  static_dir: static

- url: /.*
  script: auto

my-project/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

my-project/tweet_service/tweet.yaml

service: service-job
runtime: python37

handlers:
- url: /.*
  script: auto

my-project/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)

Question:

When the request https://tweet-dot-my-project.appspot.com/tweet is made, the responses is a 502 Bad Gateway error. Why is this? The dispatch.yaml should have taken the following steps:

  1. Noticed the /tweet path and calls upon the tweet microservice to handle it.
  2. Inside tweet.py, the @app.route('/tweet') handle should have been called and return the response.

Please let me know where I have misunderstood.

Upvotes: 0

Views: 256

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 76093

First, you have some issues in your examples.

In your my-project/tweet_service/tweet.yaml the service isn't tweet. The problem maybe comme to here.

Then, the dispatch allow to not directly query the sub-service tweet-dot-my-project.appspot.com/tweet but directly the base servicemy-project.appspot.com/tweet and the query is redirected to the right tweet service

Finally, there is an abnormal behavior on AppEngine.

If you have only 1 endpoint, like /tweet

Here, in your dispatch.yaml, you redirect all the url under */tweet/ to be handle by the tweet service. But the /tweet endpoint isn't redirected. Update your dispatch without the last / on tweet

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

If you have other sub-endpoints, like /tweet/other This time, your dispatch.yaml is valid, with and without the last /

This doesn't explain the 502 Bad Gateway error. So, I got it in my tests. Look at my code:

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)

I copy paste too quickly your code, and I have 2 root() functions. That causes service crash and thus a 502 errors.

Check your REAL code (the provided code isn't fully correct, I imagine there is more than this 2 endpoints) and the logs, find your crash cause, and fix it.

Upvotes: 1

Related Questions