利用阿里云API ONS接口获取当前用户下的topics使用情况

1、main.java
package com.sc.sre;

public class Main {
    public static void main(String[] args) {
         String endPointName = "xxxxxxx";
        /**
         *Open API 的接入点,设置为目标 Region
         */
        String regionId = "xxxxxxxx";
        /**
         *鉴权使用的 AccessKey,由阿里云官网控制台获取
         */
        String accessKey = "xxxxxxx";
        /**
         *鉴权使用的 SecretKey,由阿里云官网控制台获取
         */
        String secretKey = "xxxxxxxxxxxxxxx";
        /*
         *通过 Open API 访问的云产品名称,此处设置为 Ons 即可
         */
        String productName = "Ons";
        /**
         *对应 endPoint 接入点的接入点域名
         */
        String domain = "ons.cn-hangzhou.aliyuncs.com";

        String onsRegionPub = "xxxxxxx";//查询公网ons
        String onsRegionHD1 = "xxxxxxx";//查询华东1的ons
        GetWasteTopics getWasteTopicsPub = new GetWasteTopics();
        GetWasteTopics getWasteTopicsHD1 = new GetWasteTopics();

        getWasteTopicsPub.getWasteTopics(regionId,accessKey,secretKey,productName,domain,onsRegionPub);
        getWasteTopicsHD1.getWasteTopics(regionId,accessKey,secretKey,productName,domain,onsRegionHD1);
    }
}
2、GetWasteTopics.java
package com.sc.sre;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.ons.model.v20170918.*;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.ons.model.v20170918.OnsTopicStatusResponse;
import com.aliyuncs.ons.model.v20170918.OnsTopicStatusRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.io.*;
import java.io.FileWriter;


public class GetWasteTopics {
    private String regionId;
    private String accessKey;
    private String secretKey;
    private String productName;
    private String domain;
    public  void  getWasteTopics(String regionId,String accessKey,String secretKey,String productName,
                                 String domain, String onsRegionId)
    {
        try {
            DefaultProfile.addEndpoint(regionId, regionId, productName, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, secretKey);
        IAcsClient iAcsClient = new DefaultAcsClient(profile);

        OnsRegionListRequest regionRequest = new OnsRegionListRequest();
        regionRequest.setAcceptFormat(FormatType.JSON);
        regionRequest.setPreventCache(System.currentTimeMillis());

        OnsTopicListRequest topicRequest = new OnsTopicListRequest();
        OnsTopicStatusRequest topicStatusRequest = new OnsTopicStatusRequest();
        topicStatusRequest.setAcceptFormat(FormatType.JSON);

        topicRequest.setOnsRegionId(onsRegionId);
        topicStatusRequest.setOnsRegionId(onsRegionId);
        topicRequest.setPreventCache(System.currentTimeMillis());
        topicStatusRequest.setPreventCache(System.currentTimeMillis());
        try {
            OnsTopicListResponse response = iAcsClient.getAcsResponse(topicRequest);
            List<OnsTopicListResponse.PublishInfoDo> publishInfoDoList = response.getData();

            //System.out.println(publishInfoDoList.size());
            int count = 0;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            File file = new File(onsRegionId+df.format(System.currentTimeMillis()));

            FileWriter fw = null;
            try {
                fw = new FileWriter(file);
                for (OnsTopicListResponse.PublishInfoDo publishInfoDo : publishInfoDoList) {
                topicStatusRequest.setTopic(publishInfoDo.getTopic());
                OnsTopicStatusResponse topicStatusResponse = iAcsClient.getAcsResponse(topicStatusRequest);
                OnsTopicStatusResponse.Data data = topicStatusResponse.getData();
                Long totalCount = data.getTotalCount();
                Long lastTimeStamp = data.getLastTimeStamp();
                Long temTimeStamp = System.currentTimeMillis() - lastTimeStamp;
                if (temTimeStamp / (1000 * 3600) >= 72) {// get unused topics in three days
                    count++;
                    fw.write(count+"    "+publishInfoDo.getTopic()+"\n");
                }
            }
                //fw.write("TotalCount:"+count+"\n");
            fw.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fw != null) {
                        try {
                            fw.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    //Convert Unix timestamp to normal date style
    public static String stampToDate(long s){
        String lastTime = Long.toString(s);
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(lastTime);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
}

3、用IDE编译或者打成jar包运行,获取公网和华东1的ons 近三天未更新的topics列表。
 

reference:https://help.aliyun.com/document_detail/29593.html


1、MQ场景     1)订单异步解耦     2)解决分布式事务问题     3)应用于聊天平台     4)大规模机器的Cache同步     5)MySQL BinLog订阅数据分发 2、ONS应用场景     异步、解耦、最终一致、并行 3、设计假定     1)每台PC机器都可能down机不可服务     2)任意集群都可能处理能力不足     3)最坏情况一定会发生     4)内网环境需要低延迟来提供你最佳用户体验 4、关键设计     1)分布式集群化         a、理论上无限处理能力         b、集群级别高可用     2)强数据安全         a、单机磁盘级别冗余         b、单组多队列级别冗余         c、多组消息队列冗余     3)海量数据堆积         a、推模式:订阅者逻辑简单         b、拉模式:关注吞吐量,快         c、推拉结合:队列通知消费者,消费者去拉取(两次交互)         d、阿里采用长连接和轮询:轮询去拉,有则拉取,无则保持长连接等待,直到有消息     4)毫秒级投递延迟 5、关键概念     1)Topic:第一级消息类型,主标题     2)Tug:第二级消息类型,分标题     3)发送组:生产者所在集群     4)订阅组:消费者所在集群     5)RocketMQ不是一对一,也不是一对多,是随机一对一     6)网络三种状态:成功、失败、没响应 6、消息乱序问题:Message服务器不处理,恰好不需要解决     1)发送时对消息进行编号     2)一组消息只有唯一一个订阅者处理(sharding)     3)一组消息的数量(即“锁的颗粒度”)越小越好 7、消息重复问题     1)重复原因:网络不可达     2)幂等:某个操作无论重复多少次,结果都一样(不需要解决,性能极高)     3)非幂等,去重         a、保证有个唯一ID标记每一条消息;         b、保证消息处理成功与去重表日志同时出现     4)去重代价:额外的tps和qps 8、事务的分布式优化     1)事务1-->MQ Server-->事务2     2)同时成功,同时失败:         a、发消息;         b、执行事务1;         c、确认消息发送;         d、投递消息到消费者     3)处理超时问题(重复):事务2增加消息确认表(去重表)     4)消息失败(事务2失败):记录后人工处理(小概率事件)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值