设置集群均衡配置示例代码
引导式阅读
Python
设置集群均衡配置示例代码
作者
c***r
上架时间
2023-11-17 08:33:28

1. 介绍

文档数据库服务(document database service,简称dds)完全兼容mongodb协议,提供安全、高可用、高可靠、弹性伸缩和易用的数据库服务,同时提供一键部署、弹性扩容、容灾、备份、恢复、监控和告警等功能。

2. 流程图

设置集群均衡开关的流程图

3. 前置条件

1.已 注册 华为云,并完成 实名认证

2.获取华为云开发工具包(SDK),您也可以查看安装Python SDK。

3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥

4.已具备开发环境 ,支持Python 3及其以上版本。

4. SDK获取和安装

具体的SDK版本号请参见 SDK开发中心

pip install huaweicloudsdkdds

5. 关键代码片段

以下代码展示如何使用SDK设置集群均衡开关:

class ClusterConfigDemo: def __init__(self): pass @staticmethod def main(args): """ args[0] = "<YOUR IAM_END_POINT>" args[1] = "<YOUR END_POINT>" args[2] = "<YOUR AK>" args[3] = "<YOUR SK>" args[4] = "<YOUR INSTANCE_ID>" args[5] = "<YOUR REGION_ID>" @param args """ if (len(args) != 6): print("Illegal Arguments") iam_endpoint = args[0] endpoint = args[1] # 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; ak = args[2] sk = args[3] instance_id = args[4] region_id = args[5] auth = BasicCredentials( iam_endpoint=iam_endpoint, ak=ak, sk=sk ) client = DdsClient.new_builder() \ .with_credentials(credentials=auth) \ .with_region(region=Region(id=region_id, endpoint=endpoint)) \ .build() # 设置集群均衡开关 ClusterConfigDemo.__set_cluster_balancer_switch(client, instance_id) # 设置集群均衡活动时间窗 ClusterConfigDemo.__set_cluster_balancer_time_window(client, instance_id) # 查询集群均衡设置 ClusterConfigDemo.__query_cluster_balancer_config(client, instance_id) @staticmethod def __query_cluster_balancer_config(client, instance_id): request = ShowShardingBalancerRequest() request.instance_id = instance_id try: response = client.show_sharding_balancer(request) print(response) except ConnectionException as e: print("ConnectionException", e) except RequestTimeoutException as e: print("RequestTimeoutException ", e) except ServiceResponseException as e: print("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.status_code, e.error_code, e.error_msg) @staticmethod def __set_cluster_balancer_time_window(client, instance_id): request = SetBalancerWindowRequest() balancer_active_window = BalancerActiveWindow() balancer_active_window.start_time = "20:00" balancer_active_window.stop_time = "23:00" request.instance_id = instance_id request.body = balancer_active_window try: response = client.balancer_window = request print(response) except ConnectionException as e: print("ConnectionException", e) except RequestTimeoutException as e: print("RequestTimeoutException ", e) except ServiceResponseException as e: print("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.status_code, e.error_code, e.error_msg) @staticmethod def __set_cluster_balancer_switch(client, instance_id): request = SetBalancerSwitchRequest() request.instance_id = instance_id request.action = "start" try: response = client.balancer_switch = request print(response) except ConnectionException as e: print("ConnectionException", e) except RequestTimeoutException as e: print("RequestTimeoutException ", e) except ServiceResponseException as e: print("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.status_code, e.error_code, e.error_msg)

6. 返回结果示例

  • 设置集群均衡开关(SetBalancerSwitch)接口的返回值:
class SetBalancerSwitchResponse { jobId: d890eb80-d16c-438f-a623-376f02f0db3d }
  • 设置集群均衡活动时间窗(SetBalancerWindow)接口的返回值:
class SetBalancerWindowResponse { jobId: 9e1ae2e6-bffd-4c95-8dc0-46b73432cf95 }
  • 查询集群均衡设置(ShowShardingBalancer)接口的返回值:
class ShowShardingBalancerResponse { isOpen: true activeWindow: class BalancerActiveWindow { startTime: 20:00 stopTime: 23:00 } }

7.参考链接

请见 设置集群均衡开关API 您可以在 API Explorer 中直接运行调试该接口。

请见 设置集群均衡活动时间窗API 您可以在 API Explorer 中直接运行调试该接口。

请见 查询集群均衡设置API 您可以在 API Explorer 中直接运行调试该接口。

修订记录

发布日期 文档版本 修订说明
2022-10-20 1.0 文档首次发布