在Kong中添加API

时间:2016-11-25 14:51:25

标签: api spring-boot kong

REST和API网关的新功能。

我在开发机器上安装了Kong和Cassandra,我试图添加我的API(spring-boot应用程序),但是阅读我努力使其工作的文档。

我的API:

http://ff-nginxdev-01:9003/fund-information-services/first-information/fund/{fundId}

当我跑

http http://ff-nginxdev-01:9003/fund-information-services/first-information/fund/630

    HTTP/1.1 200
    Content-Type: application/json;charset=UTF-8
    Date: Fri, 25 Nov 2016 14:47:30 GMT
    Transfer-Encoding: chunked
    X-Application-Context: application:9003

    {
        "assetSplit": {
            "allocationHistories": [
                {
                    "key": {
                        "asset": {
                            "description": "Other Far East",
                            "id": 18
                        },
                        "assetSplit": "09",
                        "effectiveDate": 1430348400000
                    },
    ......
    ......

一切都很好,我能够检索Json消息。

在Kong中添加API:

http POST http://ff-nginxdev-01:8001/apis/ name=fund-information upstream_url=http://ff-nginxdev-01:9003/ request_path=/fund-information-services


    HTTP/1.1 201 Created
    Access-Control-Allow-Origin: *
    Connection: keep-alive
    Content-Type: application/json; charset=utf-8
    Date: Fri, 25 Nov 2016 14:39:45 GMT
    Server: kong/0.9.4
    Transfer-Encoding: chunked

    {
        "created_at": 1480084785000,
        "id": "fdcc76d7-e2a2-4816-8f27-d506fdd32c0a",
        "name": "fund-information",
        "preserve_host": false,
        "request_path": "/fund-information-services",
        "strip_request_path": false,
        "upstream_url": "http://ff-nginxdev-01:9003/"
    }

测试孔API网关:

http http://ff-nginxdev-01:8000/fund-information-services/first-information/fund/630

HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Content-Type: text/plain; charset=UTF-8
Date: Fri, 25 Nov 2016 14:44:33 GMT
Server: kong/0.9.4
Transfer-Encoding: chunked

An invalid response was received from the upstream server

我知道我错过了一些东西,但我不清楚。

2 个答案:

答案 0 :(得分:0)

默认情况下,动态SSL插件会将特定的SSL证书绑定到API中的request_host。 您只需在API中定义request_path,这意味着当您使用request_path时,SSL插件不适用。

您可以阅读更多内容,了解其中的原因:this issue

为了使您的请求有效,我认为您应该阅读如何代理API:Proxy Reference

以下是我的问题解决方案:

方法1:将“strip_request_path”更改为true

"strip_request_path": true

此方法假设您没有先指定request_host

方法2:使用request_host

"request_host" : "your_api"

然后在您的请求标头中,您应该添加此request_host:

示例请求:

curl -i -X POST --url http://ff-nginxdev-01:8000/fund-information-services/first-information/fund/630 --header 'Host: your_api' 

它会起作用

答案 1 :(得分:0)

你错过了kong"基金信息"已知的api名称,所以如果你这样做

http POST http://ff-nginxdev-01:8001/apis/ name = fund-information upstream_url = http://ff-nginxdev-01:9003 request_path = / fund-information-services

你的测试网址是 http http://ff-nginxdev-01:8000/fund-information/first-information/fund/630