| 
					
				 | 
			
			
				@@ -10,6 +10,7 @@ from aliyunsdkcore import client 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from aliyunsdkcore.client import AcsClient 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from aliyunsdkcore.acs_exception.exceptions import ClientException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from aliyunsdkcore.acs_exception.exceptions import ServerException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from aliyunsdkcore.request import CommonRequest 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from aliyunsdkecs.request.v20140526.CreateInstanceRequest import CreateInstanceRequest 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from aliyunsdkecs.request.v20140526.StartInstanceRequest import StartInstanceRequest 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -43,7 +44,7 @@ key_pair_name = "stuuudy" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #slb_id = "lb-bp1r9duz4k7z0riz9zs3n" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #vlogapi 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 slb_id_2 = "lb-bp1qk9mkvjtundlzz7owm" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-slb_id_3 = "lb-bp1akia6vlguiwg4znaq8" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+# slb_id_3 = "lb-bp1akia6vlguiwg4znaq8" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 slb_id_4 = "lb-bp1pj2v06ladvgftgxcp0" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 slb_id_5 = "lb-bp1y63rnrb2e64whryghz" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -56,7 +57,7 @@ def addBackendServers(slb_id,instance_id): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ipaddr = getIpaddr(instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     request.set_LoadBalancerId(slb_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    request.set_BackendServers([{"ServerId": instance_id, "Weight": "100", "Type": "ecs", "ServerIp": ipaddr}]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.set_BackendServers([{"ServerId": instance_id, "Weight": "0", "Type": "ecs", "ServerIp": ipaddr}]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     response = client.do_action_with_exception(request) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     logging.info(response) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -81,12 +82,35 @@ def healthCheck(instance_id): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   time.sleep(300) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #  addBackendServers(slb_id_1,instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   addBackendServers(slb_id_2,instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  addBackendServers(slb_id_3,instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  # addBackendServers(slb_id_3,instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   addBackendServers(slb_id_4,instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   addBackendServers(slb_id_5,instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  for i in range(1, 6): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      time.sleep(5) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      weight = i * 20 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      logging.info(f"instance_id: {instance_id}, weight: {weight}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      setWeight(slb_id=slb_id_2, instance_id=instance_id, weight=weight) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      setWeight(slb_id=slb_id_4, instance_id=instance_id, weight=weight) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      setWeight(slb_id=slb_id_5, instance_id=instance_id, weight=weight) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+#设置权重 instance_id :服务器id,weight:权重值 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def setWeight(slb_id,instance_id ,weight): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    BackendServers = [{"ServerId": instance_id, "Weight": weight}] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request = CommonRequest() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.set_accept_format ('json') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.set_domain ('slb.aliyuncs.com') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.set_version ('2014-05-15') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.set_method ('POST') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.set_action_name ('SetBackendServers') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.add_query_param ('BackendServers', BackendServers) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    request.add_query_param ('LoadBalancerId', slb_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        response = clt.do_action (request) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    except Exception as e: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        print (e) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 # 创建ECS实例并启动。 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 def create_multiple_instances(): 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -139,7 +163,7 @@ def build_request(): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     request.set_Tags([ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         "Key": "ecs", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        "Value": "vlogapi.prod" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        "Value": "longvideoapi.prod" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       }  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     return request 
			 |