微服务平台 TSF Mesh 应用开发

这篇将以 Python 应用为例说明如何改造代码来接入 TSF。你不需要修改 Python 服务代码,只需要修改服务间调用的 host。

相关产品
微服务平台 TSF 产品页,含版本规格、价格与购买入口。

·将原来的 IP:Port 替换为服务名,如果不使用服务名调用,流量不会经过 sidecar 直接传递到被调服务,被调服务无法识别主调服务的服务名。

·端口使用80或者业务真实的监听端口。

·其他代码不做修改。

注意:说明:

注意:以下代码片段可参见 Demo 工程内 userService.py。

改造前:

sidecarPort = 80if common.sendAndVerify("127.0.0.1", sidecarPort, "/api/v6/shop/items", headers): self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() msg = {"result":{"userId":"1234", "userName":"vincent"}} self.wfile.write(json.dumps(msg)) else: self.send_response(500) self.send_header('Content-type', 'application/json') self.end_headers() msg = {"exception":"Error invoke %s" % "/api/v6/shop/items"} self.wfile.write(json.dumps(msg))

改造后:

sidecarPort = 80if common.sendAndVerify("shop", sidecarPort, "/api/v6/shop/items", headers): self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() msg = {"result":{"userId":"1234", "userName":"vincent"}} self.wfile.write(json.dumps(msg)) else: self.send_response(500) self.send_header('Content-type', 'application/json') self.end_headers() msg = {"exception":"Error invoke %s" % "/api/v6/shop/items"} self.wfile.write(json.dumps(msg))

可以看到,代码行中除了访问方式发生变化(127.0.0.1变为shop)外,其他都不需要改动。

一、服务定义和注册(必选)

如果是虚拟机部署,需要在应用程序所在目录中设置创建spec.yaml文件;如果是容器部署,需要在应用启动时,在/opt/tsf/app_config下写入spec.yaml文件,该文件用于描述服务信息。Sidecar 会通过服务描述文件将服务注册到服务注册中心。

注意:说明:

注意:当前支持在控制台上选择使用本地 spec.yaml和控制台配置两种方式描述服务信息,推荐在控制台上直接设置。具体操作参见 Mesh 应用部署(容器篇) 或者 Mesh 应用部署(虚拟机篇)。

若选择使用本地 spec.yaml 文件,spec.yaml 格式如下:

apiVersion: v1kind: Applicationspec: services: – name: user # 服务名 ports: – targetPort: 8091 # 服务监听端口 protocol: http # 目前支持 HTTP、HTTP2 和 gRPC healthCheck: path: /health # 健康检查 URL

二、使用服务名调用

在 user 服务所在实例上执行 curl 命令,通过使用shop服务名进行访问。

curl shop:<shop端口>/api/v6/shop/order

三、API 定义和上报(可选)

TSF 支持 Mesh 应用 API 上报功能,用于 API 级别的服务治理,如路由、鉴权和限流等,不需要可以跳过。

·如果是虚拟机部署,需要在应用程序所在目录中创建apis目录。

·如果是容器部署,需要在/opt/tsf/app_config下创建apis目录,该目录放置服务的 API 定义。

一个服务对应一个 yaml 文件,文件名即服务名,如 petstore 服务对应 petstore.yaml 配置。API 遵循 OPENAPI 3.0 规范,user.yml 的 API 定义如下:

openapi: 3.0.0info: version: "1.0.0" title: user servicepaths: /api/v6/user/create: get: responses: '200': description: OK '401': description: Unauthorized '402': description: Payment Required '403': description: Forbidden /api/v6/user/account/query: get: responses: '200': description: OK '401': description: Unauthorized '402': description: Payment Required '403': description: Forbidden /health: get: responses: '200': description: OK '401': description: Unauthorized '402': description: Payment Required '403': description: Forbidden

四、设置自定义标签 (可选)

Mesh 支持通过 HTTP Header 设置自定义标签,以 Python 应用为例说明如何设置自定义标签。

·对于调用链和全链路灰度发布中的自定义标签。

import requestsurl = 'https://api.github.com/some/endpoint'headers = {'tsf-mesh-tag': 'custom-key=custom-value'} r = requests.get(url, headers=headers)

·对于服务鉴权、服务路由和服务限流中的自定义标签。

import requestsurl = 'https://api.github.com/some/endpoint'headers = {'custom-key': 'custom-value'}r = requests.get(url, headers=headers)

注意:说明:

注意:以上示例已经在依赖机器上安装了 requests 库。

五、调用链 Header 传递(可选)

要实现 Mesh 应用调用链和服务依赖拓扑功能,需要在请求中带上9个相关 header。

// 9个调用链相关的头,具体说明见(https://www.envoyproxy.io/docs/envoy/v1.8.0/configuration/http_conn_man/headers.html?highlight=tracing)traceHeaders = ['x-request-id', 'x-trace-service', 'x-ot-span-context', 'x-client-trace-id', 'x-b3-traceid', 'x-b3-spanid', 'x-b3-parentspanid', 'x-b3-sampled', 'x-b3-flags']

具体使用方法参见 Mesh Demo 介绍。

六、全链路灰度发布 Header 传递(可选)

要实现全链路灰度发布功能,还需要在请求中额外带上tsf-system-tags的 header,传递方式和调用链 Header 传递类似。

赞 (0)
上一篇 4天前
下一篇 4天前