接触小米这款运维软件Open-Falcon一周了,最近想用java来尝试push数据。下面是我的测试代码:
private static void Test(){ try{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://127.0.0.1:1988/v1/push"); JSONObject js = new JSONObject(); js.put("endpoint","test"); js.put("metric","test-metric"); js.put("timestamp",System.currentTimeMillis()); js.put("step",60); js.put("value", 100); js.put("counterType", "GAUGE"); js.put("tags","mark=test"); StringEntity s = new StringEntity("["+js.toString()+"]"); //一开始没加[],一直报错误400 s.setContentEncoding("UTF-8"); s.setContentType("application/json"); post.setEntity(s); HttpResponse res = client.execute(post); if(res.getStatusLine().getStatusCode() == 200){ HttpEntity entity = res.getEntity(); System.out.println(EntityUtils.toString(entity)); } } catch(Exception e){ System.out.println(e); } } 上面需要用到json的相关包和httpclient、httpcore包。根据需要修改代码后,打包成可运行的jar包,部署到对应的linux服务器上,配置为crontab进程,即可实现定时push数据到agent上面。
