Kong网关API的使用

2019-06-13  本文已影响0人  君致

简介

Kong在v0.13.x版本之前通过接口来管理用户接入的API,但是在v0.13.x版本之后,官方不建议使用API来管理用户接口,而是用Service和Route模块来替代,管理的更精细.

Service操作

接口说明

请求接口: /services
参数说明:

添加service

请求方法:POST
请求实例:

# curl命令请求
curl -i -X POST --url http://KongUrl/services
    --data "name=test_1" 
    --data "protocol=http" 
    --data "host=test_1"
    --data "port=80"
    --data "path=/test_1"
# 返回参数结果
{
    'host': 'test_1', 
    'created_at': 1560396796, 
    'connect_timeout': 60000, 
    'id': 'be48404c-f396-465d-9010-a33a59a0tf99', 
    'protocol': 'http', 
    'name': 'test_1', 
    'read_timeout': 60000, 
    'port': 80, 
    'path': '/test_1', 
    'updated_at': 1560396796, 
    'retries': 5, 
    'write_timeout': 60000, 
    'tags': None
}
获取所有service数据

请求方法: GET
请求实例:

# curl命令请求
curl http://KongUrl/services
# 返回参数结果
{
    'next': None, 
    'data': [
        {
            'host': 'test_2', 
            'created_at': 1560312006, 
            'connect_timeout': 60000, 
            'id': '51146ad9-6a73-4409-9395-43539cfe74f3', 
            'protocol': 'http', 
            'name': 'test_2', 
            'read_timeout': 60000, 
            'port': 80, 
            'path': None, 
            'updated_at': 1560324556, 
            'retries': 5, 
            'write_timeout': 60000, 
            'tags': None
        }, 
        {
            'host': 'test_1', 
            'created_at': 1560396796, 
            'connect_timeout': 60000, 
            'id': 'be48404c-f396-465d-9010-a33a59a0tf99', 
            'protocol': 'http', 
            'name': 'test_1', 
            'read_timeout': 60000, 
            'port': 80, 
            'path': '/test_1', 
            'updated_at': 1560396817, 
            'retries': 5, 
            'write_timeout': 60000, 
            'tags': None
        }
    ]
}
获取单个service数据

请求方法: GET
请求实例:

# curl命令请求(根据service名字或ID)
curl http://KongUrl/services/test_1
# 返回参数结果
{
    'host': 'test_1', 
    'created_at': 1560396796, 
    'connect_timeout': 60000, 
    'id': 'be48404c-f396-465d-9010-a33a59a0tf99', 
    'protocol': 'http', 
    'name': 'test_1', 
    'read_timeout': 60000, 
    'port': 80, 
    'path': '/test_1', 
    'updated_at': 1560396796, 
    'retries': 5, 
    'write_timeout': 60000, 
    'tags': None
}
更新service

请求方法: PATCH
请求实例:

# 例如通过service名字(ID)更新path
curl -i -X PATCH --url http://KongUrl/services/test_1 --data "path=test_1_path"
# 返回参数结果
{   
    'host': 'test_1', 
    'created_at': 1560396796, 
    'connect_timeout': 60000, 
    'id': 'be48404c-f396-465d-9010-a33a59a0tf9', 
    'protocol': 'http', 
    'name': 'test_1', 
    'read_timeout': 60000, 
    'port': 80, 
    'path': '/test_1_patch', 
    'updated_at': 1560405161, 
    'retries': 5, 
    'write_timeout': 60000, 
    'tags': None
}
注: 使用PUT方法,进行创建或更细service,不存在对应的service即创建,存在即更新
删除service数据

请求方法: DELETE
请求实例:

# 根据service名字或ID删除
curl -i -X DELETE --url http://KongUrl/services/test_1

Route操作

接口说明:
添加某个Service的路由

请求方法: POST
请求实例:

# curl命令请求--创建test_1服务的路由
curl -i -X POST --url http://KongUrl/service/test_1/routes/
--data "protocols[]=http&protocols[]=https"
--data "hosts[]=test.cn"
--data "methods[]=GET&methods[]=POST"
# 返回参数结果
{   
    'updated_at': 1560409444, 
    'created_at': 1560409444, 
    'strip_path': True, 
    'snis': None, 
    'hosts': ['test.cn'], 
    'name': 'test_1', 
    'methods': ['GET', 'POST'], 
    'sources': None, 
    'preserve_host': False, 
    'regex_priority': 0, 
    'service': {'id': 'be48404c-f396-465d-9010-a33a59a00tf9'}, 
    'paths': ['/test_1'], 
    'destinations': None, 
    'id': '29310394-6342-48b8-a95e-533bdc5b26d9', 
    'protocols': ['http', 'https'], 
    'tags': None
}
Route其它操作
# 更新route名字为test_1的paths
curl -i -X PATCH --url http://KongUrl/services/test_1 --data "paths[]=test_1_path"
# 根据routes的名字或ID删除routes
curl -i -X DELETE --url http://KongUrl/routes/test_1
# 获取所有routes
curl http://KongUrl/routes
# 获取某个service的routes,通过service名字或ID获取
curl http://KongUrl/{service name or id}/routes
# 检索某个routes,通过route的名字或ID获取
curl http://KongUrl/routes/{name or id}

操作Consumer

操作Plugins

操作Certificate

操作Sni

操作Upstream

# curl方法请求
curl -i -X POST http://KongUrl/upstreams --data="name=test_1"
# 返回参数结果
{
    'created_at': 1560477909,
    'hash_on': 'none', # 加权循环方案,默认为none
    'id': 'a28a67a0-f13a-42e9-b003-6337c79f96ba',
    'tags': None,
    'name': 'test_1',
    'hash_fallback_header': None,
    'hash_on_cookie': None,
    'healthchecks':
        {
            'active':
                {
                    'unhealthy': {
                        'http_statuses': [429, 404, 500, 501, 502, 503, 504, 505],
                        'tcp_failures': 0,
                        'timeouts': 0,
                        'http_failures': 0,
                        'interval': 0
                    },
                    'type': 'http',
                    'http_path': '/',
                    'timeout': 1,
                    'healthy':
                        {
                            'successes': 0,
                            'interval': 0,
                            'http_statuses': [200, 302]
                        },
                    'https_sni': None,
                    'https_verify_certificate': True,
                    'concurrency': 10
                },
            'passive': {
                'unhealthy':
                    {
                        'http_failures': 0,
                        'http_statuses': [429, 500, 503],
                        'tcp_failures': 0,
                        'timeouts': 0
                    },
                'healthy':
                    {
                        'http_statuses': [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308],
                        'successes': 0
                },
                'type': 'http'
        }
    },
    'hash_on_cookie_path': '/',
    'hash_fallback': 'none',
    'hash_on_header': None,
    'slots': 10000
}

curl http://KongUrl/upstreams
curl http://KongUrl/upstreams/{name or id}
curl http://KongUrl/{name or id}/health

操作Target

# curl请求方法
curl -i -X POST http://KongUrl/upstreams/a28a67a0-f13a-42e9-b003-6337c79f96ba/
 --data="target=192.168.99.100:8080"
 -- data="weight=100"
 --data="tags=test"
# 返回参数结果
{
    'created_at': 1560483493.414,
    'upstream': {'id': 'a28a67a0-f13a-42e9-b003-6337c79f96ba'},
    'id': 'e58948fe-0040-4a5c-951c-fdc08398396b',
    'tags': ['test'],
    'target': '192.168.99.100:8080',
    'weight': 100
}

推荐操作流程:

上一篇 下一篇

猜你喜欢

热点阅读