本文将以 Python 应用为例说明如何改造代码来接入 TSF。您不需要修改 Python 服务代码,只需要修改服务间调用的 host。
将原来的 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 文件,spec.yaml 格式如下:
apiVersion: v1kind: Applicationspec:services:- name: user # 服务名ports:- targetPort: 8091 # 服务监听端口protocol: http # 目前支持 HTTP、HTTP2 和 gRPChealthCheck:path: /health # 健康检查 URL
注意
healthCheck 是健康检查的接口,请确认本地调用
curl -i -H 'Host: local-service' {ip}:{Port}/health
能返回200,否则,健康检查失败会导致此服务实例变为离线状态,其它服务将无法调用该服务实例;如果不提供此健康检查接口,sidecar 会通过 TCP 的方式探测 targetPort 是否连通来判断此服务实例是否健康。Host: local-service
是代理加的 header,业务如果对 Host 有检查(如 Nginx 配置的 server_name),则需将 local-service 加到白名单。服务间调用方式
使用服务名调用
在 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']
全链路灰度发布 Header 传递(可选)
要实现全链路灰度发布功能,还需要在请求中额外带上
tsf-system-tags
的 header,传递方式和调用链 Header 传递类似。