基于springboot的httpclient访问ES的logs实现整理

一、创建httpclient工具类,由于采用的是dopost()方法进行请求响应,考虑到是带参请求。

 

二、封装ES中json数据的工具类,以及解析json数据的工具类。

1、封装es,需要用到aggs(聚合函数),封装的过程是根据聚合函数的最里层到最外层进行层层封装。(注意到:"size"和"aggs"是在并列层,所以封装在同一个对象中)

/**
     * Description: 封装ES工具类
     *
     * @author lisiying
     * @date Created on 2019年8月29日
     */
    public static JSONObject packageEs(){
        /**
         *      {
         *           "size":0,
         *              "aggs": {
         *                 "agg1": {
         *                  "terms": {
         *                         "field": "srvName.keyword"
         *                }
         *              }
         *           }
         *        }
         */
//封装查询所有服务的count数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("size",0);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("field","srvName.keyword");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("terms",jsonObject1);
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("agg1",jsonObject2);
        jsonObject.put("aggs",jsonObject3);
        return jsonObject;

    }

2、解析json数据,可以用map封装,也可用object,依照个人习惯,这里我采用的是map进行封装解析数据。

@Component
public class ParamJsonUtil {
    //解析数据用map封装(也可以用对象),解析json字符串,将x、y轴传入map集合
    public Map<String,List<String>> analysisMap(String message){
        //创建json对象,将参数message封装进messageJson中。
        JSONObject messageJson=JSONObject.parseObject(message);
        //创建数组类型的json对象,通过带参的messageJson对象获取需要解析的数据类型封装在数组jsonArray中
        JSONArray jsonArray = messageJson.getJSONObject("aggregations").getJSONObject("num").getJSONArray("buckets");
         //创建X/Y轴列表
        List<String> servername_x = new ArrayList<String>();
        List<String> count_y = new ArrayList<String>();
//遍历jsonArray数组,将遍历到的数据根据服务名和调用量添加到servername_x和count_y列表中。
        for (int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            servername_x.add(jsonObject1.getString("key_as_string"));
            count_y.add(jsonObject1.getString("doc_count"));
        }
        //创建map对象,将X,Y轴列表数据封装进map中。
        Map<String,List<String>> map = new HashMap<String, List<String>>();
        map.put("key_as_string",servername_x);
        map.put("doc_count",count_y);
        return map;
    }

三、controller层,带参数(url,jsonObject)的dopost请求,并响应。

@Controller
public class ServerController extends BaseController {

   public static String url = "http://172.18.232.196:9200/test-2019-02/doc/_search?pretty";

    @RequestMapping("/showMessage")
    public String firstpage(){
        return "manage/AccessLog/showMessage";
    }
    @RequestMapping("echartsPage")
    @ResponseBody
    public Map ES() {
        //1、创建客户端对象
        HttpClientUtils httpClientUtils = new HttpClientUtils();
        //2、创建es工具类对象,调用serverJson(),获取封装服务调用计数的api
        EsUtil esUtil=new EsUtil();
        //3、通过将URL和请求参数封装进POST方法中,发出请求。并将请求返回对象放入message中
        String message = httpClientUtils.doPostTest(url,esUtil.serverJson());
        //4、创建解析json的工具类对象,
        ParamJsonUtil paramJsonUtil=new ParamJsonUtil();
        //5、调用解析工具类中的解析方法,解析message并返回在showMessage页面中。
        return paramJsonUtil.analysisMap(message);
    }
}

四、页面显示。

<body>
<div id = "esEcharts" style="height: 400px"></div>
......
    $.post("echartsPage",function (data) {
        var myChart = echarts.init(document.getElementById('esEcharts'));
      ......
</body>

注:其中id中标识要和document.getElementById('*')中的*保持一致;$.post("echartsPage",function (data) 中的“echartsPage”和controller中的 @RequestMapping("echartsPage")中的标识也要保持一致。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值