The load balance of the first knowledge Kong

Source: Internet
Author: User
Tags curl nginx load balancing

Use the Kong Community Edition (Community edition v1.3.0) to build a load balancer, since Kong is based on Openresty and Openresty is a two-time package for Nginx, all many of the configuration items are similar to Nginx.

To see a more typical Nginx load balancing configuration

Upstream Hello {
    server localhost:3000 weight=100;
    Server localhost:3001 weight=50;
}

server {
    listen  ;
    Location/hello {
        proxy_pass Http://hello
    }
}

Nginx listens for requests from the local 80 port and, if the path matches/hello, forwards the request to the upstream named Hello, which upstream we configure a load balancer and route to the local 3000 port and 3001 port.

@SpringBootApplication
@RestController Public
class Kongdemoapplication {public

   static void main ( String[] args) {
      system.setproperty ("Server.port", "3000");
      System.setproperty ("Server.port", "3001");
      Springapplication.run (Kongdemoapplication.class, args);
   }

   @RequestMapping ("/hi") public
    String port () {return
       system.getproperty ("Server.port");
    }

Start two servers to listen for local 3000 ports and 3001 ports, respectively.

How your machine has been installed Kong, and the Kong Admin API has a basic understanding of the next can be for the Kong load-balanced configuration.

configuring upstream and Target

Create a name for Hello upstream

Curl-x POST http://localhost:8001/upstreams--data "Name=hello"

Add two load balancing nodes to Hello

Curl-x POST http://localhost:8001/upstreams/hello/targets--data "target=localhost:3000"--data "weight=100"
Curl-x POST http://localhost:8001/upstreams/hello/targets--data "target=localhost:3001"--data "weight=50"

The configuration above corresponds to the configuration of the Nginx

Upstream Hello {
    server localhost:3000 weight=100;
    Server localhost:3001 weight=50;
}
Configure service and Route

Users with older versions of Kong may have access to the concept of APIs, but in Kong v1.3.0, they have been abolished and replaced with service and route configuration.

Configure a service

Curl-x POST http://localhost:8001/services--data "Name=hello"--data "Host=hello"

The value of the host corresponds to the name of the upstream, which returns the ID of the generated service after the successful configuration, and my return result: 8695cc65-16c1-43b1-95a1-5d30d0a50409

Configure routing information for the service above

Curl-x POST http://localhost:8001/routes--data "Paths[]=/hello--data" service.id= 8695cc65-16c1-43b1-95a1-5d30d0a50409 "

The request path contains/hello requests that are transferred to the corresponding service for processing.

The configuration above will correspond to the

Location/hello {
    proxy_pass http://hello;
}
load balancing of test Kong
Curl Http://localhost:8000/hello/hi

Because of the complex equilibrium reason, needs to test several times, many times after curl the result is as follows:

3000
3000
3000
3000
3000 3000 3001 3001 3001
3000
3001
reference Documentation

https://getkong.org/docs/0.13.x/loadbalancing/

https://getkong.org/docs/0.13.x/configuration/
The original stick poked me

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.