详情页标题前

阿里云日志服务SLS使用Java SDK管理机器组-云淘科技

详情页1

机器组是包含多台服务器的虚拟分组,日志服务通过机器组的方式管理所有需要通过Logtail采集日志的服务器。本文通过代码示例介绍如何创建、修改、查询、删除机器组等。

前提条件

  • 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权。

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见配置环境变量。

    重要

    • 阿里云账号的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接口说明,请参见如下:

    • CreateMachineGroup

    • DeleteMachineGroup

    • GetMachineGroup

    • ListMachineGroup

    • UpdateMachineGroup

    • ApplyConfigToMachineGroup

  • 更多示例代码,请参见Aliyun Log Java SDK on GitHub。

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家

阿里云企业补贴进行中: 马上申请

腾讯云限时活动1折起,即将结束: 马上收藏

同尘科技为腾讯云授权服务中心。

购买腾讯云产品享受折上折,更有现金返利:同意关联,立享优惠

转转请注明出处:https://www.yunxiaoer.com/166076.html

(0)
上一篇 2023年12月10日
下一篇 2023年12月10日
详情页2

相关推荐

  • 阿里云日志服务SLS鉴权规则-云淘科技

    当RAM用户通过日志服务API对阿里云账号的资源进行访问时,日志服务后台对RAM用户进行权限检查,以确保资源拥有者的确将相关资源的相关权限授予了调用者。本文列举RAM用户通过日志服务API访问阿里云账号资源时的鉴权规则。 Logstore 每个不同的日志服务API会根据涉及到的资源以及API的语义来确定需要检查哪些资源的权限。具体各类API的鉴权规则见下表。…

    阿里云日志服务SLS 2023年12月10日
  • 阿里云日志服务SLS使用GetHistograms查询日志分布数量-云淘科技

    本文通过代码示例介绍如何使用GetHistograms接口查询日志在某时间区间中的分布数量。 前提条件 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权。 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见配置环境变量。 重要 阿里云账号的…

    阿里云日志服务SLS 2023年12月10日
  • 信息流广告,信息流部分建议宽度830px,只针对默认列表样式,顺序随机
  • 阿里云对象存储OSSJava获取存储空间的地域-云淘科技

    存储空间(Bucket)是存储对象(Object)的容器。对象都隶属于存储空间。本文介绍如何获取存储空间所在的地域。 注意事项 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见访问域名和数据中心。 本文以从环境变量…

    阿里云对象存储 2023年12月10日
  • 阿里云RDS数据库DescribeResourceUsage – 查看实例的空间利用信息-云淘科技

    该接口用于查询RDS实例的空间使用信息。 接口说明 适用引擎 RDS MySQL RDS PostgreSQL RDS SQL Server RDS MariaDB 调试 您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。 调试调试授权信息下表是API对应的…

    阿里云数据库 2023年12月9日
  • 阿里云负载均衡UpdateListenerAttribute – 更新监听配置-云淘科技

    更新网络型负载均衡监听的配置。 调试 您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。 调试调试授权信息下表是API对应的授权信息,可以在RAM权限策略语句的Action元素中使用,用来给RAM用户或RAM角色授予调用此API的权限。具体说明如下: 操作:…

    阿里云负载均衡 2023年12月10日

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
本站为广大会员提供阿里云、腾讯云、华为云、百度云等一线大厂的购买,续费优惠,保证底价,买贵退差。