edit-icon download-icon

Server Load Balancer

Last Updated: Mar 20, 2018

Alibaba Cloud Server Load Balancer (SLB) is a traffic distribution control service that distributes the incoming traffic among multiple Elastic Compute Service (ECS) instances according to the configured forwarding rules. It expands the service capabilities of the application and increases the availability of the application. For more information, see Server Load Balancer documentation.

This tutorial uses the CreateLoadBalancer API of SLB to show you how to use Alibaba Cloud Java SDK to call SLB APIs.

Note: Running the code in this example will create an SLB instance and generate fees.

  1. import java.util.UUID;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.exceptions.ServerException;
  6. import com.aliyuncs.profile.DefaultProfile;
  7. import com.aliyuncs.slb.model.v20140515.CreateLoadBalancerRequest;
  8. import com.aliyuncs.slb.model.v20140515.CreateLoadBalancerResponse;
  9. public class Demo {
  10. public static void main(String[] args) {
  11. // Create and initialize the DefaultAcsClient instance
  12. DefaultProfile profile = DefaultProfile.getProfile(
  13. "<your-region-id>", // Your region ID
  14. "<your-access-key-id>", // Your AccessKey ID
  15. "<your-access-key-secret>"); // Your AccessKey Secret
  16. IAcsClient client = new DefaultAcsClient(profile);
  17. // Create a request and set parameters
  18. CreateLoadBalancerRequest request = new CreateLoadBalancerRequest();
  19. request.setLoadBalancerName("MyLoadBalancer");
  20. request.setAddressType("internet");
  21. request.setClientToken(UUID.randomUUID().toString());
  22. // Initiate the request and handle the response or exceptions
  23. CreateLoadBalancerResponse response;
  24. try {
  25. response = client.getAcsResponse(request);
  26. String loadBalancerId = response.getLoadBalancerId();
  27. System.out.println("Create loadBalancer success, loadBalancerId = " + loadBalancerId);
  28. } catch (ServerException e) {
  29. e.printStackTrace();
  30. } catch (ClientException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
Thank you! We've received your feedback.