如何解决Kong First Reqtest延迟问题,如何限制对某些路线的访问,如何进行仪器

发布于 2025-01-26 05:05:59 字数 6707 浏览 0 评论 0原文

我有一个基于python/django的几个服务的微服务结构

。结构如下:

用户< ===> kong < ===> alpha< ===> kong < ===> beta< ===> kong < ===>伽马

这是我的kong.yml声明配置:

_format_version: "2.1"
_transform: true

services:
  - name: alpha-beta-gamma-live
    host: alpha
    port: 8000
    protocol: http
    path: /beta/gamma/live
    routes:
      - name: alpha-beta-gamma-live
        methods:
          - GET
        paths:
          - /alpha/beta/gamma/live
        strip_path: true

  - name: beta-gamma-live
    host: beta
    port: 8000
    protocol: http
    path: /gamma/live
    routes:
      - name: beta-gamma-live
        methods:
          - GET
        paths:
          - /beta/gamma/live
        strip_path: true

  - name: gamma-live
    host: gamma
    port: 8000
    protocol: http
    path: /live
    routes:
      - name: gamma-live
        methods:
          - GET
        paths:
          - /gamma/live
        strip_path: true
plugins:
  - name: correlation-id
    config:
      header_name: X-Kong-Correlation-ID
      generator: uuid
      echo_downstream: true
  - name: zipkin
    config:
      local_service_name: kong
      http_endpoint: http://zipkin:9411/api/v2/spans
      sample_ratio: 1
      include_credential: true
      traceid_byte_count: 16
      header_type: preserve
      default_header_type: b3
      tags_header: Zipkin-Tags

这是

version: "3.8"

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  kong:
    container_name: kong
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.40
    healthcheck:
      test: [ “CMD”, “curl”, “-f”, “http://kong:8000” ]
      interval: 5s
      timeout: 2s
      retries: 15
    environment:
      - KONG_DATABASE=off
      - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
      - KONG_PROXY_ACCESS_LOG=/dev/stdout
      - KONG_ADMIN_ACCESS_LOG=/dev/stdout
      - KONG_PROXY_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_ERROR_LOG=/dev/stderr
      - KONG_DECLARATIVE_CONFIG=/kong/declarative/kong.yml
    ports:
      - "8444:8444"
      - "80:8000"
      - "443:8443"

从alpha服务中运行kong的docker-compose文件,我使用python的request> request库来调用beta的终点使用Python的请求库来调用/live gamma的端点如下

alpha

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  alpha:
    container_name: alpha
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.11
    ports:
      - "8011:8000"
    environment:
      SECRET_KEY: secret-key
      DEBUG: 'true'
    command: python manage.py runserver 0.0.0.0:8000
@api_view(["GET"])
def beta_gamma_live(request):

    res_kong = requests.get("http://kong:8000/beta/gamma/live")

    return Response({
        "chained-alpha-beta-gamma-status-check-through-kong": res_kong.status_code,
    }, status.HTTP_200_OK)

beta

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  beta:
    container_name: beta
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.12
    ports:
      - "8012:8000"
    environment:
      SECRET_KEY: secret-key
      DEBUG: 'true'
    command: python manage.py runserver 0.0.0.0:8000
@api_view(["GET"])
def gamma_live(request):

    res_kong = requests.get("http://kong:8000/gamma/live")

    return Response({
        "chained-beta-gamma-status-check-through-kong": res_kong.status_code,
    }, status.HTTP_200_OK)

gamma

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  gamma:
    container_name: gamma
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.13
    ports:
      - "8013:8000"
    environment:
      SECRET_KEY: secret-key
      DEBUG: 'true'
    command: python manage.py runserver 0.0.0.0:8000
@api_view(["GET"])
def live(request):
    return Response({"status": "Success"}, status.HTTP_200_OK)

第一个问题

我发现了这个问题

/eskity/3058

是否有一种方法可以解决/解决此问题?


第二个问题

我使用docker容器运行Zipkin UI,如下面

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  zipkin:
    container_name: zipkin
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.41
    ports:
      - "9411:9411"

我打开Zipkin UI时on http://127.0.0.1:9411 ,跟踪显示我的三个请求如下:

  • ##### #####################
  • ​#〜20 s

“在这里输入图像描述”

像这个

  • ########
  • 虽然应该 ## 〜20 s
  • -.-.-.-.-.--------.-.-.-.-.-.-.-.-.- #########至20 s

,为了实现这一目标,我需要仪器使用下面列表中的库中的每项服务:

https://zipkin.io/pages/pages/tracers_instrumentation

问题2第1部分:我应该为干净易用的python做什么?

问题2第2部分:有没有办法不必这样做?我正在寻找语言敏捷的延迟跟踪,可以通过kong来完成,以便我不必在服务alpha,beta,gamma中添加任何东西

I have a microservices structure with a few services based on python/Django

The structure is as below:

user <===> kong <===> alpha <===> kong <===> beta <===> kong <===> gamma

this is my kong.yml declarative configuration:

_format_version: "2.1"
_transform: true

services:
  - name: alpha-beta-gamma-live
    host: alpha
    port: 8000
    protocol: http
    path: /beta/gamma/live
    routes:
      - name: alpha-beta-gamma-live
        methods:
          - GET
        paths:
          - /alpha/beta/gamma/live
        strip_path: true

  - name: beta-gamma-live
    host: beta
    port: 8000
    protocol: http
    path: /gamma/live
    routes:
      - name: beta-gamma-live
        methods:
          - GET
        paths:
          - /beta/gamma/live
        strip_path: true

  - name: gamma-live
    host: gamma
    port: 8000
    protocol: http
    path: /live
    routes:
      - name: gamma-live
        methods:
          - GET
        paths:
          - /gamma/live
        strip_path: true
plugins:
  - name: correlation-id
    config:
      header_name: X-Kong-Correlation-ID
      generator: uuid
      echo_downstream: true
  - name: zipkin
    config:
      local_service_name: kong
      http_endpoint: http://zipkin:9411/api/v2/spans
      sample_ratio: 1
      include_credential: true
      traceid_byte_count: 16
      header_type: preserve
      default_header_type: b3
      tags_header: Zipkin-Tags

and this is the docker-compose file to run kong

version: "3.8"

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  kong:
    container_name: kong
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.40
    healthcheck:
      test: [ “CMD”, “curl”, “-f”, “http://kong:8000” ]
      interval: 5s
      timeout: 2s
      retries: 15
    environment:
      - KONG_DATABASE=off
      - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
      - KONG_PROXY_ACCESS_LOG=/dev/stdout
      - KONG_ADMIN_ACCESS_LOG=/dev/stdout
      - KONG_PROXY_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_ERROR_LOG=/dev/stderr
      - KONG_DECLARATIVE_CONFIG=/kong/declarative/kong.yml
    ports:
      - "8444:8444"
      - "80:8000"
      - "443:8443"

from within alpha service I use python's requests library to call an endpoint of beta that use python's requests library to call /live endpoint of gamma as below

alpha

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  alpha:
    container_name: alpha
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.11
    ports:
      - "8011:8000"
    environment:
      SECRET_KEY: secret-key
      DEBUG: 'true'
    command: python manage.py runserver 0.0.0.0:8000
@api_view(["GET"])
def beta_gamma_live(request):

    res_kong = requests.get("http://kong:8000/beta/gamma/live")

    return Response({
        "chained-alpha-beta-gamma-status-check-through-kong": res_kong.status_code,
    }, status.HTTP_200_OK)

beta

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  beta:
    container_name: beta
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.12
    ports:
      - "8012:8000"
    environment:
      SECRET_KEY: secret-key
      DEBUG: 'true'
    command: python manage.py runserver 0.0.0.0:8000
@api_view(["GET"])
def gamma_live(request):

    res_kong = requests.get("http://kong:8000/gamma/live")

    return Response({
        "chained-beta-gamma-status-check-through-kong": res_kong.status_code,
    }, status.HTTP_200_OK)

gamma

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  gamma:
    container_name: gamma
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.13
    ports:
      - "8013:8000"
    environment:
      SECRET_KEY: secret-key
      DEBUG: 'true'
    command: python manage.py runserver 0.0.0.0:8000
@api_view(["GET"])
def live(request):
    return Response({"status": "Success"}, status.HTTP_200_OK)

The First Question

I have found this issue about it

https://github.com/Kong/kong/issues/3058

Is there a way to fix/address this problem?


Second question

I run Zipkin UI using a docker container as below

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  zipkin:
    container_name: zipkin
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.41
    ports:
      - "9411:9411"

When I open Zipkin UI on http://127.0.0.1:9411, The trace shows me three requests as below:

  • ########################## ~ 60 s
  • ################ ~ 40 s
  • ####### ~ 20 s

enter image description here

While it should be like this

  • ######### ~ 20 s
  • -.-.-.-.-.-.- ######### ~ 20 s
  • -.-.-.-.--.-.-.-.-.-.-.- ######### ~ 20 s

In order to achieve this, I need to instrument each of the services using a library in the list below:

https://zipkin.io/pages/tracers_instrumentation

Question 2 part 1: What Should I do for python that is clean and easy to use?

Question 2 part 2: is there a way not to have to do it? I am looking for latency tracing that is language-agnostic and can be done through kong so that I don't have to add anything to the services alpha, beta, gamma

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文