【微服务】Kong 添加多后端Api

Kong接口多后端服务添加

首先添加Api接口

调用接口

POST http://192.168.0.181:8001/apis/
#参数
methods:GET
uris:/service
name:spring-boot-consul-service
#如果后端服务是单节点,那么可以填写ip+端口访问
upstream_url:http://spring-boot-consul-service.service.dc1.consul:17003
{
    "created_at": 1514995615974,
    "strip_uri": true,
    "id": "b8431831-6a77-434e-ac4a-7e379550714f",
    "name": "spring-boot-consul-service",
    "methods": [
        "GET"
    ],
    "http_if_terminated": false,
    "preserve_host": false,
    "upstream_url": "http://spring-boot-consul-service.service.dc1.consul:17003",
    "uris": [
        "/service"
    ],
    "upstream_send_timeout": 60000,
    "upstream_connect_timeout": 60000,
    "upstream_read_timeout": 60000,
    "retries": 5,
    "https_only": false
}

通过以上接口创建Api

然后创建upstreams

由于后端服务分别有:

  • 192.168.0.184:17003
  • 192.168.0.185:17003
  • 192.168.0.186:17003 因此首先要创建upstreams,调用如下接口:
POST http://192.168.0.181:8001/upstreams/
#参数
name:spring-boot-consul-service.service.dc1.consul
{
    "created_at": 1515009399437,
    "orderlist": [
        78,
        4,
        81,
        16,
        8,
        65,
        61,
        63,
        1,
        68,
        18,
        88,
        93,
        66,
        80,
        3,
        26,
        79,
        10,
        53,
        15,
        62,
        43,
        54,
        55,
        36,
        73,
        24,
        75,
        67,
        37,
        13,
        69,
        95,
        50,
        19,
        52,
        49,
        77,
        35,
        98,
        94,
        39,
        17,
        5,
        47,
        86,
        48,
        76,
        85,
        59,
        22,
        51,
        60,
        41,
        58,
        7,
        91,
        30,
        90,
        14,
        70,
        92,
        25,
        72,
        71,
        6,
        32,
        57,
        97,
        100,
        20,
        83,
        28,
        56,
        38,
        40,
        82,
        31,
        46,
        42,
        23,
        96,
        74,
        33,
        2,
        84,
        29,
        89,
        44,
        9,
        11,
        27,
        45,
        87,
        99,
        34,
        21,
        64,
        12
    ],
    "id": "5d58cf34-67f6-45ac-b837-be819ee42beb",
    "name": "spring-boot-consul-service.service.dc1.consul",
    "slots": 100
}
再创建targets

通过targets指定实际的后端服务地址 调用如下接口:

POST http://192.168.0.181:8001/upstreams/spring-boot-consul-service.service.dc1.consul/targets
参数:
target:192.168.0.184:17003
weight:15
{
	"created_at": 1515009876740,
	"id": "282ed14d-58de-4add-bd8e-5b67ed2e4a5b",
	"upstream_id": "5d58cf34-67f6-45ac-b837-be819ee42beb",
	"target": "192.168.0.184:17003",
	"weight": 15
}

加入3个target,通过如下接口查询列表:

GET http://192.168.0.181:8001/upstreams/spring-boot-consul-service.service.dc1.consul/targets
{
    "total": 3,
    "data": [
        {
            "created_at": 1515009876740,
            "id": "282ed14d-58de-4add-bd8e-5b67ed2e4a5b",
            "upstream_id": "5d58cf34-67f6-45ac-b837-be819ee42beb",
            "target": "192.168.0.184:17003",
            "weight": 15
        },
        {
            "created_at": 1515009885520,
            "id": "f787480d-6469-4914-b870-ee8eb25ebe36",
            "upstream_id": "5d58cf34-67f6-45ac-b837-be819ee42beb",
            "target": "192.168.0.185:17003",
            "weight": 15
        },
        {
            "created_at": 1515009889985,
            "id": "8f4ca0b7-8131-4ace-a191-8f9706a5189b",
            "upstream_id": "5d58cf34-67f6-45ac-b837-be819ee42beb",
            "target": "192.168.0.186:17003",
            "weight": 15
        }
    ]
}
最后调用接口

调用接口:

GET http://192.168.0.181:8000/service/service/add?a=1&b=2
#header
Host:spring-boot-consul-service.service.dc1.consul

刷新几次返回如下:

/add, host:192.168.0.184, service_id:application-17003; result : 3; port:17003
/add, host:192.168.0.185, service_id:application-17003; result : 3; port:17003
/add, host:192.168.0.186, service_id:application-17003; result : 3; port:17003

转载于:https://my.oschina.net/tree/blog/1603344

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值