機器組是包含多台伺服器的虛擬分組,Log Service通過機器組的方式管理所有需要通過Logtail採集日誌的伺服器。本文通過程式碼範例介紹如何建立、修改、查詢、刪除機器組等。
前提條件
已開通Log Service。更多資訊,請參見開通Log Service。
已建立RAM使用者並完成授權。具體操作,請參見建立RAM使用者並完成授權。
已配置環境變數ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。具體操作,請參見在Linux、macOS和Windows系統配置環境變數。
重要阿里雲帳號的AccessKey擁有所有API的存取權限,建議您使用RAM使用者的AccessKey進行API訪問或日常營運。
強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。
已安裝Log ServiceJava SDK。具體操作,請參見安裝Java SDK。
已建立Project。具體操作,請參見使用Java SDK管理Project。
注意事項
本樣本以華東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相關文檔
在調用API介面過程中,若服務端返回結果中包含錯誤資訊,則表示調用API介面失敗。您可以參考API錯誤碼對照表尋找對應的解決方案。更多資訊,請參見API錯誤處理對照表。
阿里雲OpenAPI開發人員門戶提供調試、SDK、樣本和配套文檔。通過OpenAPI,您無需手動封裝請求和簽名操作,就可以快速對Log ServiceAPI進行調試。更多資訊,請參見OpenAPI開發人員門戶。
為滿足越來越多的自動化Log Service配置需求,Log Service提供命令列工具CLI(Command Line Interface)。更多資訊,請參見Log Service命令列工具CLI。
關於機器組API介面說明,請參見如下:
更多範例程式碼,請參見Aliyun Log Java SDK on GitHub。