Todos os produtos
Search
Central de documentação

Simple Log Service:Gerencie grupos de consumidores com o Simple Log Service SDK for Python

Última atualização: Sep 12, 2026

Os grupos de consumidores gerenciam o balanceamento de carga e o failover durante o consumo de dados de log, permitindo que você se concentre na lógica de negócios. Este tópico apresenta exemplos de código para criar, modificar, consultar e excluir grupos de consumidores e seus checkpoints.

Pré-requisitos

Precauções

Este exemplo utiliza o endpoint público do Simple Log Service na região China (Hangzhou). Endpoint: https://cn-hangzhou.log.aliyuncs.com.

Para acessar o Simple Log Service a partir de outros serviços da Alibaba Cloud localizados na mesma região do seu projeto, utilize o endpoint interno do Simple Log Service: https://cn-hangzhou-intranet.log.aliyuncs.com.

Para obter mais informações sobre as regiões e endpoints compatíveis com o Simple Log Service, consulte Endpoint.

Exemplo de código para criar um grupo de consumidores

O código abaixo demonstra como criar um grupo de consumidores chamado ali-test-consumergroup:

from aliyun.log import LogClient
import os

# This example obtains the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace it with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The project name.
project_name = "ali-test-project"

# The Logstore name.
logstore_name = "ali-test-logstore"

# The consumer group name.
consumergroup_name = "ali-test-consumergroup"

if __name__ == '__main__':
    print("ready to create consumergroup")
    res = client.create_consumer_group(project_name, logstore_name, consumergroup_name, 300, in_order=False)
    print("create consumergroup success ")

    res2 = client.list_consumer_group(project_name, logstore_name)
    for r in res2.get_consumer_groups():
        print("The consumergroup name is:" + r.get_consumer_group_name())

Resultados esperados:

ready to create consumergroup
create consumergroup success
The consumergroup name is:ali-test-consumergroup

Exemplo de código para modificar um grupo de consumidores

O exemplo a seguir ilustra como modificar o grupo de consumidores ali-test-consumergroup:

from aliyun.log import LogClient
import os

# This example obtains the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace it with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The project name.
project_name = "ali-test-project"

# The Logstore name.
logstore_name = "ali-test-logstore"

# The consumer group name.
consumergroup_name = "ali-test-consumergroup"

if __name__ == '__main__':
    print("ready to update consumergroup")
    # Change the timeout period of the consumer group to 350 seconds.
    res = client.update_consumer_group(project_name, logstore_name, consumergroup_name, 350, in_order=False)
    print("update consumergroup success ")

    res2 = client.list_consumer_group(project_name, logstore_name)
    for r in res2.get_consumer_groups():
        print("The consumergroup name is:" + r.get_consumer_group_name())
        print("The consumergroup timeout is:%s" % r.get_timeout())

Resultados esperados:

ready to update consumergroup
update consumergroup success
The consumergroup name is:ali-test-consumergroup
The consumergroup timeout is:350

Exemplo de código para consultar todos os grupos de consumidores

Utilize o código abaixo para listar todos os grupos de consumidores em um Logstore específico.

from aliyun.log import LogClient
import os

# This example obtains the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace it with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The project name.
project_name = "ali-test-project"

# The Logstore name.
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    print("ready to list consumergroup")
    # Query all consumer groups in the specified Logstore.
    res = client.list_consumer_group(project_name, logstore_name)
    for r in res.get_consumer_groups():
        print("The consumergroup name is:" + r.get_consumer_group_name())
        print("The consumergroup timeout is:%s" % r.get_timeout())
        print("The consumergroup order is:%s" % r.is_in_order())
    print("list consumergroup success ")

Resultados esperados:

ready to list consumergroup
The consumergroup name is:ali-test-consumergroup
The consumergroup timeout is:350
The consumergroup order is:False
list consumergroup success

Exemplo de código para excluir um grupo de consumidores

Veja como excluir um grupo de consumidores de um projeto específico usando o código abaixo:

from aliyun.log import LogClient
import os

# This example obtains the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace it with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The project name.
project_name = "ali-test-project"

# The Logstore name.
logstore_name = "ali-test-logstore"

# The consumer group name.
consumergroup_name = "ali-test-consumergroup2"

if __name__ == '__main__':
    print("ready to delete consumergroup")
    # Delete the specified consumer group.
    res = client.delete_consumer_group(project_name, logstore_name, consumergroup_name)
    print("delete consumergroup success ")

Resultados esperados:

ready to delete consumergroup
delete consumergroup success

Exemplo de código para obter o checkpoint de um grupo de consumidores

O trecho a seguir mostra como recuperar o checkpoint de um grupo de consumidores definido:

from aliyun.log import LogClient, ListConsumerGroupResponse
import os

# This example obtains the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace it with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The project name.
project_name = "ali-test-project"

# The Logstore name.
logstore_name = "ali-test-logstore"

# The consumer group name.
consumergroup_name = "ali-test-consumergroup"

if __name__ == '__main__':
    print("ready to get CheckPoint")
    # Get the checkpoint of a shard in the specified consumer group.
    res = client.get_check_point(project_name, logstore_name, consumergroup_name, shard=0)
    print("The consumergroup checkpoints info is:%s" % res.get_consumer_group_check_points())
    print("list CheckPoint success in shard_0")

Resultados esperados:

ready to get CheckPoint
The consumergroup checkpoints info is:[{'shard': 0, 'checkpoint': 'MTY3MDk5OTY3NzEzMzQzODg2NQ==', 'updateTime': 1671607210514072, 'consumer': 'consumer_1'}]
list CheckPoint success in shard_0

Referências