All Products
Search
Document Center

Simple Log Service:Gunakan Java SDK untuk mengelola kelompok konsumen

Last Updated:Jun 04, 2026

Kelompok konsumen menangani load balancing dan failover untuk konsumsi data log, sehingga Anda dapat fokus pada logika bisnis. Topik ini menyediakan contoh kode untuk membuat, memodifikasi, mengkueri, dan menghapus kelompok konsumen beserta checkpoint-nya.

Prasyarat

Perhatian

Pada contoh ini, endpoint publik Simple Log Service untuk wilayah China (Hangzhou) digunakan. Endpoint: https://cn-hangzhou.log.aliyuncs.com.

Jika Anda ingin mengakses Simple Log Service dari layanan Alibaba Cloud lain yang berada di wilayah yang sama dengan proyek Anda, Anda dapat menggunakan endpoint internal Simple Log Service, yaitu https://cn-hangzhou-intranet.log.aliyuncs.com.

Untuk informasi selengkapnya mengenai wilayah dan endpoint Simple Log Service yang didukung, lihat Endpoint.

Buat kelompok konsumen

Kode berikut membuat kelompok konsumen bernama ali-test-consumergroup.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.ConsumerGroup;
import com.aliyun.openservices.log.exception.LogException;

public class CreateConsumerGroup {
    public static void main(String[] args) throws LogException {
         // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String projectName = "ali-test-project";
        // Masukkan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti dengan titik akhir yang sesuai.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Tetapkan nama kelompok konsumen.
            String consumerGroupName = "ali-test-consumergroup2";
            System.out.println("ready to create consumergroup");

            ConsumerGroup consumerGroup = new ConsumerGroup(consumerGroupName, 300, true);

            client.CreateConsumerGroup(projectName, logstoreName, consumerGroup);

            System.out.println(String.format("create consumergroup %s success", consumerGroupName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Hasil yang diharapkan:

ready to create consumergroup
create consumergroup ali-test-consumergroup success

Modifikasi kelompok konsumen

Kode berikut memodifikasi kelompok konsumen bernama ali-test-consumergroup.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class UpdateConsumerGroup {
    public static void main(String[] args) throws LogException {
         // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String projectName = "ali-test-project";
        // Masukkan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti dengan titik akhir yang sesuai.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        try {
            String consumerGroupName = "ali-test-consumergroup";
            System.out.println("ready to update consumergroup");

            // Ubah periode timeout kelompok konsumen menjadi 350 detik.
            client.UpdateConsumerGroup(projectName, logstoreName, consumerGroupName, false, 350);

            System.out.println(String.format("update consumergroup %s success", consumerGroupName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Hasil yang diharapkan:

ready to update consumergroup
update consumergroup ali-test-consumergroup success

Kueri semua kelompok konsumen

Kode berikut mengkueri semua kelompok konsumen dalam suatu Logstore.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.ConsumerGroup;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListConsumerGroupResponse;

public class ListConsumerGroup {
    public static void main(String[] args) throws LogException {
         // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String projectName = "ali-test-project";
        // Masukkan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti dengan titik akhir yang sesuai.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        try {
            System.out.println("ready to list consumergroup");

            // Kueri semua kelompok konsumen dalam Logstore yang ditentukan.
            ListConsumerGroupResponse response = client.ListConsumerGroup(projectName,logstoreName);

            for(ConsumerGroup consumerGroup : response.GetConsumerGroups()){
                System.out.println("ConsumerName is : " + consumerGroup.getConsumerGroupName());
            }

            System.out.println(String.format("list consumergroup from %s success",projectName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Hasil yang diharapkan:

ready to list consumergroup
ConsumerName is : ali-test-consumergroup2
ConsumerName is : ali-test-consumergroup
list consumergroup from ali-test-project success

Hapus kelompok konsumen

Kode berikut menghapus kelompok konsumen tertentu.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class DeleteConsumerGroup {
    public static void main(String[] args) throws LogException {
         // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String projectName = "ali-test-project";
        // Masukkan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti dengan titik akhir yang sesuai.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        try {
            String consumerGroupName = "ali-test-consumergroup";
            System.out.println("ready to delete consumergroup");

            // Hapus kelompok konsumen tertentu.
            client.DeleteConsumerGroup(projectName,logstoreName,consumerGroupName);

            System.out.println(String.format("delete consumergroup %s success",consumerGroupName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Hasil yang diharapkan:

ready to delete consumergroup
delete consumergroup ali-test-consumergroup success

Dapatkan checkpoint kelompok konsumen

Kode berikut mendapatkan checkpoint dari kelompok konsumen.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetCheckPointResponse;

public class GetCheckPoint {
    public static void main(String[] args) throws LogException {
         // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String projectName = "ali-test-project";
        // Masukkan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti dengan titik akhir yang sesuai.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        try {
            String consumerGroupName = "consumerGroupX";
            System.out.println("ready to get consumergroup checkpoint");

            // Dapatkan checkpoint shard dalam kelompok konsumen yang ditentukan.
            GetCheckPointResponse response1 = client.getCheckpoint(projectName,logstoreName,consumerGroupName,0);
            GetCheckPointResponse response2 = client.getCheckpoint(projectName,logstoreName,consumerGroupName,1);
            System.out.println("The checkpoint of Shard 0 is : " + response1.getCheckpoint());
            System.out.println("The checkpoint of Shard 1 is : " + response2.getCheckpoint());

            System.out.println(String.format("get consumergroup %s checkpoint success",consumerGroupName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Hasil yang diharapkan:

ready to get consumergroup checkpoint
The checkpoint of Shard 0 is : ConsumerGroupShardCheckPoint [shard=0, checkPoint=MTY2NzgxMDc0Nzk5MDk5MzAyMg==, updateTime=1668750821709044, consumer=consumer_1]
The checkpoint of Shard 1 is : ConsumerGroupShardCheckPoint [shard=1, checkPoint=MTY2NzgxMDc0Nzk5MTk0NTU0NQ==, updateTime=1668750828790425, consumer=consumer_1]
get consumergroup consumerGroupX checkpoint success

Perbarui checkpoint kelompok konsumen

Kode berikut memperbarui checkpoint dari kelompok konsumen.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.ConsumerGroup;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListConsumerGroupResponse;

public class ConsumerGroupUpdateCheckpoint {
    public static void main(String[] args) throws LogException {
         // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Masukkan nama proyek.
        String projectName = "ali-test-project";
        // Masukkan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti dengan titik akhir yang sesuai.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);

        try {
            String consumerGroupName = "consumerGroupX";
            System.out.println("ready to update checkpoint");

            // Kueri semua kelompok konsumen dalam Logstore yang ditentukan.
            ListConsumerGroupResponse response = client.ListConsumerGroup(projectName, logstoreName);

            for(ConsumerGroup consumerGroup : response.GetConsumerGroups()){
                System.out.println("ConsumerName is : " + consumerGroup.getConsumerGroupName());
                System.out.println("Consumer order is : " + consumerGroup.isInOrder());
            }

            // Perbarui checkpoint Shard 0. Anda dapat memanggil operasi GetCursor untuk mendapatkan kursor pada waktu tertentu.
            client.UpdateCheckPoint(projectName, logstoreName, consumerGroupName, 0, "MTY2NzgxMDc0Nzk5MTAwNjQ3Mg==");
            System.out.println(String.format("update checkpoint of %s success", consumerGroupName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Hasil yang diharapkan:

ready to update checkpoint
ConsumerName is : consumerGroupX
Consumer order is : false
ConsumerName is : ali-test-consumergroup2
Consumer order is : true
ConsumerName is : ali-test-consumergroup
Consumer order is : false
update consumergroup checkpoint is:consumerGroupX
update checkpoint of consumerGroupX success

Referensi