Authored by xuhui

te

package com.monitor.awstools.service;
import com.monitor.cloudtools.model.CommLbInstances;
import java.util.List;
/**
* Created by xh on 2017/6/14.
*/
public interface AwsLbService {
/**
* 查询负载均衡实例列表
* @return
*/
List<CommLbInstances> getLbInstances();
}
\ No newline at end of file
... ...
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
... ...
... ... @@ -5,14 +5,21 @@ package com.monitor.cloudtools.controller;
*/
import com.alibaba.fastjson.JSON;
import com.model.InternalDomain;
import com.monitor.awstools.service.AwsLbService;
import com.monitor.cloudtools.model.CommLbInstances;
import com.monitor.cloudtools.model.DnsPodRes;
import com.monitor.cloudtools.util.DnsPodUtil;
import com.monitor.model.response.BaseResponse;
import com.monitor.mysql.mapper.InternalDomainMapper;
import com.monitor.qcloudtools.service.QcloudLbService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
... ... @@ -29,6 +36,15 @@ public class CloudLbController {
private QcloudLbService qcloudLbService;
/**
* 亚马逊云
*/
@Autowired
private AwsLbService awsLbService;
@Autowired
private InternalDomainMapper internalDomainMapper;
/**
* 伸缩组信息查询
* @param cloudType 云类型 1:AWS 2:QCLOUD
* @return
... ... @@ -39,12 +55,53 @@ public class CloudLbController {
BaseResponse response;
List<CommLbInstances> instances = null;
if(1 == cloudType){
instances = awsLbService.getLbInstances();
}else if(2 == cloudType){
instances = qcloudLbService.getLbInstances();
} else{
return null;
}
try{
if(CollectionUtils.isNotEmpty(instances)){
List<DnsPodRes> listRes1 = DnsPodUtil.getDnsPod1();
List<DnsPodRes> listRes2 = DnsPodUtil.getDnsPod2();
for(CommLbInstances instance : instances){
List<String> commDomains = new ArrayList<>();
if(instance.getNetType() == 0){
//内网
List<InternalDomain> domains = internalDomainMapper.selectByValue(instance.getAddress());
if(CollectionUtils.isEmpty(domains)){
continue;
}
for(InternalDomain domain : domains){
if(commDomains.contains(domain.getName() + "." + domain.getDomain())){
continue;
}
commDomains.add(domain.getName() + "." + domain.getDomain());
}
}else{
//外网
for(DnsPodRes res : listRes1){
if(res.getValue().indexOf(instance.getAddress()) > -1){
commDomains.add(res.getName() + "." + "yoho.cn");
}
}
for(DnsPodRes res : listRes2){
if(res.getValue().indexOf(instance.getAddress()) > -1){
commDomains.add(res.getName() + "." + "yohobuy.com");
}
}
}
instance.setDomain(commDomains);
}
}
}catch (Exception e){
System.out.printf(e.getMessage());
}
response = new BaseResponse();
String result = JSON.toJSONString(instances);
response.setData(instances);
... ...
... ... @@ -35,9 +35,8 @@ public class CommLbInstances {
/**
* 关联域名
* internal_domain表
*/
private String domain;
private List<String> domain;
/**
* 监听的端口号集合
... ...
package com.monitor.cloudtools.model;
import lombok.Data;
/**
* Created by xh on 2017/6/14.
*/
@Data
public class DnsPodRes {
private String value;
private String name;
}
\ No newline at end of file
... ...
package com.monitor.cloudtools.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.monitor.cloudtools.model.DnsPodRes;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xh on 2017/6/14.
*/
public class DnsPodUtil {
private static String login_token="31578,5f5402160468dc375159e2e94eeef1da";
private static String format = "json";
/**
* yoho.cn
*/
public static List<DnsPodRes> getDnsPod1(){
List<DnsPodRes> listRes = new ArrayList<>();
MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
requestEntity.add("login_token", login_token);
requestEntity.add("format",format);
requestEntity.add("domain_id", "16862974");
try{
RestTemplate restTemplate = new RestTemplate();
String yoho = restTemplate.postForObject("https://dnsapi.cn/Record.List", requestEntity, String.class);
JSONObject responseJSON = JSONObject.parseObject(yoho);
listRes = JSON.parseArray(responseJSON.getString("records"), DnsPodRes.class);
}catch (Exception e){
}
return listRes;
}
/**
* yohobuy.com
*/
public static List<DnsPodRes> getDnsPod2(){
List<DnsPodRes> listRes = new ArrayList<>();
MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
requestEntity.add("login_token", login_token);
requestEntity.add("format",format);
requestEntity.add("domain_id", "20765505");
try{
RestTemplate restTemplate = new RestTemplate();
String yohobuy = restTemplate.postForObject("https://dnsapi.cn/Record.List", requestEntity, String.class);
JSONObject responseJSON = JSONObject.parseObject(yohobuy);
listRes = JSON.parseArray(responseJSON.getString("records"), DnsPodRes.class);
}catch (Exception e){
}
return listRes;
}
}
\ No newline at end of file
... ...
... ... @@ -25,7 +25,7 @@ import java.util.TreeMap;
*/
@Service
public class QcloudLbServiceImpl implements QcloudLbService {
public static final Logger logger = LoggerFactory.getLogger("cloudElbLogger");
public static final Logger logger = LoggerFactory.getLogger("cloudLbLogger");
/**
* 查询负载均衡实例列表
... ... @@ -63,7 +63,6 @@ public class QcloudLbServiceImpl implements QcloudLbService {
if(CollectionUtils.isNotEmpty(instance.getLoadBalancerVips())){
commInstances.setAddress(StringUtils.join(instance.getLoadBalancerVips(),','));
}
commInstances.setDomain("");
if(CollectionUtils.isNotEmpty(listeners)){
List<Integer> ports = new ArrayList<>();
... ...
... ... @@ -22,4 +22,6 @@ public interface InternalDomainMapper {
InternalDomain selectByCondition(@Param("domain") String domain,@Param("environment") String environment,@Param("name") String name,@Param("type") String type);
List<InternalDomain> selectByValue(@Param("value")String value);
}
... ...
... ... @@ -73,4 +73,12 @@
delete from internal_domain
where id = #{id}
</delete>
<select id="selectByValue" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from internal_domain
where
value = #{value}
</select>
</mapper>
\ No newline at end of file
... ...