Baidu AI Cloud
中国站

百度智能云

Load Balance

Application blb Instance

Instance Management

Create instance

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

args := &appblb.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 appblb failed:", err) 
} else { 
    fmt.Println("create appblb success: ", result) 
} 

Update example

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

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

Query existing instances

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

args := &appblb.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 appblb failed:", err) 
} else { 
    fmt.Println("list all appblb success: ", result) 
} 

Query instance details

By the following codes, you can query the detailed information of the specific application type 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 appblb detail failed:", err) 
} else { 
    fmt.Println("get appblb detail success: ", result) 
} 

Release the 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 appblb failed:", err) 
} else { 
    fmt.Println("delete appblb success") 
} 

Server Group Management

Create an application type server group

Through the following code, Under the specified application BLB, create a server group to bind the backend server and open the corresponding port for the listener.

args := &appblb.CreateAppServerGroupArgs{ 
    // Set server group name 
    Name: "sdkTest", 
    // Set server group description 
    Description: "sdk test desc", 
} 

// If you want to bind the back end server directly, you can set the following parameters. 
args.BackendServerList = []appblb.AppBackendServer{ 
    { 
        InstanceId: instanceId, 
        Weight:     50, 
    }, 
} 

result, err := client.CreateAppServerGroup(blbId, args) 
if err != nil { 
    fmt.Println("create server group failed:", err) 
} else { 
    fmt.Println("create server group success: ", result) 
} 

Update the server group

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 := &appblb.UpdateAppServerGroupArgs{ 
    // Set the server group ID to be updated 
    SgId:        serverGroupId, 
    // Set new server group name 
    Name:        "testSdk", 
    // Set new server group description 
    Description: "test desc", 
} 
err := client.UpdateAppServerGroup(blbId, args) 
if err != nil { 
    fmt.Println("update server group failed:", err) 
} else { 
    fmt.Println("update server group success: ", result) 
} 

Query the server group

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

args := &appblb.DescribeAppServerGroupArgs{} 

// Conduct global query according to BLBID and name 
args.BlbId = blbId 
args.Name = servergroupName 
args.ExactlyMatch = true 

result, err := client.DescribeAppServerGroup(blbId, args) 
if err != nil { 
    fmt.Println("describe server group failed:", err) 
} else { 
    fmt.Println("describe server group success: ", result) 
} 

Delete a server group

By the following codes, delete the server group. By the server group id, specify.

args := &appblb.DeleteAppServerGroupArgs{ 
    // Server group ID to delete 
    SgId: serverGroupId, 
} 
err := client.DeleteAppServerGroup(blbId, args) 
if err != nil { 
    fmt.Println("delete server group failed:", err) 
} else { 
    fmt.Println("delete server group success: ", result) 
} 

CreateAppServerGroupPort

Through the following code, under the specified application BLB, create a server group backend port to distribute all traffic sent to the port to the servers in the corresponding server list bound by weight.

args := &appblb.CreateAppServerGroupPortArgs{ 
    // Server group ID to which the port belongs 
    SgId: serverGroupId, 
    // Listener port number 
    Port: 80, 
    // Listener protocol type 
    Type: "TCP", 
} 

// You can select to set the parameters of the corresponding health check protocols. 
args.HealthCheck = "TCP" 
args.HealthCheckPort = 30 
args.HealthCheckIntervalInSecond = 10 
args.HealthCheckTimeoutInSecond = 10 

result, err := client.CreateAppServerGroupPort(blbId, args) 
if err != nil { 
    fmt.Println("create server group port failed:", err) 
} else { 
    fmt.Println("create server group port success: ", result) 
} 

Hint

Update the server group port

By the following codes, update the server group port according to id.

args := &appblb.UpdateAppServerGroupPortArgs{ 
	 // Server group ID to which the port belongs 
    SgId: serverGroupId, 
    // Port Id, returned by the created application type server group port 
    PortId: portId, 
    // Set the health check protocol parameters 
    HealthCheck:                 "TCP", 
    HealthCheckPort:             30, 
    HealthCheckIntervalInSecond: 10, 
    HealthCheckTimeoutInSecond:  10, 
} 
err := client.UpdateAppServerGroupPort(blbId, args) 
if err != nil { 
    fmt.Println("update server group port failed:", err) 
} else { 
    fmt.Println("update server group port success: ", result) 
} 

Hint

  • For the parameters to configure the health check protocols, please refer to the BLB API document Update Server Group Port for details.

Delete a server group port

By the following codes, delete the server group port. By the server group id, specify

args := &appblb.DeleteAppServerGroupPortArgs{ 
	 // Server group ID to which the port belongs 
    SgId:        serverGroupId, 
	 / / port service ID list to be deleted 
    PortIdList:  []string{portId}, 
} 
err := client.DeleteAppServerGroupPort(blbId, args) 
if err != nil { 
    fmt.Println("delete server group port failed:", err) 
} else { 
    fmt.Println("delete server group port success: ", result) 
} 

Add the application type BLB back end RS

Through the following code, bind backend server RS under specified application BLB and server group

args := &appblb.CreateBlbRsArgs{ 
    BlbRsWriteOpArgs: appblb.BlbRsWriteOpArgs{ 
        // ID of the server group to which the RS belongs 
        SgId:        serverGroupId, 
        // Configure the list and weight of back end server 
        BackendServerList: []appblb.AppBackendServer{  
            {InstanceId: instanceId, Weight: 30}, 
        }, 
    }, 
} 
err := client.CreateBlbRs(blbId, args) 
if err != nil { 
    fmt.Println("create blbRs failed:", err) 
} else { 
    fmt.Println("create blbRs success: ", result) 
} 

Update the RS weight mounted under the server group

By the following codes, update the RS information under the specified server group.

args := &appblb.UpdateBlbRsArgs{ 
    BlbRsWriteOpArgs: appblb.BlbRsWriteOpArgs{ 
        // ID of the server group to which the RS belongs 
        SgId:        serverGroupId, 
        // Configure the list and weight of back end server to be updated. 
        BackendServerList: []appblb.AppBackendServer{ 
            {InstanceId: instanceId, Weight: 60}, 
        }, 
    }, 
} 
err := client.UpdateBlbRs(blbId, args) 
if err != nil { 
    fmt.Println("update blbRs failed:", err) 
} else { 
    fmt.Println("update blbRs success: ", result) 
} 

Query the RS list information under the server group

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

args := &appblb.DescribeBlbRsArgs{ 
	 // ID of the server group to which the RS belongs 
    SgId: serverGroupId, 
} 
result, err := client.DescribeBlbRs(blbId, args) 
if err != nil { 
    fmt.Println("describe blbRs failed:", err) 
} else { 
    fmt.Println("describe blbRs success: ", result) 
} 

Delete the rs mounted under the server group

By the following codes, delete the server group. By the server group id, specify

args := &appblb.DeleteBlbRsArgs{ 
	 // ID of the server group to which the RS belongs 
    SgId: serverGroupId, 
    // List of instances to be deleted from the RS list 
    BackendServerIdList: []string{instanceId}, 
} 
err := client.DeleteBlbRs(blbId, args) 
if err != nil { 
    fmt.Println("delete blbRs failed:", err) 
} else { 
    fmt.Println("delete blbRs success: ", result) 
} 

Query the server bound to the server group

By the following codes, query the server bound under the server group

result, err := client.DescribeRsMount(blbId, serverGroupId) 
if err != nil { 
    fmt.Println("describe mount Rs list failed:", err) 
} else { 
    fmt.Println("describe mount Rs list success: ", result) 
} 

Query the RS unbound to the server group

By the following codes, query the RS unbound under the server group

result, err := client.DescribeRsUnMount(blbId, serverGroupId) 
if err != nil { 
    fmt.Println("describe unmount Rs list failed:", err) 
} else { 
    fmt.Println("describe unmount Rs list success: ", result) 
} 

Listener Management

Create TCP listener

Through the following code, under the specified LoadBalancer, create an application 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 := &appblb.CreateAppTCPListenerArgs{ 
    // The monitoring port of listener should be within 1-65535. 
    ListenerPort: 90, 
    // 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.CreateAppTCPListener(BLBID, args) 
if err != nil { 
    fmt.Println("create TCP Listener failed:", err) 
} else { 
    fmt.Println("create TCP Listener success") 
} 

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 := &appblb.UpdateAppTCPListenerArgs{ 
    UpdateAppListenerArgs: appblb.UpdateAppListenerArgs{ 
        // The listener port to be updated 
        ListenerPort: 90, 
        // Update the algorithm of load balance 
        Scheduler:    "Hash", 
        // Update the timeout time of tcp link 
        TcpSessionTimeout: 2000, 
    }, 
} 
err := client.UpdateAppTCPListener(BLBID, args) 
if err != nil { 
    fmt.Println("update TCP Listener failed:", err) 
} else { 
    fmt.Println("update TCP Listener success") 
} 

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 := &appblb.DescribeAppListenerArgs{ 
    // The listener port to query 
    ListenerPort: 90, 
} 
result, err := client.DescribeAppTCPListeners(BLBID, args) 
if err != nil { 
    fmt.Println("describe TCP Listener failed:", err) 
} else { 
    fmt.Println("describe TCP Listener success: ", result) 
} 

Create UDP listener

Through the following code, under the specified LoadBalancer, create an application 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 := &appblb.CreateAppUDPListenerArgs{ 
    // The monitoring port of listener should be within 1-65535. 
    ListenerPort: 80, 
    // Load balance algorithm, support RoundRobin/LeastConnection/Hash 
    Scheduler:    "RoundRobin", 
} 
err := client.CreateAppUDPListener(BLBID, args) 
if err != nil { 
    fmt.Println("create UDP Listener failed:", err) 
} else { 
    fmt.Println("create UDP Listener success") 
} 

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 := &appblb.UpdateAppUDPListenerArgs{ 
    UpdateAppListenerArgs: appblb.UpdateAppListenerArgs{ 
        // The listener port to be updated 
        ListenerPort: 80, 
        // Update the algorithm of load balance 
        Scheduler:    "Hash", 
    }, 
} 
err := client.UpdateAppUDPListener(BLBID, args) 
if err != nil { 
    fmt.Println("update UDP Listener failed:", err) 
} else { 
    fmt.Println("update UDP Listener success") 
} 

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 := &appblb.DescribeAppListenerArgs{ 
    // The listener port to query 
    ListenerPort: 80, 
} 
result, err := client.DescribeAppUDPListeners(BLBID, args) 
if err != nil { 
    fmt.Println("describe UDP Listener failed:", err) 
} else { 
    fmt.Println("describe UDP Listener success: ", result) 
} 

Create HTTP listener

Execute the following codes, under the specified LoadBalancer, create an application 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 := &appblb.CreateAppHTTPListenerArgs{ 
    // The monitoring port of listener should be within 1-65535. 
    ListenerPort: 80, 
    // Load balance algorithm, support RoundRobin/LeastConnection 
    Scheduler:    "RoundRobin", 
} 
err := client.CreateAppHTTPListener(BLBID, args) 
if err != nil { 
    fmt.Println("create HTTP Listener failed:", err) 
} else { 
    fmt.Println("create HTTP Listener success") 
} 

Hint

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

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 := &appblb.UpdateAppHTTPListenerArgs{ 
	 // 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.UpdateAppHTTPListener(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 := &appblb.DescribeAppListenerArgs{ 
    // The listener port to query 
    ListenerPort: 80, 
} 
result, err := client.DescribeAppHTTPListeners(BLBID, args) 
if err != nil { 
    fmt.Println("describe HTTP Listener failed:", err) 
} else { 
    fmt.Println("describe HTTP Listener success: ", result) 
} 

Create HTTPS listener

Execute the following codes, under the specified LoadBalancer, to create an application 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 := &appblb.CreateAppHTTPSListenerArgs{ 
    // The monitoring port of listener should be within 1-65535. 
    ListenerPort: 80, 
    // Load balance algorithm, support RoundRobin/LeastConnection 
    Scheduler:    "RoundRobin", 
    // Configure the certificate list 
    CertIds:      []string{certId}, 
} 
err := client.CreateAppHTTPSListener(BLBID, args) 
if err != nil { 
    fmt.Println("create HTTPS Listener failed:", err) 
} else { 
    fmt.Println("create HTTPS Listener success") 
} 

Hint

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

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 := &appblb.UpdateAppHTTPSListenerArgs{ 
	 // The listener port to be updated 
    ListenerPort: 80, 
    // Update the algorithm of load balance 
    Scheduler:    "LeastConnection", 
    // Enable the session persistence feature 
    KeepSession:  true, 
    // Configure the certificate list 
    CertIds:      []string{certId}, 
} 
err := client.UpdateAppHTTPSListener(BLBID, args) 
if err != nil { 
    fmt.Println("update HTTPS Listener failed:", err) 
} else { 
    fmt.Println("update HTTPS Listener success") 
} 

Hint

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

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 := &appblb.DescribeAppListenerArgs{ 
    // The listener port to query 
    ListenerPort: 80, 
} 
result, err := client.DescribeAppHTTPSListeners(BLBID, args) 
if err != nil { 
    fmt.Println("describe HTTPS Listener failed:", err) 
} else { 
    fmt.Println("describe HTTPS Listener success: ", result) 
} 

Create SSL listener

Through the following code, under the specified LoadBalancer, create an application 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 := &appblb.CreateAppSSLListenerArgs{ 
    // The monitoring port of listener should be within 1-65535. 
    ListenerPort: 80, 
    // Load balance algorithm, support RoundRobin/LeastConnection/Hash 
    Scheduler:    "RoundRobin", 
    // Configure the certificate list 
    CertIds:      []string{certId}, 
} 
err := client.CreateAppSSLListener(BLBID, args) 
if err != nil { 
    fmt.Println("create SSL Listener failed:", err) 
} else { 
    fmt.Println("create SSL Listener success") 
} 

Hint

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

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 := &appblb.UpdateAppSSLListenerArgs{ 
	 // The listener port to be updated 
    ListenerPort: 80, 
    // Update the algorithm of load balance 
    Scheduler:    "LeastConnection", 
    // Configure the certificate list 
    CertIds:      []string{certId}, 
} 
err := client.UpdateAppSSLListener(BLBID, args) 
if err != nil { 
    fmt.Println("update SSL Listener failed:", err) 
} else { 
    fmt.Println("update SSL Listener success") 
} 

Hint

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

Query the SSL 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 := &appblb.DescribeAppListenerArgs{ 
    // The listener port to query 
    ListenerPort: 80, 
} 
result, err := client.DescribeAppSSLListeners(BLBID, args) 
if err != nil { 
    fmt.Println("describe SSL Listener failed:", err) 
} else { 
    fmt.Println("describe SSL Listener success: ", result) 
} 

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 := &appblb.DeleteAppListenersArgs{ 
    // Monitoring port of listener to be deleted 
    PortList:    []uint16{80}, 
} 
err := client.DeleteAppListeners(BLBID, args) 
if err != nil { 
    fmt.Println("delete Listeners failed:", err) 
} else { 
    fmt.Println("delete Listeners success: ", result) 
} 

Create policy

By the following codes, create a policy under the specified application type BLB listener port.

args := &appblb.CreatePolicysArgs{ 
    // Monitoring port of listener of policy to be bound 
    ListenerPort: 80, 
    // For the policies to be bound, TCP/UDP/SSL only supports binding of one policy, and HTTP/HTTPS supports binding of multiple policies. 
    AppPolicyVos: []appblb.AppPolicy{ 
    { 
        // Policy description 
        Description:      "test policy", 
        // Server group ID bound to the policy 
        AppServerGroupId: servergroupId, 
        // Target port number 
        BackendPort:      80, 
        // Policy priority, the valid value ranges from 1 to 32768 
        Priority:         301, 
        // Rule list of policy, TCP/UDP/SSL only supports {*: *} Policy 
        RuleList: []appblb.AppRule{ 
                { 
                    Key:   "*", 
                    Value: "*", 	 
                }, 
            }, 
        }, 
    }, 
} 
err := client.CreatePolicys(blbId, args) 
if err != nil { 
    fmt.Println("create policy failed:", err) 
} else { 
    fmt.Println("create policy success") 
} 

Hint

  • The backendPort parameter in the strategy, namely, Target port number. It is necessary to pass in the number of TCP ports opened under corresponding server group (appServerGroupId) in case the policy corresponding to listenerPort is TCP or SSL; it is necessary to pass in the number of HTTP ports opened under corresponding server group (appServerGroupId) in case the policy corresponding to listenerPort is HTTP or HTTPS; it is necessary to pass in the number of UDP ports opened under corresponding server group (appServerGroupId) in case the policy corresponding to listenerPort is UDP
  • For the specific configuration parameters of policies which can be bound to each protocol, you can refer to the BLB API document Create Policy .

Query the policy information under the corresponding BLB port

By the following codes, query the information of all server groups 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 := &appblb.DescribePolicysArgs{ 
    // The listener port to query 
    Port: 80, 
} 
result, err := client.DescribePolicys(blbId, args) 
if err != nil { 
    fmt.Println("describe policy failed:", err) 
} else { 
    fmt.Println("describe policy success: ", result) 
} 

Batch deletion of policies

By the following codes, delete the policies under the corresponding BLB port in batch.

args := &appblb.DeletePolicysArgs{ 
    // Port number of the listener where the policy is located to be deleted 
    Port: 80, 
    // Policy ID list to be deleted 
    PolicyIdList: []string{describeResult.PolicyList[0].Id}, 
} 
err := client.DeletePolicys(blbId, args) 
if err != nil { 
    fmt.Println("delete policy failed:", err) 
} else { 
    fmt.Println("delete policy success") 
} 

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 appblb detail success: ", blbDetail) 
} 
Previous
Common blb Instance
Next
Version Change Record