AWS 之Load Balance篇

  1  public class CreateELB
  2     {
  3         /// <summary>
  4         /// 连接AWS服务器
  5         /// </summary>
  6         /// <param name="awsAccessKeyId">Access Key Id</param>
  7         /// <param name="awsSecretAccessKey">Secret Access Key</param>
  8         /// <param name="regionEndpoint">服务器区域</param>
  9         /// <returns></returns>
 10         public AmazonElasticLoadBalancingClient CreateELB(string awsAccessKeyId, string awsSecretAccessKey, RegionEndpoint regionEndpoint)
 11         {
 12             AmazonElasticLoadBalancingClient client = new AmazonElasticLoadBalancingClient(awsAccessKeyId, awsSecretAccessKey, regionEndpoint);
 13             return client;
 14         }
 15 
 16         #region 添加负载均衡器
 17         /// <summary>
 18         /// 添加负载均衡器
 19         /// </summary>
 20         /// <param name="client">连接请求</param>
 21         /// <param name="LoadBalancerName">负载均衡器名称</param>
 22         /// <returns></returns>
 23         private string CreateLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
 24         {
 25             string DNSName = "";
 26             try
 27             {
 28                 List<string> availabilityZonesList = GetAvailabilityZonesList();
 29                 List<Listener> listenerList = GetListenerList();
 30                 List<Tag> tagList = GetTagList();
 31                 List<string> securityGroupslist = GetSecurityGroupslist();
 32                 if (!ExistLoadBalancer(client, LoadBalancerName))
 33                 {
 34                     CreateLoadBalancerRequest request = new CreateLoadBalancerRequest()
 35                     {
 36                         //可用区域
 37                         AvailabilityZones = availabilityZonesList,
 38                         //侦听器
 39                         Listeners = listenerList,
 40                         //负载平衡名称
 41                         LoadBalancerName = LoadBalancerName,
 42                         //设置负载平衡是面向 Internet , 默认 面向Internet
 43                         // Scheme = "",
 44                         //安全组
 45                         SecurityGroups = securityGroupslist,
 46                         //子网
 47                         //    Subnets = list,
 48                         //标签
 49                         Tags = tagList
 50                     };
 51                     CreateLoadBalancerResponse response = client.CreateLoadBalancer(request);
 52                     DNSName = response.DNSName;
 53                     if (RegisterInstancesWithLoadBalancer(client, LoadBalancerName))
 54                     {
 55                         Console.Write("添加实例成功");
 56                         Console.WriteLine("");
 57                     }
 58                     if (ConfigureHealthCheck(client, LoadBalancerName))
 59                     {
 60                         Console.Write("配置运行状况检查成功");
 61                         Console.WriteLine("");
 62                     }
 63                 }
 64                 else
 65                 {
 66                     Console.Write("名为<" + LoadBalancerName + ">负载均衡存在");
 67                     Console.WriteLine("");
 68                 }
 69             }
 70             catch (Exception)
 71             {
 72                 throw;
 73             }
 74             return DNSName;
 75         }
 76         #endregion
 77         #region 可用区域实体
 78         /// <summary>
 79         /// 可用区域实体
 80         /// </summary>
 81         /// <returns></returns>
 82         private List<string> GetAvailabilityZonesList()
 83         {
 84             List<string> availabilityZonesList = new List<string>();
 85             string availabilityZones = "ap-southeast-1a";
 86             availabilityZonesList.Add(availabilityZones);
 87             return availabilityZonesList;
 88         }
 89         #endregion
 90         #region 监听器实体
 91         /// <summary>
 92         /// 监听器实体
 93         /// </summary>
 94         /// <returns></returns>
 95         private List<Listener> GetListenerList()
 96         {
 97             List<Listener> listenerList = new List<Listener>();
 98             Listener listener = new Listener();
 99             listener.InstancePort = 80;
100             listener.InstanceProtocol = "HTTP";
101             listener.LoadBalancerPort = 80;
102             listener.Protocol = "HTTP";
103             listenerList.Add(listener);
104             return listenerList;
105         }
106         #endregion
107         #region 标签实体
108         /// <summary>
109         /// 标签实体
110         /// </summary>
111         /// <returns></returns>
112         private List<Tag> GetTagList()
113         {
114             //添加标签
115             List<Tag> tagList = new List<Tag>();
116 
117             Tag tag = new Tag();
118             tag.Key = "key";
119             tag.Value = "value";
120             tagList.Add(tag);
121             return tagList;
122         }
123         #endregion
124         #region 安全组实体
125         /// <summary>
126         /// 安全组实体
127         /// </summary>
128         /// <returns></returns>
129         private List<string> GetSecurityGroupslist()
130         {
131             string str = "sg-4afa562f";
132             List<string> securityGroupslist = new List<string>();
133             securityGroupslist.Add(str);
134             return securityGroupslist;
135         }
136         #endregion
137         #region 添加实例
138         /// <summary>
139         /// 添加实例
140         /// </summary>
141         /// <param name="client"></param>
142         /// <param name="LoadBalancerName"></param>
143         /// <returns></returns>
144         private bool RegisterInstancesWithLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
145         {
146             bool result = false;
147             try
148             {
149                 List<Instance> instanceList = new List<Instance>();
150                 Instance instance = new Instance();
151                 instance.InstanceId = "i-18de45d5";
152                 instanceList.Add(instance);
153 
154                 Instance instance1 = new Instance();
155                 instance1.InstanceId = "i-26d918e9";
156                 instanceList.Add(instance1);
157                 RegisterInstancesWithLoadBalancerRequest requestInstance = new RegisterInstancesWithLoadBalancerRequest()
158                 {
159                     Instances = instanceList,
160                     LoadBalancerName = LoadBalancerName
161                 };
162                 RegisterInstancesWithLoadBalancerResponse responseInstance = client.RegisterInstancesWithLoadBalancer(requestInstance);
163                 result = true;
164             }
165             catch (Exception)
166             {
167                 throw;
168             }
169             return result;
170         }
171         #endregion
172         #region 配置运行状况检查
173         /// <summary>
174         /// 配置运行状况检查
175         /// </summary>
176         /// <param name="client"></param>
177         /// <param name="LoadBalancerName"></param>
178         /// <returns></returns>
179         private bool ConfigureHealthCheck(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
180         {
181             bool result = false;
182             try
183             {
184                 HealthCheck healthCheck = new HealthCheck();
185                 healthCheck.HealthyThreshold = 10;
186                 healthCheck.Interval = 30;
187                 healthCheck.Target = "HTTP:80/index.html";
188                 healthCheck.Timeout = 5;
189                 healthCheck.UnhealthyThreshold = 2;
190                 ConfigureHealthCheckRequest requestHealth = new ConfigureHealthCheckRequest()
191                 {
192                     HealthCheck = healthCheck,
193                     LoadBalancerName = LoadBalancerName
194                 };
195                 ConfigureHealthCheckResponse responseHealth = client.ConfigureHealthCheck(requestHealth);
196                 result = true;
197             }
198             catch (Exception)
199             {
200                 throw;
201             }
202             return result;
203         }
204         #endregion
205         #region 删除负载均衡器
206         /// <summary>
207         /// 删除负载均衡器
208         /// </summary>
209         /// <param name="client"></param>
210         /// <param name="LoadBalancerName"></param>
211         /// <returns></returns>
212         private string DeleteLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
213         {
214             string result = "";
215             try
216             {
217                 DeleteLoadBalancerRequest request = new DeleteLoadBalancerRequest()
218                 {
219                     LoadBalancerName = LoadBalancerName
220                 };
221                 DeleteLoadBalancerResponse response = client.DeleteLoadBalancer(request);
222                 result = response.HttpStatusCode.ToString();
223             }
224             catch (Exception)
225             {
226                 throw;
227             }
228             return result;
229         }
230         #endregion
231         #region 负载均衡器是否存在
232         /// <summary>
233         /// 负载均衡器是否存在
234         /// </summary>
235         /// <param name="client"></param>
236         /// <param name="LoadBalancerName"></param>
237         private bool ExistLoadBalancer(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
238         {
239             bool result = false;
240             DescribeLoadBalancersResponse response = client.DescribeLoadBalancers();
241             List<LoadBalancerDescription> loadBalancerDescriptionList = response.LoadBalancerDescriptions;
242             foreach (LoadBalancerDescription loadBalancerDescription in loadBalancerDescriptionList)
243             {
244                 if (loadBalancerDescription.LoadBalancerName == LoadBalancerName)
245                 {
246                     result = true;
247                 }
248             }
249             return result;
250         }
251         #endregion
252         #region 得到负载均衡器DNS名称
253         /// <summary>
254         /// 得到负载均衡器DNS名称
255         /// </summary>
256         /// <param name="client"></param>
257         /// <param name="LoadBalancerName"></param>
258         /// <returns></returns>
259         private string GetLoadBalancerDescription(AmazonElasticLoadBalancingClient client, string LoadBalancerName)
260         {
261             string DNSName = "";
262             DescribeLoadBalancersResponse response = client.DescribeLoadBalancers();
263             List<LoadBalancerDescription> loadBalancerDescriptionList = response.LoadBalancerDescriptions;
264             foreach (LoadBalancerDescription loadBalancerDescription in loadBalancerDescriptionList)
265             {
266                 if (loadBalancerDescription.LoadBalancerName == LoadBalancerName)
267                 {
268                     DNSName = loadBalancerDescription.DNSName;
269                 }
270             }
271             return DNSName;
272         }
273         #endregion
274 
275     }

 

posted on 2016-06-23 14:23  流编程风  阅读(1540)  评论(0编辑  收藏  举报