作为项目的一部分,我需要在amazon AWS上创建一个新的负载均衡器。 我需要使用java sdk
来做如何使用代码设置负载均衡器?
这是我到目前为止没有成功的尝试。
//create load balancer
CreateLoadBalancerRequest lbRequest = new CreateLoadBalancerRequest();
lbRequest.setLoadBalancerName("loader");
List<Listener> listeners = new ArrayList<Listener>(1);
listeners.add(new Listener("HTTP", 80, 80));
lbRequest.withAvailabilityZones(availabilityZone1,availabilityZone2);
lbRequest.setListeners(listeners);
CreateLoadBalancerResult lbResult=new CreateLoadBalancerResult();
System.out.println("created load balancer loader");
答案 0 :(得分:15)
首先创建一个elb客户端
AmazonElasticLoadBalancingClient elb = new AmazonElasticLoadBalancingClient(credentials);
然后
使用它来发起请求
//create load balancer
CreateLoadBalancerRequest lbRequest = new CreateLoadBalancerRequest();
lbRequest.setLoadBalancerName("loader");
List<Listener> listeners = new ArrayList<Listener>(1);
listeners.add(new Listener("HTTP", 80, 80));
lbRequest.withAvailabilityZones(availabilityZone1,availabilityZone2);
lbRequest.setListeners(listeners);
CreateLoadBalancerResult lbResult=elb.createLoadBalancer(lbRequest);
System.out.println("created load balancer loader");
然后将您的实例注册到负载均衡器
//get the running instances
DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
List<Reservation> reservations = describeInstancesRequest.getReservations();
List<Instance> instances = new ArrayList<Instance>();
for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
}
//get instance id's
String id;
List instanceId=new ArrayList();
List instanceIdString=new ArrayList();
Iterator<Instance> iterator=instances.iterator();
while (iterator.hasNext())
{
id=iterator.next().getInstanceId();
instanceId.add(new com.amazonaws.services.elasticloadbalancing.model.Instance(id));
instanceIdString.add(id);
}
//register the instances to the balancer
RegisterInstancesWithLoadBalancerRequest register =new RegisterInstancesWithLoadBalancerRequest();
register.setLoadBalancerName("loader");
register.setInstances((Collection)instanceId);
RegisterInstancesWithLoadBalancerResult registerWithLoadBalancerResult= elb.registerInstancesWithLoadBalancer(register);
你完成了......
答案 1 :(得分:0)
V2代码-
package com.example.loadbalance;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.elasticloadbalancing.ElasticLoadBalancingClient;
import software.amazon.awssdk.services.elasticloadbalancing.model.CreateLoadBalancerRequest;
import software.amazon.awssdk.services.elasticloadbalancing.model.ElasticLoadBalancingException;
import software.amazon.awssdk.services.elasticloadbalancing.model.CreateLoadBalancerResponse;
import software.amazon.awssdk.services.elasticloadbalancing.model.Listener;
import java.util.ArrayList;
import java.util.List;
public class CreateLoadBalancer {
public static void main(String[] args) {
Region region = Region.US_WEST_2;
ElasticLoadBalancingClient loadClient = ElasticLoadBalancingClient.builder()
.region(region)
.build();
try {
Listener listener = Listener.builder()
.protocol("HTTP")
.instancePort(80)
.loadBalancerPort(80)
.build();
List<Listener> listeners = new ArrayList<>();
listeners.add(listener);
List<String> avZone = new ArrayList<String>();
avZone.add("us-west-2a");
avZone.add("us-west-2b");
//Define security groups
List<String> secGroups = new ArrayList<String>();
secGroups.add("sg-7aba5827");
CreateLoadBalancerRequest loadBalancerRequest = CreateLoadBalancerRequest.builder()
.availabilityZones(avZone)
.securityGroups(secGroups)
.loadBalancerName("LoadB44")
.listeners(listeners)
.scheme("internet-facing")
.build();
CreateLoadBalancerResponse response = loadClient.createLoadBalancer(loadBalancerRequest);
System.out.println("The DNS Name is "+response.dnsName());
} catch (ElasticLoadBalancingException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}