全部產品
Search
文件中心

Simple Log Service:使用Java SDK管理機器組

更新時間:Oct 25, 2024

機器組是包含多台伺服器的虛擬分組,Log Service通過機器組的方式管理所有需要通過Logtail採集日誌的伺服器。本文通過程式碼範例介紹如何建立、修改、查詢、刪除機器組等。

前提條件

  • 已開通Log Service。更多資訊,請參見開通Log Service

  • 已建立RAM使用者並完成授權。具體操作,請參見建立RAM使用者並完成授權

  • 已配置環境變數ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具體操作,請參見在Linux、macOS和Windows系統配置環境變數

    重要
    • 阿里雲帳號的AccessKey擁有所有API的存取權限,建議您使用RAM使用者的AccessKey進行API訪問或日常營運。

    • 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。

注意事項

本樣本以華東1(杭州)的公網Endpoint為例,其公網Endpoint為https://cn-hangzhou.log.aliyuncs.com。如果您通過與Project同地區的其他阿里雲產品訪問Log Service,請使用內網Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。關於Log Service支援的地區與Endpoint的對應關係,請參見服務入口

建立機器組範例程式碼

以下代碼用於建立名為ali-test-machinegroup的IP地址機器組。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.GroupAttribute;
import com.aliyun.openservices.log.common.MachineGroup;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateMachineGroupRequest;

import java.util.Collections;

public class CreateMachineGroup {
    public static void main(String[] args) throws LogException {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

        try {
            // 設定機器組名稱。
            String machineGroupName = "ali-test-machinegroup";
            System.out.println("ready to create machinegroup");

            MachineGroup machineGroup = new MachineGroup();
           
            machineGroup.SetGroupName(machineGroupName);

            // 建立IP地址機器組。
            machineGroup.SetMachineIdentifyType("ip");

            // 設定機器組屬性。
            GroupAttribute groupAttribute = new GroupAttribute();

            // 設定機器組Topic。
            groupAttribute.SetGroupTopic("ide test");
            machineGroup.SetGroupAttribute(groupAttribute);

            // 設定機器組標識,此處為IP地址。
            machineGroup.SetMachineList(Collections.singletonList("192.168.XX.XX"));

            // 建立機器組。
            CreateMachineGroupRequest request = new CreateMachineGroupRequest(projectName, machineGroup);
            client.CreateMachineGroup(request);

            System.out.println(String.format("create machinegroup %s success", machineGroupName));

        } 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;
        }
    }
}

預期結果如下:

ready to create machinegroup
create machinegroup ali-test-machinegroup success

修改機器組範例程式碼

以下代碼用於修改名為ali-test-machinegroup的IP地址機器組。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.GroupAttribute;
import com.aliyun.openservices.log.common.MachineGroup;
import com.aliyun.openservices.log.exception.LogException;

import java.util.Collections;

public class UpdateMachineGroup {
    public static void main(String[] args) throws LogException {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

        try {
            // 輸入機器組名稱。
            String machineGroupName = "ali-test-machinegroup";
            System.out.println("ready to update machinegroup");

            MachineGroup machineGroup = new MachineGroup();
            
            machineGroup.SetGroupName(machineGroupName);

            // 設定IP地址機器組。
            machineGroup.SetMachineIdentifyType("ip");

            // 設定機器組屬性。
            GroupAttribute groupAttribute = new GroupAttribute();

            // 修改機器組Topic。
            groupAttribute.SetGroupTopic("new ide test");
            machineGroup.SetGroupAttribute(groupAttribute);

            // 更新機器組。
            client.UpdateMachineGroup(projectName, machineGroup);

            System.out.println(String.format("update machinegroup %s success", machineGroupName));

        } 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;
        }
    }
}

預期結果如下:

ready to update logstore
update logstore ali-test-logstore success

查詢所有機器組範例程式碼

以下代碼用於查詢目標Project下的所有機器組。

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

public class ListMachineGroup {
    public static void main(String[] args) throws LogException {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

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

            // 查詢Project下的所有機器組。
            ListMachineGroupResponse response = client.ListMachineGroup(projectName);

            for (String machineGroup : response.GetMachineGroups()) {
                System.out.println(machineGroup.toString());
            }

            System.out.println(String.format("list project %s machinegroup 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;
        }
    }
}

預期結果如下:

ready to list machinegroup
ali-test-machinegroup
ali-test-machinegroup2
list project ali-test-project machinegroup success

查詢指定機器組範例程式碼

以下代碼用於查詢指定機器組資訊。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.MachineGroup;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetMachineGroupResponse;

public class GetMachineGroup {
    public static void main(String[] args) throws LogException {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

        try {
            // 輸入機器組名稱。
            String machineGroupName = "ali-test-machinegroup";
            System.out.println("ready to get machinegroup");

            // 查詢目標機器組。
            GetMachineGroupResponse response = client.GetMachineGroup(projectName, machineGroupName);
            MachineGroup machineGroup = response.GetMachineGroup();

            // 輸出目標機器組資訊。
            System.out.println("The machinegroup name is:" + machineGroup.GetGroupName());
            System.out.println("The machinegroup topic is:" + machineGroup.GetGroupTopic());
            System.out.println("The machinegroup list is:" + machineGroup.GetMachineList());

            System.out.println(String.format("get machinegroup 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;
        }
    }
}

預期結果如下:

ready to get machinegroup
The machinegroup name is:ali-test-machinegroup
The machinegroup topic is:new ide test
The machinegroup list is:[192.168.XX.XX]
get machinegroup from ali-test-project success

刪除機器組範例程式碼

以下代碼用於刪除目標Project下的機器組。

重要

刪除機器組後,會解除綁定對應的Logtail採集配置,將無法採集日誌。

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

public class DeleteMachineGroup {
    public static void main(String[] args) throws LogException {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

        try {
            // 輸入機器組名稱。
            String machineGroupName = "ali-test-machinegroup";
            System.out.println("ready to delete machinegroup");

            // 刪除目標機器組。
            client.DeleteMachineGroup(projectName, machineGroupName);

            System.out.println(String.format("delete machinegroup 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;
        }
    }
}

預期結果如下:

ready to delete machinegroup
delete machinegroup from ali-test-project success

相關文檔