机器组是包含多台服务器的虚拟分组,日志服务通过机器组的方式管理所有需要通过Logtail采集日志的服务器。本文通过代码示例介绍如何创建、修改、查询、删除机器组等。
前提条件
已开通日志服务。更多信息,请参见开通日志服务。
已创建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泄露,威胁您账号下所有资源的安全。
已安装日志服务Java SDK。具体操作,请参见安装Java SDK。
已创建Project。具体操作,请参见使用Java SDK管理Project。
注意事项
本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com
。如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com
。关于日志服务支持的地域与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";
// 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
String host = "https://cn-hangzhou.log.aliyuncs.com";
// 创建日志服务Client。
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";
// 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
String host = "https://cn-hangzhou.log.aliyuncs.com";
// 创建日志服务Client。
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";
// 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
String host = "https://cn-hangzhou.log.aliyuncs.com";
// 创建日志服务Client。
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";
// 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
String host = "https://cn-hangzhou.log.aliyuncs.com";
// 创建日志服务Client。
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";
// 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
String host = "https://cn-hangzhou.log.aliyuncs.com";
// 创建日志服务Client。
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,您无需手动封装请求和签名操作,就可以快速对日志服务API进行调试。更多信息,请参见OpenAPI开发者门户。
为满足越来越多的自动化日志服务配置需求,日志服务提供命令行工具CLI(Command Line Interface)。更多信息,请参见日志服务命令行工具CLI。
关于机器组API接口说明,请参见如下:
更多示例代码,请参见Aliyun Log Java SDK on GitHub。