|
|
package com.monitor.awstools.service.impl;
|
|
|
|
|
|
import com.amazonaws.regions.Regions;
|
|
|
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
|
|
|
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
|
|
|
import com.amazonaws.services.ec2.model.Instance;
|
|
|
import com.amazonaws.services.ec2.model.Reservation;
|
|
|
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancing;
|
|
|
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient;
|
|
|
import com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersRequest;
|
|
|
import com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult;
|
|
|
import com.amazonaws.services.elasticloadbalancing.model.ListenerDescription;
|
|
|
import com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription;
|
|
|
import com.monitor.awstools.comp.AWSClientComp;
|
|
|
import com.monitor.awstools.service.AwsLbService;
|
|
|
import com.monitor.cloudtools.model.CommLbBackends;
|
|
|
import com.monitor.cloudtools.model.CommLbInstances;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* aws elb
|
|
|
* Created by xh on 2017/6/14.
|
|
|
*/
|
|
|
@Service
|
|
|
public class AwsLbServiceImpl implements AwsLbService {
|
|
|
public static final Logger logger = LoggerFactory.getLogger("cloudLbLogger");
|
|
|
|
|
|
@Autowired
|
|
|
AWSClientComp awsClientComp = new AWSClientComp();
|
|
|
|
|
|
/**
|
|
|
* 查询负载均衡实例列表
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public List<CommLbInstances> getLbInstances() {
|
|
|
List<CommLbInstances> listCommInstances = null;
|
|
|
List<CommLbBackends> listCommBackends = null;
|
|
|
CommLbInstances commInstances = null;
|
|
|
CommLbBackends commBackends = null;
|
|
|
|
|
|
try{
|
|
|
AmazonElasticLoadBalancing client = new AmazonElasticLoadBalancingClient(awsClientComp.getAWSCredentials());
|
|
|
client.setRegion(awsClientComp.getRegion(Regions.CN_NORTH_1));
|
|
|
DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest();
|
|
|
DescribeLoadBalancersResult result = client.describeLoadBalancers(request);
|
|
|
if(result == null || CollectionUtils.isEmpty(result.getLoadBalancerDescriptions())){
|
|
|
return null;
|
|
|
}
|
|
|
List<LoadBalancerDescription> listBalancer = result.getLoadBalancerDescriptions();
|
|
|
if(CollectionUtils.isEmpty(listBalancer)){
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
listCommInstances = new ArrayList<>();
|
|
|
for(LoadBalancerDescription balancer : listBalancer){
|
|
|
commInstances = new CommLbInstances();
|
|
|
commInstances.setCloudType("aws");
|
|
|
if("internet-facing".equals(balancer.getScheme())){
|
|
|
commInstances.setNetType(1);
|
|
|
}else{
|
|
|
commInstances.setNetType(0);
|
|
|
}
|
|
|
commInstances.setName(balancer.getLoadBalancerName());
|
|
|
commInstances.setAddress(balancer.getDNSName());
|
|
|
|
|
|
if(CollectionUtils.isNotEmpty(balancer.getListenerDescriptions())){
|
|
|
List<Integer> ports = new ArrayList<>();
|
|
|
for(ListenerDescription listener : balancer.getListenerDescriptions()){
|
|
|
if(listener.getListener() == null){
|
|
|
continue;
|
|
|
}
|
|
|
ports.add(listener.getListener().getLoadBalancerPort());
|
|
|
}
|
|
|
commInstances.setPorts(ports);
|
|
|
}
|
|
|
if(CollectionUtils.isNotEmpty(balancer.getInstances())){
|
|
|
List<String> instances = new ArrayList<>();
|
|
|
for(com.amazonaws.services.elasticloadbalancing.model.Instance instance : balancer.getInstances()){
|
|
|
instances.add(instance.getInstanceId());
|
|
|
}
|
|
|
List<CommLbBackends> listBackend = this.getDescribeInstances(instances);
|
|
|
commInstances.setBackends(listBackend);
|
|
|
}
|
|
|
|
|
|
listCommInstances.add(commInstances);
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
logger.error("AwsLbServiceImpl - getLbInstances - error" , e);
|
|
|
}
|
|
|
return listCommInstances;
|
|
|
}
|
|
|
|
|
|
private List<CommLbBackends> getDescribeInstances(List<String> ids){
|
|
|
List<CommLbBackends> listBackend = null;
|
|
|
CommLbBackends backend = null;
|
|
|
if(CollectionUtils.isEmpty(ids)){
|
|
|
return null;
|
|
|
}
|
|
|
try{
|
|
|
DescribeInstancesRequest request = new DescribeInstancesRequest()
|
|
|
.withInstanceIds(ids);
|
|
|
DescribeInstancesResult result = awsClientComp.getClient().describeInstances(request);
|
|
|
if(result == null || CollectionUtils.isEmpty(result.getReservations())){
|
|
|
return null;
|
|
|
}
|
|
|
listBackend = new ArrayList<>();
|
|
|
for(Reservation res : result.getReservations()){
|
|
|
List<Instance> instances = res.getInstances();
|
|
|
if(CollectionUtils.isEmpty(instances)){
|
|
|
continue;
|
|
|
}
|
|
|
for(Instance instance : instances) {
|
|
|
backend = new CommLbBackends();
|
|
|
backend.setInstanceId(instance.getInstanceId());
|
|
|
backend.setLanIp(instance.getPrivateIpAddress());
|
|
|
|
|
|
listBackend.add(backend);
|
|
|
}
|
|
|
}
|
|
|
}catch(Exception e){
|
|
|
logger.error("AwsLbServiceImpl - getDescribeInstances - error" , e);
|
|
|
}
|
|
|
|
|
|
return listBackend;
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|