[关闭]
@xtccc 2017-03-05T21:55:33.000000Z 字数 1585 阅读 1852

Elastic Load Balancer

给我写信
GitHub

此处输入图片的描述


AWS



1. 将Instances加入到某个Load Balancer


  1. import com.amazonaws.auth.profile.ProfileCredentialsProvider
  2. import com.amazonaws.regions.{Region, Regions}
  3. import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient
  4. import com.amazonaws.services.elasticloadbalancing.model.{Instance, RegisterInstancesWithLoadBalancerRequest}
  5. import scala.collection.JavaConverters._
  6. /**
  7. * Created by tao on 3/5/17.
  8. */
  9. object RegisterInstances {
  10. def main(args: Array[String]): Unit = {
  11. val region = Regions.US_WEST_2.getName
  12. val instanceIDs = List("i-036707a13d12dd2bc", "i-0ab71d63e08b5afe8")
  13. val lbName = "tao-load-balancer"
  14. RegisterInstancesToLoadBalancer(region, instanceIDs, lbName)
  15. }
  16. /**
  17. * 把若干个给定的instance加入到一个已有的load balancer中
  18. * 前提是这个load balancer已经配置好了(例如listner已经配置好),且正在运行
  19. *
  20. * 即使提供的instance已经在load balancer中了, 重复注册也没关系
  21. * */
  22. private def RegisterInstancesToLoadBalancer(regionName: String, instanceIds: List[String], loadBalancerName: String): Unit = {
  23. val credentials = new ProfileCredentialsProvider()
  24. val elbClient = new AmazonElasticLoadBalancingClient(credentials)
  25. /** 必须为 elbClient 指定正确的region, 否则后面会报: no active load balancer */
  26. elbClient.withRegion(Region.getRegion(Regions.fromName(regionName)))
  27. // String(instance id) => com.amazonaws.services.elasticloadbalancing.model.Instance(instance)
  28. val instanceList = instanceIds.map(id => {
  29. val instance = new Instance()
  30. instance.setInstanceId(id)
  31. instance
  32. }).asJavaCollection
  33. val elbRegiReq = new RegisterInstancesWithLoadBalancerRequest()
  34. elbRegiReq.setLoadBalancerName(loadBalancerName)
  35. elbRegiReq.setInstances(instanceList)
  36. val result = elbClient.registerInstancesWithLoadBalancer(elbRegiReq)
  37. println(s"result = \n$result")
  38. }
  39. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注