所有服务均用 Golang 编写。我想发送
/register
请求到 auth
服务,但是当我发送 POST
请求 Kong 返回 404 (我可以发送请求到 http:localhost:9094 服务正在工作)。
我是 Kong 的新手。 :(
我的 kong.yaml 文件看起来像这样
_format_version: "2.1"
services:
- name: auth-service
url: http://auth:9094
routes:
- name: register
paths:
- /register
methods:
- GET
- POST
- OPTIONS
- PATCH
# plugins section
plugins:
- name: proxy-cache
config:
response_code:
- 200
request_method:
- GET
- HEAD
content_type:
- text/plain
- application/json
- application/json; charset=utf-8
cache_ttl: 300
strategy: memory
enabled: false
- name: cors
config:
origins:
- '*'
methods:
- GET
- POST
headers:
- Accept
- Accept-Version
- Content-Length
- Content-MD5
- Content-Type
- Date
- X-Auth-Token
- Authorization
exposed_headers:
- X-Auth-Token
credentials: true
max_age: 3600
preflight_continue: false
这是我的 docker-compose.yml 文件
version: '3'
networks:
kong-net:
driver: bridge
services:
kong:
image: kong:latest
restart: always
container_name: kong
networks:
- kong-net
environment:
KONG_DATABASE: off
KONG_PROXY_LISTEN: 0.0.0.0:8000
KONG_PROXY_LISTEN_SSL: 0.0.0.0:8443
KONG_ADMIN_LISTEN: 0.0.0.0:8001
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: /opt/kong/kong.yaml
command: "kong start"
volumes:
- ./config/kong:/opt/kong
healthcheck:
test: ["CMD", "curl", "-f", "http://kong:8001"]
interval: 5s
timeout: 2s
retries: 15
ports:
- "8001:8001"
- "8000:8000"
auth:
container_name: auth
build:
context: .
dockerfile: Dockerfile
target: auth_builder
ports:
- "9094:9094"
networks:
- kong-net
command: ["./app"]
game:
container_name: game
build:
context: .
dockerfile: Dockerfile
target: game_builder
networks:
- kong-net
command: ["./app"]
item:
container_name: item
build:
context: .
dockerfile: Dockerfile
target: item_builder
networks:
- kong-net
command: ["./app"]
marketplace:
container_name: marketplace
build:
context: .
dockerfile: Dockerfile
target: marketplace_builder
networks:
- kong-net
command: ["./app"]
transaction:
container_name: transaciton
restart: always
build:
context: .
dockerfile: Dockerfile
target: transaction_builder
networks:
- kong-net
command: ["./app"]
user:
container_name: user
restart: always
build:
context: .
dockerfile: Dockerfile
target: user_builder
networks:
- kong-net
command: ["./app"]
vault:
container_name: vault
build:
context: .
dockerfile: Dockerfile
target: vault_builder
networks:
- kong-net
command: ["./app"]
Kong服务响应
{"data":[{"enabled":true,"write_timeout":60000,"tls_verify_depth":null,"path":null,"connect_timeout":60000,"id":"65386085-491f-5350-93f9-60f08d9dfd6c","read_timeout":60000,"protocol":"http","created_at":1712500608,"retries":5,"tls_verify":null,"name":"auth-service","host":"auth","updated_at":1712500608,"port":9094,"tags":null,"ca_certificates":null,"client_certificate":null}],"next":null}
香港航线回应
{"data":[{"request_buffering":true,"response_buffering":true,"https_redirect_status_code":426,"strip_path":true,"regex_priority":0,"protocols":["http","https"],"headers":null,"methods":["GET","POST","OPTIONS","PATCH"],"name":"register","id":"ecadfa7c-9bce-5026-930b-488a2c1fce9d","created_at":1712500608,"updated_at":1712500608,"preserve_host":false,"sources":null,"service":{"id":"65386085-491f-5350-93f9-60f08d9dfd6c"},"snis":null,"hosts":null,"destinations":null,"tags":null,"path_handling":"v0","paths":["/register"]}],"next":null}
很明显我很蠢。
paths
中的kong.yaml
部分基本上指的是您可以到达服务的路径。例如,如果我在身份验证服务中有一条名为 /login
的路由,并且如果我在 /auth
中为身份验证服务定义了 kong.yaml
,我可以像 /auth/login
一样访问它 :(((((()