百度智能云

All Product Document

          Load Balance

          Common blb Instance

          Create blb Instance

          By the following codes, you can create an common LoadBalancer and return the distributed service address and instance ID.

          args := &blb.CreateLoadBalancerArgs{ 
              // Set instance name 
              Name:        "sdkBlb", 
              // Set instance description 
              Description: "sdk create", 
              // Set the vpc to which the instance belongs 
              VpcId:       vpcId, 
              // Set the subnet to which the instance belongs 
              SubnetId:    subnetId, 
          } 
          
          // To set a tag for the instance, you can set the tag by the following code and can't modify or delete the set tag. 
          args.Tags = []model.TagModel{ 
              { 
                  TagKey:   "key", 
                  TagValue: "value", 
              }, 
          } 
          result, err := client.CreateLoadBalancer(args) 
          if err != nil { 
              fmt.Println("create blb failed:", err) 
          } else { 
              fmt.Println("create blb success: ", result) 
          } 

          Hint

          Update blb Instance

          By the following codes, you can update the configuration information of a LoadBalancer, such as the instance name and description.

          args := &blb.UpdateLoadBalancerArgs{ 
              Name:        "testSdk", 
              Description: "test desc", 
          } 
          err := client.UpdateLoadBalancer(blbId, args) 
          if err != nil { 
              fmt.Println("update blb failed:", err) 
          } else { 
              fmt.Println("update blb success") 
          } 

          Hint

          Query a List of blb Instances

          By the following codes, you can query all the information of LoadBalancer under the user account.

          args := &blb.DescribeLoadBalancersArgs{} 
          
          // Support query by LoadBalancer id, name and address. Matching rules support partial inclusion (regular is not supported) 
          args.BlbId = blbId 
          args.Name = blbName 
          args.Address = blbAddress 
          args.ExactlyMatch = true 
          
          // Support to find the bound LoadBalancer of the BLB specified by the blbId parameter 
          args.BlbId = blbId 
          
          result, err := client.DescribeLoadBalancers(args) 
          if err != nil { 
              fmt.Println("list all blb failed:", err) 
          } else { 
              fmt.Println("list all blb success: ", result) 
          } 

          Hint

          Query blb Instance Details

          By the following codes, you can query the detailed information of the specific common LoadBalancer under the user account according to id, including all the listener port information of LoadBalancer.

          result, err := client.DescribeLoadBalancerDetail(blbId) 
          if err != nil { 
              fmt.Println("get blb detail failed:", err) 
          } else { 
              fmt.Println("get blb detail success: ", result) 
          } 

          Hint

          Release blb Instance

          By the following codes, you can remove the specified LoadBalancer and the removed LoadBalancer can't be retrieved.

          err := client.DeleteLoadBalancer(blbId) 
          if err != nil { 
              fmt.Println("delete blb failed:", err) 
          } else { 
              fmt.Println("delete blb success") 
          } 

          Hint

          Add common BLB back end servers

          By the following codes, bind a back end server under the specified common BLB.

          args := &blb.AddBackendServersArgs{ 
              // Configure the list and weight of back end server 
              BackendServerList: []blb.BackendServerModel{  
                  {InstanceId: instanceId, Weight: 100}, 
              }, 
          } 
          err := client.AddBackendServers(blbId, args) 
          if err != nil { 
              fmt.Println("add backend servers failed:", err) 
          } else { 
              fmt.Println("add backend servers success: ") 
          } 

          Hint

          Update the back end server weight

          By the following codes, update the information under the specified blb.

          args := &blb.UpdateBackendServersArgs{ 
              // Configure the list and weight of back end server 
              BackendServerList: []blb.BackendServerModel{  
                  {InstanceId: instanceId, Weight: 30}, 
              }, 
          } 
          err := client.UpdateBackendServers(blbId, args) 
          if err != nil { 
              fmt.Println("update backend servers failed:", err) 
          } else { 
              fmt.Println("update backend servers success: ") 
          } 

          Hint

          Query the information of back end server list

          Through the following code, query information about all server under the specified LoadBalancer

          args := &blb.DescribeBackendServersArgs{ 
          	
          } 
          result, err := client.DescribeBackendServers(blbId, args) 
          if err != nil { 
              fmt.Println("describe backend servers failed:", err) 
          } else { 
              fmt.Println("describe backend servers success: ", result) 
          } 

          Hint

          Query the health status of back end server

          By the following codes, query the health status information of back end server under the specified monitoring port.

          args := &blb.DescribeHealthStatusArgs{ 
          	 ListenerPort: 80, 
          } 
          result, err := client.DescribeHealthStatus(blbId, args) 
          if err != nil { 
              fmt.Println("describe health status failed:", err) 
          } else { 
              fmt.Println("describe health status success: ", result) 
          } 

          Hint

          Remove the back end server

          By the following codes, remove the back end server.

          args := &blb.RemoveBackendServersArgs{ 
              // List of instances to be removed from the back end server list 
              BackendServerList: []string{instanceId}, 
          } 
          err := client.RemoveBackendServers(blbId, args) 
          if err != nil { 
              fmt.Println("remove backend servers failed:", err) 
          } else { 
              fmt.Println("remove backend servers success: ") 
          } 

          Hint

          Listener management

          Create TCP listener

          Through the following code, under the specified LoadBalancer, create an common blb listener based on the TCP protocol to listen to a frontend port and forward all TCP traffic sent to that port according to the policy.

          args := &blb.CreateTCPListenerArgs{ 
              // The monitoring port of listener should be within 1-65535. 
              ListenerPort: 80, 
              // The monitoring port of back end server should be within 1-65535. 
              BackendPort: 80, 
              // Load balance algorithm, support RoundRobin/LeastConnection/Hash 
              Scheduler: "RoundRobin", 
              // TCP sets the link timeout time as 900 seconds by default. It should be an integer between 10-4000. 
              TcpSessionTimeout: 1000, 
          } 
          err := client.CreateTCPListener(BLBID, args) 
          if err != nil { 
              fmt.Println("create TCP Listener failed:", err) 
          } else { 
              fmt.Println("create TCP Listener success") 
          } 

          Hint

          Update the TCP listener

          Through the following code, update the TCP listener parameters under the specified LoadBalancer All the domains specified in the request parameters will be updated, the unspecified domains will remain unchanged, and the listener will be specified through the port.

          args := &blb.UpdateTCPListenerArgs{ 
              // The listener port to be updated 
              ListenerPort: 80, 
              // Update the algorithm of load balance 
              Scheduler:    "Hash", 
              // Update the timeout time of tcp link 
              TcpSessionTimeout: 2000, 
          } 
          err := client.UpdateTCPListener(BLBID, args) 
          if err != nil { 
              fmt.Println("update TCP Listener failed:", err) 
          } else { 
              fmt.Println("update TCP Listener success") 
          } 

          Hint

          Query the TCP listener

          By the following codes, query the information of all TCP listeners under the specified LoadBalancer. The matching query according to the listener port is supported. The result supports marker paging. The paging size is 1000 by default and can be specified by the maxKeys parameter.

          args := &blb.DescribeListenerArgs{ 
              // The listener port to query 
              ListenerPort: 80, 
          } 
          result, err := client.DescribeTCPListeners(BLBID, args) 
          if err != nil { 
              fmt.Println("describe TCP Listener failed:", err) 
          } else { 
              fmt.Println("describe TCP Listener success: ", result) 
          } 

          Hint

          Create UDP listener

          Through the following code, under the specified LoadBalancer, create an listener based on the UDP protocol to listen to a frontend port and forward all UDP traffic sent to that port according to the policy.

          args := &blb.CreateUDPListenerArgs{ 
              // The monitoring port of listener should be within 1-65535. 
              ListenerPort: 53, 
              // The monitoring port of back end server should be within 1-65535. 
              BackendPort: 53, 
              // Load balance algorithm, support RoundRobin/LeastConnection/Hash 
              Scheduler:    "RoundRobin", 
              // Health check character string. Health sent request character string, the back end server should respond after receiving the request character string. Support hexadecimal\00-\FF and standard ASCII character string, maximum length of 1299. 
              HealthCheckString: "\00\01\01\00\00\01\00\00\00\00\00\00\05baidu\03com\00\00\01\00\01" 
          } 
          err := client.CreateUDPListener(BLBID, args) 
          if err != nil { 
              fmt.Println("create UDP Listener failed:", err) 
          } else { 
              fmt.Println("create UDP Listener success") 
          } 

          Hint

          Update the UDP listener

          Through the following code, update the UDP listener parameters under the specified LoadBalancer All the domains specified in the request parameters will be updated, the unspecified domains will remain unchanged, and the listener will be specified through the port.

          args := &blb.UpdateUDPListenerArgs{ 
              // The listener port to be updated 
              ListenerPort: 53, 
              // Update the algorithm of load balance 
              Scheduler:    "Hash", 
          } 
          err := client.UpdateUDPListener(BLBID, args) 
          if err != nil { 
              fmt.Println("update UDP Listener failed:", err) 
          } else { 
              fmt.Println("update UDP Listener success") 
          } 

          Hint

          Query the UDP listener

          By the following codes, query the information of all UDP listeners under the specified LoadBalancer. The matching query according to the listener port is supported. The result supports marker paging. The paging size is 1000 by default and can be specified by the maxKeys parameter.

          args := &blb.DescribeListenerArgs{ 
              // The listener port to query 
              ListenerPort: 53, 
          } 
          result, err := client.DescribeUDPListeners(BLBID, args) 
          if err != nil { 
              fmt.Println("describe UDP Listener failed:", err) 
          } else { 
              fmt.Println("describe UDP Listener success: ", result) 
          } 

          Hint

          Create HTTP listener

          Execute the following codes, under the specified LoadBalancer, create an listener based on the HTTP protocol to listen to a frontend port and forward all HTTP requests sent to this port to the backend port monitored by the backend server according to the policy.

          args := &blb.CreateHTTPListenerArgs{ 
              // The monitoring port of listener should be within 1-65535. 
              ListenerPort: 80, 
              // The monitoring port of back end server should be within 1-65535. 
              BackendPort: 80, 
              // Load balance algorithm, support RoundRobin/LeastConnection 
              Scheduler:    "RoundRobin", 
          } 
          err := client.CreateHTTPListener(BLBID, args) 
          if err != nil { 
              fmt.Println("create HTTP Listener failed:", err) 
          } else { 
              fmt.Println("create HTTP Listener success") 
          } 

          Hint

          Update the HTTP listener

          Through the following code, update the HTTP listener parameters under the specified LoadBalancer All the domains specified in the request parameters will be updated, the unspecified domains will remain unchanged, and the listener will be specified through the port.

          args := &blb.UpdateHTTPListenerArgs{ 
          	 // The listener port to be updated 
              ListenerPort: 80, 
              // Update the algorithm of load balance 
              Scheduler:    "LeastConnection", 
              // Enable the session persistence feature 
              KeepSession:  true, 
          } 
          err := client.UpdateHTTPListener(BLBID, args) 
          if err != nil { 
              fmt.Println("update HTTP Listener failed:", err) 
          } else { 
              fmt.Println("update HTTP Listener success") 
          } 

          Hint

          • For the detailed parameter configuration and limit conditions, you can refer to the BLB API document Update HTTP Listener.

          Query the HTTP listener

          By the following codes, query the information of all HTTP listeners under the specified LoadBalancer. The matching query according to the listener port is supported. The result supports marker paging. The paging size is 1000 by default and can be specified by the maxKeys parameter.

          args := &blb.DescribeListenerArgs{ 
              // The listener port to query 
              ListenerPort: 80, 
          } 
          result, err := client.DescribeHTTPListeners(BLBID, args) 
          if err != nil { 
              fmt.Println("describe HTTP Listener failed:", err) 
          } else { 
              fmt.Println("describe HTTP Listener success: ", result) 
          } 

          Hint

          Create HTTPS listener

          Execute the following codes, under the specified LoadBalancer, to create an listener based on the HTTPS protocol, listening to a frontend port, and converting all HTTPS requests sent to this port through SSL offload to HTTP requests before forwarding them to the backend port for monitoring the backend server according to the policy.

          args := &blb.CreateHTTPSListenerArgs{ 
              // The monitoring port of listener should be within 1-65535. 
              ListenerPort: 443, 
              // The monitoring port of back end server should be within 1-65535. 
              BackendPort: 80, 
              // Load balance algorithm, support RoundRobin/LeastConnection 
              Scheduler:    "RoundRobin", 
              // Configure the certificate list 
              CertIds:      []string{certId}, 
          } 
          err := client.CreateHTTPSListener(BLBID, args) 
          if err != nil { 
              fmt.Println("create HTTPS Listener failed:", err) 
          } else { 
              fmt.Println("create HTTPS Listener success") 
          } 

          Hint

          Update the HTTPS listener

          Through the following code, update the HTTPS listener parameters under the specified LoadBalancer All the domains specified in the request parameters will be updated, the unspecified domains will remain unchanged, and the listener will be specified through the port.

          args := &blb.UpdateHTTPSListenerArgs{ 
          	 // The listener port to be updated 
              ListenerPort: 443, 
              // Update the algorithm of load balance 
              Scheduler:    "LeastConnection", 
              // Configure the certificate list 
              CertIds:      []string{certId}, 
          } 
          err := client.UpdateHTTPSListener(BLBID, args) 
          if err != nil { 
              fmt.Println("update HTTPS Listener failed:", err) 
          } else { 
              fmt.Println("update HTTPS Listener success") 
          } 

          Hint

          Query the HTTPS listener

          By the following codes, query the information of all LoadBalancer listeners under the specified LoadBalancer. The matching query according to the listener port is supported. The result supports marker paging. The paging size is 1000 by default and can be specified by the maxKeys parameter.

          args := &blb.DescribeListenerArgs{ 
              // The listener port to query 
              ListenerPort: 443, 
          } 
          result, err := client.DescribeHTTPSListeners(BLBID, args) 
          if err != nil { 
              fmt.Println("describe HTTPS Listener failed:", err) 
          } else { 
              fmt.Println("describe HTTPS Listener success: ", result) 
          } 

          Hint

          Create SSL listener

          Through the following code, under the specified LoadBalancer, create an blb listener based on the SSL protocol to listen to a frontend port and forward all SSL traffic sent to that port according to the policy.

          args := &blb.CreateSSLListenerArgs{ 
              // The monitoring port of listener should be within 1-65535. 
              ListenerPort: 443, 
              // The monitoring port of back end server should be within 1-65535. 
              BackendPort: 80, 
              // Load balance algorithm, support RoundRobin/LeastConnection/Hash 
              Scheduler:    "RoundRobin", 
              // Configure the certificate list 
              CertIds:      []string{certId}, 
          } 
          err := client.CreateSSLListener(BLBID, args) 
          if err != nil { 
              fmt.Println("create SSL Listener failed:", err) 
          } else { 
              fmt.Println("create SSL Listener success") 
          } 

          Hint

          Update the SSL listener

          Through the following code, update the SSL listener parameters under the specified LoadBalancer All the domains specified in the request parameters will be updated, the unspecified domains will remain unchanged, and the listener will be specified through the port.

          args := &blb.UpdateSSLListenerArgs{ 
          	 // The listener port to be updated 
              ListenerPort: 443, 
              // Update the algorithm of load balance 
              Scheduler:    "LeastConnection", 
              // Configure the certificate list 
              CertIds:      []string{certId}, 
          } 
          err := client.UpdateSSLListener(BLBID, args) 
          if err != nil { 
              fmt.Println("update SSL Listener failed:", err) 
          } else { 
              fmt.Println("update SSL Listener success") 
          } 

          Hint

          Query the SSL listener

          By the following codes, query the information of all SSL listeners under the specified LoadBalancer. The matching query according to the listener port is supported. The result supports marker paging. The paging size is 1000 by default and can be specified by the maxKeys parameter.

          args := &blb.DescribeListenerArgs{ 
              // The listener port to query 
              ListenerPort: 443, 
          } 
          result, err := client.DescribeSSLListeners(BLBID, args) 
          if err != nil { 
              fmt.Println("describe SSL Listener failed:", err) 
          } else { 
              fmt.Println("describe SSL Listener success: ", result) 
          } 

          Hint

          Delete listener

          By the following codes, remove the listener under the specified LoadBalancer. The listener is specified by the monitoring port. The batch remove is supported.

          args := &blb.DeleteListenersArgs{ 
              ClientToken: "be31b98c-5e41-4838-9830-9be700de5a20", 
              // Monitoring port of listener to be deleted 
              PortList:    []uint16{80, 443}, 
          } 
          err := client.DeleteListeners(BLBID, args) 
          if err != nil { 
              fmt.Println("delete Listeners failed:", err) 
          } else { 
              fmt.Println("delete Listeners success: ") 
          } 

          Hint

          Error handling

          GO language marks error with error type, and BLB supports 2 errors in the table below:

          Error type Description
          BceClientError Error arising from user operation
          BceServiceError Error returned by BLB service

          You call BLB related interface with SDK, in addition to the results required, error is returned, and users can get relevant errors for handling. Instance is as follows”

          // blbClient is a created BLB Client object. 
          blbDetail, err := blbClient.DescribeLoadBalancerDetail(blbId) 
          if err != nil { 
          	 switch realErr := err.(type) { 
          	 case *bce.BceClientError: 
          	 	 fmt.Println("client occurs error:", realErr.Error()) 
          	 case *bce.BceServiceError: 
          	 	 fmt.Println("service occurs error:", realErr.Error()) 
          	 default: 
          	 	 fmt.Println("unknown error:", err) 
          	 } 
          } else { 
          	 fmt.Println("get blb detail success: ", blbDetail) 
          } 
          Previous
          Initialization
          Next
          Application blb Instance