flink实战--REST API与metrics监控

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.youkuaiyun.com/aA518189/article/details/88952910
                </div>
                                                
                                    
            <div class="htmledit_views" id="content_views">
                                        <h1><a name="t0"></a><a></a>Flink metrics简介</h1>

          Flink的metrics是Flink公开的一个度量系统,允许收集flink程序的公开指标到外部系统,我们也可以自定义指标通过metric收集,实际开发时经常需要查看当前程序的运行状况,flink 提供了 UI 界面,有比较详细的统计信息。但是 UI 界面也有不完善的地方,比如想要获取 flink 的实时吞吐。本文将详细介绍如何通过metric监控flink程序,自定义监控指标以及metrics在flink的UI界面的应用。

 metrics在UI页面上的应用

          在flink的UI的界面上我们点击任务详情,然后点击Task Metrics会弹出如下的界面,在 add metic按钮上 我们可以添加我需要的监控指标。

注意:如果点击Task Metrics没有显示Add metics 点击一下任务的DAG图就会显示出来,当我们点击了DAG图中某个算子的名字,那么Add  metric显示的就是该算子的监控指标,且按照分区显示,算子名前置的数字就是分区号。

各个指标的含义

          关于各个指标的含义官网上有详细介绍:https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/metrics.html#availability

自定义监控指标

案例:在map算子内计算输入的总数据

设置MetricGroup为:flink_test_metric

指标变量为:mapDataNub

代码:


 
 
  1. DataStream< String> userData = kafkaData. map(new RichMapFunction< String, String>() {
  2. Counter mapDataNub;
  3. @ Override
  4. public void open( Configuration parameters) throws Exception {
  5. mapDataNub= getRuntimeContext()
  6. .getMetricGroup()
  7. .addGroup( "flink_test_metric")
  8. .counter( "mapDataNub");
  9. }
  10. @ Override
  11. public String map( String s) {
  12. String s1 = "";
  13. try {
  14. String[] split = s. split( ",");
  15. long userID = Long.parseLong( split[ 0]);
  16. long itemId = Long.parseLong( split[ 1]);
  17. long categoryId = Long.parseLong( split[ 2]);
  18. String behavior = split[ 3];
  19. long timestamp = Long.parseLong( split[ 4]);
  20. Map map = new HashMap();
  21. map.put( "userID", userID);
  22. map.put( "itemId", itemId);
  23. map.put( "categoryId", categoryId);
  24. map.put( "behavior", behavior);
  25. map.put( "timestamp", timestamp);
  26. s1 = JSON.toJSONString( map);
  27. mapDataNub.inc();
  28. System.out. println( "数据"+ map. toString());
  29. } catch ( NumberFormatException e) {
  30. e.printStackTrace();
  31. }
  32. return s1;
  33. }
  • 1

程序启动之后就可以在任务的ui界面上查看

注意点:

1.搜索自定义或者查看某个指标需要点击DAG图中对应算子的名称

2.指标的前缀0,1,2....是指算子的分区数

flink metric监控程序

        前面介绍了flink公共的监控指标以及如何自定义监控指标,那么实际开发flink任务我们需要及时知道这些监控指标的数据,去获取程序的健康值以及状态。这时候就需要我们通过 flink REST API ,自己编写监控程序去获取这些指标。很简单,当我们知道每个指标请求的URL,我们便可以编写程序通过http请求获取指标的监控数据。

flink REST API监控程序 

      为了获取flink任务运行状态和吞吐量我们需要注意一下两点:

  1.   flink集群模式需要知道 JobManager 的地址和端口(5004)
  2.   对于 flink on yarn 模式来说,则需要知道 RM 代理的 JobManager UI 地址,例如 http://yarn-resource-manager-ui/proxy/application_155316436xxxx_xxxx

1.获取flink任务运行状态(我们可以在浏览器进行测试,输入如下的连接)

 http://yarn-resource-manager-ui/proxy/application_155316436xxxx_xxxx/jobs
 
 
  • 1

返回的结果


 
 
  1. {
  2. jobs: [{
  3. id: "ce793f18efab10127f0626a37ff4b4d4",
  4. status: "RUNNING"
  5. }
  6. ]
  7. }
  • 1

2.获取 job 详情

       需要在/jobs/jobid

 http://yarn-resource-manager-ui/proxy/application_155316436xxxx_xxxx/jobs/ce793f18efab10127f0626a37ff4b4d4

 
 
  • 1
  • 2

 
 
  1. {
  2. jid: "ce793f18efab10127f0626a37ff4b4d4",
  3. name: "Test",
  4. isStoppable: false,
  5. state: "RUNNING",
  6. start - time: 1551577191874,
  7. end - time: - 1,
  8. duration: 295120489,
  9. now: 1551872312363,
  10. 。。。。。。
  11. 此处省略n行
  12. 。。。。。。
  13. }, {
  14. id: "cbc357ccb763df2852fee8c4fc7d55f2",
  15. parallelism: 12,
  16. operator: "",
  17. operator_strategy: "",
  18. description: "Source: Custom Source -&gt; Flat Map",
  19. optimizer_properties: {}
  20. }
  21. ]
  22. }
  23. }
  • 1

更灵活的方式获取每个指标的请求连接

            有人可能会问,这么多指标,难道我要把每个指标的请求的URL格式都记住吗?今天教大家一个小技巧,一个前端技术,就是进入flink任务的UI界面,按住F12进入开发者模式,然后我们点击任意一个metric指标,便能立即看到每个指标的请求的URL。比如获取flink任务的背压情况:

   如下图我们点击某一个task的status,按一下f12,便看到了backpressue,点开backpressue就是获取任务背压情况的连接如下:

http://127.0.0.1/proxy/application_12423523_133234/jobs/86eb310874aeccb37b58ae2892feced3/vertices/cbc357ccb763df2852fee8c4fc7d55f2/backpressure
 
 
  • 1

请求连接返回的json字符串如下:我们可以获取每一个分区的背压情况,如果不是OK状态便可以进行任务报警,其他的指标获取监控值都可以这样获取 简单而又便捷。

案例:实时获取yarn上flink任务运行状态

          我们使用 flink REST API的方式,通过http请求实时获取flink任务状态,不是RUNNING状态则进行电话或邮件报警,达到实时监控的效果。


 
 
  1. public class SendGet {
  2. public static String sendGet( String url) {
  3. String result = "";
  4. BufferedReader in = null;
  5. try {
  6. String urlNameString = url;
  7. URL realUrl = new URL(urlNameString);
  8. // 打开和URL之间的连接
  9. URLConnection connection = realUrl.openConnection();
  10. // 设置通用的请求属性
  11. connection.setRequestProperty( "accept", "*/*");
  12. connection.setRequestProperty( "connection", "Keep-Alive");
  13. connection.setRequestProperty( "user-agent",
  14. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  15. // 建立实际的连接
  16. connection.connect();
  17. in = new BufferedReader( new InputStreamReader(
  18. connection.getInputStream()));
  19. String line;
  20. while ((line = in.readLine()) != null) {
  21. result += line;
  22. }
  23. } catch (Exception e) {
  24. System.out.println( "发送GET请求出现异常!" + e);
  25. e.printStackTrace();
  26. }
  27. // 使用finally块来关闭输入流
  28. finally {
  29. try {
  30. if ( in != null) {
  31. in.close();
  32. }
  33. } catch (Exception e2) {
  34. e2.printStackTrace();
  35. }
  36. }
  37. return result;
  38. }
  39. public static void main( String[] args) {
  40. String s = sendGet( "http://127.0.0.1:5004/proxy/application_1231435364565_0350/jobs");
  41. JSONObject jsonObject = JSON.parseObject(s);
  42. String string = jsonObject.getString( "jobs");
  43. String substring = string.substring( 1, string.length() - 1);
  44. JSONObject jsonObject1 = JSONObject.parseObject(substring);
  45. String status = jsonObject1.getString( "status");
  46. System.out.println(status);
  47. }
  48. }
  • 1

结果

扫一扫加入大数据技术交流群,了解更多大数据技术,还有免费资料等你哦

扫一扫加入大数据技术交流群,了解更多大数据技术,还有免费资料等你哦

扫一扫加入大数据技术交流群,了解更多大数据技术,还有免费资料等你哦

                                </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值