Swan Lake Beta3 is here.
import ballerina/http;

// Create an endpoint with port 8080 for the mock backend services.
listener http:Listener backendEP = check new (8080);

// Define the load balance client endpoint to call the backend services.
final http:LoadBalanceClient lbBackendEP = check new ({
        // Define the set of HTTP clients that need to be load balanced.
        targets: [
            {url: "http://localhost:8080/mock1"},
            {url: "http://localhost:8080/mock2"},
            {url: "http://localhost:8080/mock3"}
        ],

        timeout: 5
});

service / on new http:Listener(9090) {
    resource function 'default lb() returns string|error {
        string payload = check lbBackendEP->get("/");
        return payload;
    }
}

// Define the mock backend services, which are called by the load balancer.
service /mock1 on backendEP {
    resource function get .() returns string {
        return "Mock1 resource was invoked.";
    }
}

service /mock2 on backendEP {
    resource function get .() returns string {
        return "Mock2 resource was invoked.";
    }
}

service /mock3 on backendEP {
    resource function get .() returns string {
        return "Mock3 resource was invoked.";
    }
}

Load Balancer

An HTTP load balancing endpoint is used when the request load needs to be load balanced across a given set of target endpoints.

For more information on the underlying module, see the HTTP module.

import ballerina/http;
listener http:Listener backendEP = check new (8080);

Create an endpoint with port 8080 for the mock backend services.

final http:LoadBalanceClient lbBackendEP = check new ({

Define the load balance client endpoint to call the backend services.

        targets: [
            {url: "http://localhost:8080/mock1"},
            {url: "http://localhost:8080/mock2"},
            {url: "http://localhost:8080/mock3"}
        ],

Define the set of HTTP clients that need to be load balanced.

        timeout: 5
});
service / on new http:Listener(9090) {
    resource function 'default lb() returns string|error {
        string payload = check lbBackendEP->get("/");
        return payload;
    }
}
service /mock1 on backendEP {
    resource function get .() returns string {
        return "Mock1 resource was invoked.";
    }
}

Define the mock backend services, which are called by the load balancer.

service /mock2 on backendEP {
    resource function get .() returns string {
        return "Mock2 resource was invoked.";
    }
}
service /mock3 on backendEP {
    resource function get .() returns string {
        return "Mock3 resource was invoked.";
    }
}
bal run http_load_balancer.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:8080
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
# To invoke the Load Balancer Demo Service, use the cURL command below.
curl  http://localhost:9090/lb
Mock1 resource was invoked.
# Repeat the same cURL command to invoke the service again and it will be load balanced to the second mock service.
curl http://localhost:9090/lb
Mock2 resource was invoked.
# The following request will be load balanced to the third mock service.
curl http://localhost:9090/lb
Mock3 resource was invoked.
# The following request will be load balanced to the first mock service again.
curl http://localhost:9090/lb
Mock1 resource was invoked.
import ballerina/http;

// Create an endpoint with port 8080 for the mock backend services.
listener http:Listener backendEP = check new (8080);

// Define the load balance client endpoint to call the backend services.
final http:LoadBalanceClient lbBackendEP = check new ({
        // Define the set of HTTP clients that need to be load balanced.
        targets: [
            {url: "http://localhost:8080/mock1"},
            {url: "http://localhost:8080/mock2"},
            {url: "http://localhost:8080/mock3"}
        ],

        timeout: 5
});

service / on new http:Listener(9090) {
    resource function 'default lb() returns string|error {
        string payload = check lbBackendEP->get("/");
        return payload;
    }
}

// Define the mock backend services, which are called by the load balancer.
service /mock1 on backendEP {
    resource function get .() returns string {
        return "Mock1 resource was invoked.";
    }
}

service /mock2 on backendEP {
    resource function get .() returns string {
        return "Mock2 resource was invoked.";
    }
}

service /mock3 on backendEP {
    resource function get .() returns string {
        return "Mock3 resource was invoked.";
    }
}

Load Balancer

An HTTP load balancing endpoint is used when the request load needs to be load balanced across a given set of target endpoints.

For more information on the underlying module, see the HTTP module.

import ballerina/http;
listener http:Listener backendEP = check new (8080);

Create an endpoint with port 8080 for the mock backend services.

final http:LoadBalanceClient lbBackendEP = check new ({

Define the load balance client endpoint to call the backend services.

        targets: [
            {url: "http://localhost:8080/mock1"},
            {url: "http://localhost:8080/mock2"},
            {url: "http://localhost:8080/mock3"}
        ],

Define the set of HTTP clients that need to be load balanced.

        timeout: 5
});
service / on new http:Listener(9090) {
    resource function 'default lb() returns string|error {
        string payload = check lbBackendEP->get("/");
        return payload;
    }
}
service /mock1 on backendEP {
    resource function get .() returns string {
        return "Mock1 resource was invoked.";
    }
}

Define the mock backend services, which are called by the load balancer.

service /mock2 on backendEP {
    resource function get .() returns string {
        return "Mock2 resource was invoked.";
    }
}
service /mock3 on backendEP {
    resource function get .() returns string {
        return "Mock3 resource was invoked.";
    }
}
bal run http_load_balancer.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:8080
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
# To invoke the Load Balancer Demo Service, use the cURL command below.
curl  http://localhost:9090/lb
Mock1 resource was invoked.
# Repeat the same cURL command to invoke the service again and it will be load balanced to the second mock service.
curl http://localhost:9090/lb
Mock2 resource was invoked.
# The following request will be load balanced to the third mock service.
curl http://localhost:9090/lb
Mock3 resource was invoked.
# The following request will be load balanced to the first mock service again.
curl http://localhost:9090/lb
Mock1 resource was invoked.
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand