
本文档旨在解决 Google App Engine (GAE) 中任务调度跨服务执行的问题。核心在于如何将一个服务创建的任务,指定由另一个服务来执行。通过分析 dispatch.yaml 文件的路由规则,以及利用 HTTP 调用作为中介,提供两种可行的解决方案,帮助开发者实现灵活的任务调度策略,从而优化应用架构和资源利用。
在 Google App Engine (GAE) 中,使用任务队列可以将耗时或资源密集型任务异步执行,从而提高应用的响应速度和用户体验。默认情况下,任务由创建它的服务执行。然而,在某些场景下,我们可能需要将任务提交到一个服务,但由另一个服务来执行。本文将介绍两种实现这一目标的方法。
dispatch.yaml 文件用于定义 GAE 应用的请求路由规则。如果你的 dispatch.yaml 文件仅基于路径进行路由,而没有涉及域名或子域名,那么可以直接在任务的 relative_uri 中指定目标服务的路径,从而将任务路由到目标服务。
示例:
假设你的 dispatch.yaml 文件包含以下路由规则:
dispatch:
- url: "*/mobile/*"
service: mobile-frontend
- url: "*/work/*"
service: static-backend这意味着所有以 /mobile/ 开头的请求都将被路由到 mobile-frontend 服务,而所有以 /work/ 开头的请求都将被路由到 static-backend 服务。
现在,如果你想从 static-backend 服务提交一个任务,并让 mobile-frontend 服务来执行,你可以将任务的 relative_uri 设置为 /mobile/mobile_task。这样,任务队列会将任务路由到 mobile-frontend 服务,并执行 /mobile/mobile_task 对应的处理程序。
注意事项:
如果你的 dispatch.yaml 文件路由规则包含域名或子域名,或者你想更明确地指定任务的执行服务,可以使用 HTTP 调用作为中介。
实现步骤:
示例:
假设你有一个服务部署在 blog.example.com,你想让这个服务来执行从 default 服务提交的任务。
在 blog.example.com 服务中,创建一个名为 /prep_task_invocation/ 的 HTTP 端点。
从 default 服务,向 blog.example.com/prep_task_invocation/ 发起一个 HTTP POST 请求,并将任务数据作为请求体发送。
blog.example.com/prep_task_invocation/ 端点接收到请求后,将任务提交到任务队列,并指定 blog.example.com 服务自身作为任务的执行服务。
代码示例 (Python):
import requests
import json
task_data = {"message": "Hello from default service!"}
url = "http://blog.example.com/prep_task_invocation/"
headers = {'Content-type': 'application/json'}
response = requests.post(url, data=json.dumps(task_data), headers=headers)
if response.status_code == 200:
print("Task submission request sent successfully!")
else:
print(f"Error sending task submission request: {response.status_code}")from flask import Flask, request
from google.cloud import tasks_v2
import json
app = Flask(__name__)
@app.route('/prep_task_invocation/', methods=['POST'])
def prep_task_invocation():
task_data = request.get_json()
print(f"Received task data: {task_data}")
# Create a client.
client = tasks_v2.CloudTasksClient()
# TODO: Fill in the queue path.
queue = client.queue_path("[PROJECT_ID]", "[LOCATION_ID]", "[QUEUE_ID]")
# Construct the request body.
task = {
"app_engine_http_request": { # Specify the type of request.
"http_method": "POST",
"relative_uri": "/process_task", # Endpoint to actually process the task
"body": json.dumps(task_data).encode() # Pass the data
}
}
# Use the client to build and send the task.
response = client.create_task(request={"parent": queue, "task": task})
print(f"Created task {response.name}")
return "Task submitted successfully!", 200
@app.route('/process_task', methods=['POST'])
def process_task():
task_data = request.get_json()
print(f"Processing task with data: {task_data}")
# Your task processing logic here
return "Task processed successfully!", 200
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)注意事项:
本文介绍了两种在 Google App Engine 中实现任务调度跨服务执行的方法:利用 dispatch.yaml 文件进行路由和使用 HTTP 调用作为中介。选择哪种方法取决于你的应用架构和具体需求。使用 dispatch.yaml 文件进行路由更加简洁,但仅适用于简单的路由规则。使用 HTTP 调用作为中介更加灵活,但需要额外的代码来实现 HTTP 请求和任务提交。根据实际情况选择最适合你的解决方案,可以有效地优化你的 GAE 应用。
以上就是GAE 任务调度:跨服务执行任务的实现方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号