AWS 之Load Balance篇
public class CreateELB
{
/// <summary>
/// 连接AWS服务器
/// </summary>
/// <param name="awsAccessKeyId">Access Key Id</param>
/// <param name="awsSecretAccessKey">Secret Access Key</param>
/// <param name="regionEndpoint">服务器区域</param>
/// <returns></returns>
public AmazonElasticLoadBalancingClient CreateELB(string awsAccessKeyId, string awsSecretAccessKey, RegionEndpoint regionEndpoint)
{
AmazonElasticLoadBalancingClient client = new AmazonElasticLoadBalancingClient(awsAccessKeyId, awsSecretAccessKey, regionEndpoint);
return client;
} #region 添加负载均衡器
/// <summary>
/// 添加负载均衡器
/// </summary>
/// <param name="client">连接请求</param>
/// <param name="LoadBalancerName">负载均衡器名称</param>
/// <returns></returns>
private string CreateLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
{
string DNSName = "";
try
{
List<string> availabilityZonesList = GetAvailabilityZonesList();
List<Listener> listenerList = GetListenerList();
List<Tag> tagList = GetTagList();
List<string> securityGroupslist = GetSecurityGroupslist();
if (!ExistLoadBalancer(client, LoadBalancerName))
{
CreateLoadBalancerRequest request = new CreateLoadBalancerRequest()
{
//可用区域
AvailabilityZones = availabilityZonesList,
//侦听器
Listeners = listenerList,
//负载平衡名称
LoadBalancerName = LoadBalancerName,
//设置负载平衡是面向 Internet , 默认 面向Internet
// Scheme = "",
//安全组
SecurityGroups = securityGroupslist,
//子网
// Subnets = list,
//标签
Tags = tagList
};
CreateLoadBalancerResponse response = client.CreateLoadBalancer(request);
DNSName = response.DNSName;
if (RegisterInstancesWithLoadBalancer(client, LoadBalancerName))
{
Console.Write("添加实例成功");
Console.WriteLine("");
}
if (ConfigureHealthCheck(client, LoadBalancerName))
{
Console.Write("配置运行状况检查成功");
Console.WriteLine("");
}
}
else
{
Console.Write("名为<" + LoadBalancerName + ">负载均衡存在");
Console.WriteLine("");
}
}
catch (Exception)
{
throw;
}
return DNSName;
}
#endregion
#region 可用区域实体
/// <summary>
/// 可用区域实体
/// </summary>
/// <returns></returns>
private List<string> GetAvailabilityZonesList()
{
List<string> availabilityZonesList = new List<string>();
string availabilityZones = "ap-southeast-1a";
availabilityZonesList.Add(availabilityZones);
return availabilityZonesList;
}
#endregion
#region 监听器实体
/// <summary>
/// 监听器实体
/// </summary>
/// <returns></returns>
private List<Listener> GetListenerList()
{
List<Listener> listenerList = new List<Listener>();
Listener listener = new Listener();
listener.InstancePort = ;
listener.InstanceProtocol = "HTTP";
listener.LoadBalancerPort = ;
listener.Protocol = "HTTP";
listenerList.Add(listener);
return listenerList;
}
#endregion
#region 标签实体
/// <summary>
/// 标签实体
/// </summary>
/// <returns></returns>
private List<Tag> GetTagList()
{
//添加标签
List<Tag> tagList = new List<Tag>(); Tag tag = new Tag();
tag.Key = "key";
tag.Value = "value";
tagList.Add(tag);
return tagList;
}
#endregion
#region 安全组实体
/// <summary>
/// 安全组实体
/// </summary>
/// <returns></returns>
private List<string> GetSecurityGroupslist()
{
string str = "sg-4afa562f";
List<string> securityGroupslist = new List<string>();
securityGroupslist.Add(str);
return securityGroupslist;
}
#endregion
#region 添加实例
/// <summary>
/// 添加实例
/// </summary>
/// <param name="client"></param>
/// <param name="LoadBalancerName"></param>
/// <returns></returns>
private bool RegisterInstancesWithLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
{
bool result = false;
try
{
List<Instance> instanceList = new List<Instance>();
Instance instance = new Instance();
instance.InstanceId = "i-18de45d5";
instanceList.Add(instance); Instance instance1 = new Instance();
instance1.InstanceId = "i-26d918e9";
instanceList.Add(instance1);
RegisterInstancesWithLoadBalancerRequest requestInstance = new RegisterInstancesWithLoadBalancerRequest()
{
Instances = instanceList,
LoadBalancerName = LoadBalancerName
};
RegisterInstancesWithLoadBalancerResponse responseInstance = client.RegisterInstancesWithLoadBalancer(requestInstance);
result = true;
}
catch (Exception)
{
throw;
}
return result;
}
#endregion
#region 配置运行状况检查
/// <summary>
/// 配置运行状况检查
/// </summary>
/// <param name="client"></param>
/// <param name="LoadBalancerName"></param>
/// <returns></returns>
private bool ConfigureHealthCheck(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
{
bool result = false;
try
{
HealthCheck healthCheck = new HealthCheck();
healthCheck.HealthyThreshold = ;
healthCheck.Interval = ;
healthCheck.Target = "HTTP:80/index.html";
healthCheck.Timeout = ;
healthCheck.UnhealthyThreshold = ;
ConfigureHealthCheckRequest requestHealth = new ConfigureHealthCheckRequest()
{
HealthCheck = healthCheck,
LoadBalancerName = LoadBalancerName
};
ConfigureHealthCheckResponse responseHealth = client.ConfigureHealthCheck(requestHealth);
result = true;
}
catch (Exception)
{
throw;
}
return result;
}
#endregion
#region 删除负载均衡器
/// <summary>
/// 删除负载均衡器
/// </summary>
/// <param name="client"></param>
/// <param name="LoadBalancerName"></param>
/// <returns></returns>
private string DeleteLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
{
string result = "";
try
{
DeleteLoadBalancerRequest request = new DeleteLoadBalancerRequest()
{
LoadBalancerName = LoadBalancerName
};
DeleteLoadBalancerResponse response = client.DeleteLoadBalancer(request);
result = response.HttpStatusCode.ToString();
}
catch (Exception)
{
throw;
}
return result;
}
#endregion
#region 负载均衡器是否存在
/// <summary>
/// 负载均衡器是否存在
/// </summary>
/// <param name="client"></param>
/// <param name="LoadBalancerName"></param>
private bool ExistLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
{
bool result = false;
DescribeLoadBalancersResponse response = client.DescribeLoadBalancers();
List<LoadBalancerDescription> loadBalancerDescriptionList = response.LoadBalancerDescriptions;
foreach (LoadBalancerDescription loadBalancerDescription in loadBalancerDescriptionList)
{
if (loadBalancerDescription.LoadBalancerName == LoadBalancerName)
{
result = true;
}
}
return result;
}
#endregion
#region 得到负载均衡器DNS名称
/// <summary>
/// 得到负载均衡器DNS名称
/// </summary>
/// <param name="client"></param>
/// <param name="LoadBalancerName"></param>
/// <returns></returns>
private string GetLoadBalancerDescription(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
{
string DNSName = "";
DescribeLoadBalancersResponse response = client.DescribeLoadBalancers();
List<LoadBalancerDescription> loadBalancerDescriptionList = response.LoadBalancerDescriptions;
foreach (LoadBalancerDescription loadBalancerDescription in loadBalancerDescriptionList)
{
if (loadBalancerDescription.LoadBalancerName == LoadBalancerName)
{
DNSName = loadBalancerDescription.DNSName;
}
}
return DNSName;
}
#endregion }
AWS 之Load Balance篇的更多相关文章
- AWS的load balance
Route53实现了地理上的load balance; ELB实现了region内的load balance CloudFront实现了静态内容的全网加速 ZULh?*;&T(
- 亲密接触Redis-第三天(Redis的Load Balance)
前言 上两天讲述了Redis的基本搭建和基于HA的集群布署方式以及相关的策略和注意点.今天开始讲述Redis的Cluster功能,而这块目前来说网上资料不是太全,就算有1,2篇也只是单讲服务端的搭建也 ...
- LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级
LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一 ...
- Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server
目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...
- Oracle RAC 服务器端连接负载均衡(Load Balance)
Oracle RAC服务器端的负载均衡是根据RAC中各节点的连接负荷数情况,将新的连接请求分配到负荷最小的节点上去.当数据库处于运行时,RAC中各节点的PMON进程每3秒会将各自节点的连接负荷数更新到 ...
- Oracle RAC 客户端连接负载均衡(Load Balance)
实现负载均衡(Load Balance)是Oracle RAC最重要的特性之一,主要是把负载平均分配到集群中的各个节点,以提高系统的整体吞吐能力.通常情况下有两种方式来实现负载均衡,一个是基于客户端连 ...
- Using load balance for thrift servers
Software load balance .Nginx(http://nginx.org) 1.Install nginx download source code from http://ngin ...
- Neutron: Load Balance as a Service(LBaaS)负载均衡
load balancer 负责监听外部的连接,并将连接分发到 pool member. LBaaS 有三个主要的概念: Pool Member,Pool 和 Virtual IP Pool M ...
- Flume 高可用配置案例+load balance负载均衡+ 案例:日志的采集及汇总
高可用配置案例 (一).failover故障转移 在完成单点的Flume NG搭建后,下面我们搭建一个高可用的Flume NG集群,架构图如下所示: (1)节点分配 Flume的Agent和Colle ...
随机推荐
- jQuery中的height()、innerheight()、outerheight()的区别总结
在前端jQuery代码中突然看到outerheight(),第一感觉就是,这是什么鬼?然后仔细查阅了一下,居然发现还有这么多相似的东西. 在jQuery中,获取元素高度的函数有3个,它们分别是heig ...
- unity3d中dllimport方法的使用,以接入腾讯平台为例!!!
说到有关dllimport方法可能还有很多人比较陌生,其实我自己也说不太清楚,大概说说什么时候要用它. 事实上功能类似于调用android的第三包,我们想要使用苹果上特定的api或者第三方平台的一些东 ...
- 小圣求职记A:腾讯篇
本人普通985高校计算机专业研究生一枚,从9月12号开始正式找工作,一个月过去了,参加了能参加的各个互联网公司的宣讲.笔试.面试,现用两篇随笔分享所见所闻.随笔A将以腾讯为例详细展示整个过程,随笔B将 ...
- 后缀树系列一:概念以及实现原理( the Ukkonen algorithm)
首先说明一下后缀树系列一共会有三篇文章,本文先介绍基本概念以及如何线性时间内构件后缀树,第二篇文章会详细介绍怎么实现后缀树(包含实现代码),第三篇会着重谈一谈后缀树的应用. 本文分为三个部分, 首先介 ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- 二、Android NDK编程预备之Java jni入门Hello World
转自: http://www.eoeandroid.com/forum.php?mod=viewthread&tid=264543&fromuid=588695 昨天已经简要介绍了J ...
- Maven的配置文件pom.xml
Maven的配置文件pom.xml 简介: 什么是POM? POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示,名称叫做pom.xml. ...
- Install wxWidgets-3.0.2 on GNU/Linux Debian
转载自 http://www.binarytides.com/install-wxwidgets-ubuntu/ wxWidgets wxWidgets is an application devel ...
- spring_150803_component
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- lintcode:交错正负数
交错正负数 给出一个含有正整数和负整数的数组,重新排列成一个正负数交错的数组. 注意事项 不需要保持正整数或者负整数原来的顺序. 样例 给出数组[-1, -2, -3, 4, 5, 6],重新排序之后 ...