更新时间:2024-05-17 GMT+08:00

获取会话列表

功能介绍

获取会话列表。

调用方法

请参见如何调用API

URI

GET /v2/{project_id}/instances/{instance_id}/clients

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID。获取方法请参见获取项目ID

instance_id

String

实例ID。

表2 Query参数

参数

是否必选

参数类型

描述

node_id

String

节点ID。

offset

Integer

偏移量,表示从此偏移量开始查询,偏移量大于等于0,默认为0。

最小值:1

limit

Integer

每页显示条数,最小值为1,最大值为1000,默认值为10。

最小值:1

最大值:1000

缺省值:10

addr

String

按客户端连接地址过滤。

sort

String

排序字段,指定会话列表ClientInfo中的一个值进行排序。

order

String

排序方式,asc:正序排序,desc:倒序排序。

缺省值:desc

枚举值:

  • asc

  • desc

请求参数

响应参数

状态码: 200

表3 响应Body参数

参数

参数类型

描述

time

String

数据更新时间。

clients

Array of ClientInfo objects

会话列表。

count

Integer

会话总数。

表4 ClientInfo

参数

参数类型

描述

id

String

客户端ID。

addr

String

客户端所在的地址和端口。

fd

String

容器内所使用的文件描述符。

name

String

客户端的名称。

cmd

String

最近一次执行的命令。

age

Integer

已连接时长(单位:秒)。

idle

Integer

空闲时长(单位:秒)。

db

String

该客户端正在使用的数据库ID。

flags

String

客户端标志。默认未设置,值为N。

sub

Integer

已订阅频道的数量。

psub

Integer

已订阅模式的数量。

multi

Integer

在事务中被执行的命令数量。

qbuf

Integer

查询缓冲区的长度(单位为字节,0表示没有分配查询缓冲区)。

qbuf_free

Integer

查询缓冲区剩余空间的长度(单位为字节,0表示没有剩余空间)。

obl

Integer

输出缓冲区的长度(单位为字节,0表示没有分配输出缓冲区)。

oll

Integer

输出列表包含的对象数量(当输出缓冲区没有剩余空间时,命令回复会以字符串对象的形式被入队到这个队列里)

omem

Integer

输出缓冲区和输出列表占用的内存总量。

events

String

文件操作事件,r:读;w:写。

枚举值:

  • r

  • w

network

String

客户端所使用的网络类型。

peer

String

client地址和端口。

user

String

客户端用户。

状态码: 400

表5 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息。

状态码: 401

表6 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息。

状态码: 403

表7 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息。

状态码: 404

表8 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息。

状态码: 500

表9 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息。

请求示例

GET https://{dcs_endpoint}/v2/{project_id}/instances/{instance_id}/clients?node_id=8f7ee181-88e2-49b7-a4bf-81f18f93a135

响应示例

状态码: 200

查询会话管理列表成功。

{
  "time" : "2023-06-15T03:10:01.332Z",
  "clients" : [ {
    "id" : "11677",
    "addr" : "198.19.131.14:1637",
    "name" : "dcs",
    "cmd" : "info",
    "age" : 376,
    "idle" : 376,
    "flags" : "N",
    "fd" : "16",
    "db" : "0",
    "sub" : 0,
    "psub" : 0,
    "multi" : 1,
    "qbuf" : 0,
    "qbuf_free" : 4,
    "obl" : 0,
    "oll" : 0,
    "omem" : 0,
    "events" : "r",
    "network" : "vpc",
    "peer" : "198.198.0.121:4058",
    "user" : "dcs"
  } ],
  "count" : 1
}

状态码: 400

非法请求。

{
  "error_code" : "DCS.4957",
  "error_msg" : "This operation is not allowed when the task is in the current state."
}

SDK代码示例

SDK代码示例如下。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.dcs.v2.region.DcsRegion;
import com.huaweicloud.sdk.dcs.v2.*;
import com.huaweicloud.sdk.dcs.v2.model.*;


public class ListClientsSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        DcsClient client = DcsClient.newBuilder()
                .withCredential(auth)
                .withRegion(DcsRegion.valueOf("<YOUR REGION>"))
                .build();
        ListClientsRequest request = new ListClientsRequest();
        request.withNodeId("<node_id>");
        request.withOffset(<offset>);
        request.withLimit(<limit>);
        request.withAddr("<addr>");
        request.withSort("<sort>");
        request.withOrder(ListClientsRequest.OrderEnum.fromValue("<order>"));
        try {
            ListClientsResponse response = client.listClients(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkdcs.v2.region.dcs_region import DcsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkdcs.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = __import__('os').getenv("CLOUD_SDK_AK")
    sk = __import__('os').getenv("CLOUD_SDK_SK")

    credentials = BasicCredentials(ak, sk) \

    client = DcsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(DcsRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = ListClientsRequest()
        request.node_id = "<node_id>"
        request.offset = <offset>
        request.limit = <limit>
        request.addr = "<addr>"
        request.sort = "<sort>"
        request.order = "<order>"
        response = client.list_clients(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    dcs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := dcs.NewDcsClient(
        dcs.DcsClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.ListClientsRequest{}
	request.NodeId = "<node_id>"
	offsetRequest:= int32(<offset>)
	request.Offset = &offsetRequest
	limitRequest:= int32(<limit>)
	request.Limit = &limitRequest
	addrRequest:= "<addr>"
	request.Addr = &addrRequest
	sortRequest:= "<sort>"
	request.Sort = &sortRequest
	orderRequest:= model.GetListClientsRequestOrderEnum().<ORDER>
	request.Order = &orderRequest
	response, err := client.ListClients(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

查询会话管理列表成功。

400

非法请求。

401

认证信息非法。

403

请求被拒绝访问。

404

所请求的资源不存在。

500

内部服务错误。

错误码

请参见错误码