我目前正在为我的项目设置kong。最初,服务器是整体架构),我的团队想将其分为微服务。当我的项目使用gRPC在客户端和服务器之间进行通信时,我正在考虑使用API网关支持gRPC。我在Google和Medium中找到了Kong。并且我已经在无数据库模式下进行了设置。我使用docker-compose运行。以下是我所做的:
这里是docker-compose.yml
version: '3'
networks:
kong-net:
driver: bridge
services:
gateway:
image: kong:2.0.2
networks:
- kong-net
volumes:
- ./../kong:/var/lib/docker/volumes/kong-vol/_data
environment:
KONG_SSL: "off"
KONG_DATABASE: "off"
KONG_DECLARATIVE_CONFIG: /var/lib/docker/volumes/kong-vol/_data/kong.yml
#KONG_SSL_CERT: /
#KONG_SSL_CERT_KEY: /
KONG_PROXY_LISTEN: 0.0.0.0:8000, 0.0.0.0:8443 ssl, 0.0.0.0:9080 http2, 0.0.0.0:9081 http2 ssl
KONG_ADMIN_LISTEN: 127.0.0.1:8001, 127.0.0.1:8444 ssl
ports:
- "8000:8000"
- "8443:8443"
- "8001:8001"
- "8444:8444"
- "9080:9080"
- "9081:9081"
healthcheck:
test: ["CMD", "kong", "health"]
interval: 5s
timeout: 2s
retries: 15
restart: on-failure
appserver:
image: saigonparkingmap/appserver:v1.0
networks:
- kong-net
ports:
- "9090:9090"
- "9999:9999"
depends_on:
- gateway
这里是kong.yml
services:
- name: appserver
url: http://localhost:8001/services
protocol: grpc
host: appserser
port: 9999
connect_timeout: 30000
write_timeout: 30000
read_timeout: 30000
routes:
- name: parking-lot-service
protocols:
- grpc
paths:
- /com.bht.parkingmap.api.proto.parkinglot.ParkingLotService/
并且我遇到了来自Kong的415不支持的媒体类型错误响应。
这是我从客户端控制台收到的内容:
Caused by: io.grpc.StatusRuntimeException: UNKNOWN: HTTP status code 415
invalid content-type: text/html; charset=UTF-8
headers: Metadata(:status=415,content-type=text/html; charset=UTF-8,content-length=144,date=Sun, 29 Mar 2020 23:24:57 GMT,access-control-allow-origin=*,server=kong/2.0.2,x-kong-admin-latency=381,x-kong-upstream-latency=381,x-kong-proxy-latency=2,via=kong/2.0.2)
DATA-----------------------------
<html>
<head><title>415 Unsupported Media Type</title></head>
<body>
<center><h1>415 Unsupported Media Type</h1></center>
</body>
</html>
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:240)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:221)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:140)
at com.bht.parkingmap.api.proto.parkinglot.ParkingLotServiceGrpc$ParkingLotServiceBlockingStub.getParkingLotById
这是我从Kong Docker Container收到的内容:
gateway_1 | 2020/03/29 23:24:56 [warn] 23#0: *490 a client request body is buffered to a temporary file /usr/local/kong/client_body_temp/0000000001, client: 172.20.0.1, server: kong, request: "POST /com.bht.parkingmap.api.proto.parkinglot.ParkingLotService/getParkingLotById HTTP/2.0", host: "localhost:9080"
gateway_1 | 127.0.0.1 - - [29/Mar/2020:23:24:57 +0000] "POST /services/com.bht.parkingmap.api.proto.parkinglot.ParkingLotService/getParkingLotById HTTP/1.1" 415 144 "-" "grpc-java-netty/1.27.2"
gateway_1 | 172.20.0.1 - - [29/Mar/2020:23:24:57 +0000] "POST /com.bht.parkingmap.api.proto.parkinglot.ParkingLotService/getParkingLotById HTTP/2.0" 415 144 "-" "grpc-java-netty/1.27.2"
[我使用从客户端呼叫到Kong网关端口9080的ManagedChannel存根,并期待它将进入侦听端口9999的服务服务器。
客户(存根)---->岗:9080 ---->服务:9999
我是否错过了配置中的某些内容,或者我误解了有关grpc网关的任何信息?期待得到您的支持!非常感谢。
kong.yml
出了点问题。Kong Service中的url
键是protocol://host:port
属性的简写。
我通过在kong.yml
中执行此操作使它正常工作:
_format_version: "1.1"
services:
- name: grpc
protocol: grpc
host: host.docker.internal
port: 9797
routes:
- name: catch-all-grpc-requests
paths:
- /
protocols:
- grpc
这等效于使用url
速记。这将产生与上述相同的效果:
_format_version: "1.1"
services:
- name: grpc
url: grpc://host.docker.internal:9797
routes:
- name: catch-all-grpc-requests
paths:
- /
protocols:
- grpc
这是假设我的gRPC服务器正在端口localhost
的9797
中运行。
然后我grpcurl
想要这样:
grpcurl -v -d '{"name": "Ken"}' -plaintext localhost:9080 facade.GreetingService/SayHello
所以发生了什么事:
客户端(grpcurl)-> Kong HTTP2代理侦听器(:9080)-> gRPC服务器(:9797)