URL моего rp c равен /rpc/gateway/app
, я хочу предоставить клиенту маршрут /v4/loan/appGateway
, поэтому я настраиваю службу:
# upstream
curl -X POST http://localhost:8001/upstreams --data "name=appGatewayUpstream"
# target
curl -X POST http://localhost:8001/upstreams/appGatewayUpstream/targets --data "target=localhost:8080" --data "weight=100"
# service
curl -X POST http://localhost:8001/services --data "name=loan-backend" --data "host=appGatewayUpstream" --data "path=/rpc/gateway/app"
# route
curl -X POST http://localhost:8001/services/loan-backend/routes --data "paths[]=/v4/loan/appGateway" --data 'host[]=loan-backend.com'
Теперь клиент может запросить правильный сервер с маршрутом http://localhost:8000/v4/loan/appGateway --header 'Host: loan-backend.com'
Но теперь я хочу изменить маршрутизатор /v4/loan/appGateway/getStatus
на /v4/loan/appGateway -d '{ "method": "getStatus" }'
, поэтому я пишу плагин, функция ключа:
function Http2JsonRPCHandler:access(config)
Http2JsonRPCHandler.super.access(self)
local requestMethod = kong.request.get_method()
if 'POST' == requestMethod and config.to_jsonrpc then
-- get jsonrpc' method, it's the last part of the path
local path = kong.request.get_path()
local pathFields = StringUtils.split(path, '/')
local method = pathFields[#pathFields]
table.remove(pathFields)
local newPath = '/'..table.concat(pathFields, '/')
-- set the new path
kong.service.request.set_path(newPath)
-- get request's body
local body, err = kong.request.get_body('application/json')
if err then
kong.log.err("Convert http to jsonrpc error, can not get request body, ", err)
return kong.response.exit(500, { message = "Request's body must be JSON." })
end
-- set new body
local rpcBody = { ["method"]=method, ["id"]=0, ["jsonrpc"]="2.0", ["params"]=body }
local ok, err = kong.service.request.set_body(rpcBody)
if err then
kong.log.err("Set new rpc body err, ", rpcBody)
return kong.response.exit(500, { message = "Set request's body err, "..err })
end
end
end
Я включил его правильно. Но когда я закручиваю http://localhost:8080/v4/loan/appGateway/getStatus -d 'xxxxx'
, конг направляет этот запрос на http://localhost:8080/v4/loan/appGateway -d '{xxx}'
. Путь службы не работает.