当前位置:   article > 正文

Sentinel+prometheus+grafana 实现k8s服务的流控_sentinel prometheus

sentinel prometheus

一、Sentinel整合actuator

  1. 引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba.cloud</groupId>
  7. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>com.alibaba.cloud</groupId>
  11. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>io.micrometer</groupId>
  15. <artifactId>micrometer-registry-prometheus</artifactId>
  16. <scope>runtime</scope>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.alibaba.csp</groupId>
  20. <artifactId>sentinel-datasource-nacos</artifactId>
  21. </dependency>
  1. 配置yml nacos

  1. spring:
  2. application:
  3. name: xxx-service
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: localhost:8848 #Nacos服务注册中心地址
  8. sentinel:
  9. transport:
  10. dashboard: localhost:8080 #配置 dashboard监控平台地址
  11. port: 8719 #默认8719端口 如果被占用就自增直至找到未被占用的端口
  12. datasource:
  13. ds1:
  14. nacos:
  15. server-addr: localhost:8848
  16. dataId: ${spring.application.name}
  17. groupId: DEFAULT_GROUP
  18. data-type: json
  19. rule-type: flow
  20. management:
  21. endpoints:
  22. web:
  23. exposure:
  24. include: 'prometheus,health,info' //监听的端口
  1. 在nacos配置熔断限流规则

sentinel规则保存在内存里,重启数据丢失,用nacos持久化

二、Sentinel接入RocketMQ

  1. 引入 sentinel-core 依赖

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-core</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>
  1. 接受消息处理时埋点

  1. PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
  2. if (pullResult.getMsgFoundList() != null) {
  3. for (MessageExt msg : pullResult.getMsgFoundList()) {
  4. doSomething(msg);
  5. }
  6. }
  1. private static void doSomething(MessageExt message) {
  2. pool.submit(() -> {
  3. Entry entry = null;
  4. try {
  5. entry = SphU.entry("资源名");
  6. // Your business logic here.
  7. } catch (BlockException ex) {
  8. // Blocked.
  9. System.out.println("Blocked: " + FAIL_COUNT.addAndGet(1));
  10. } finally {
  11. if (entry != null) {
  12. entry.exit();
  13. }
  14. ContextUtil.exit();
  15. }
  16. });
  17. }

三、sentinel接入Prometheus的监控

  1. sentinel 的 prometheus 扩展

sentinel 提供 了 MetricExtension 接口,通过 SPI 方式引入,PrometheusExtension ,通过 boot actuator prometheus 暴露:

https://github.com/alibaba/Sentinel/pull/735

https://blog.csdn.net/u013887008/article/details/125776847?ops_request_misc=&request_id=&biz_id=102&utm_term=sentinel%20Prometheus&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-125776847.142^v73^pc_new_rank,201^v4^add_ask,239^v2^insert_chatgpt&spm=1018.2226.3001.4187
  1. prometheus.yml配置文件输入如下配置:

  1. management.endpoint.metrics.enabled=true
  2. management.endpoint.prometheus.enabled=true
  3. management.metrics.export.prometheus.enabled=true
  4. # pushgateway 地址
  5. management.metrics.export.prometheus.pushgateway.base-url=192.168.xx.xx:9091
  6. management.metrics.export.prometheus.pushgateway.push-rate=15s
  7. management.metrics.export.prometheus.pushgateway.job=${spring.application.name}
  8. management.metrics.export.prometheus.pushgateway.enabled=true

四、prometheus接入grafana

https://mp.weixin.qq.com/s?__biz=MzI0NTYzMjM0Ng==&mid=2247484477&idx=1&sn=5c4cd1448b9f94eb309610eda7d78e8a&scene=21#wechat_redirect

五、k8s接入prometheus

  1. 集群部署prometheus

  1. pod自动发现prometheus

要想自动发现集群中的 pod,也需要我们在 pod 的annotation区域添加:prometheus.io/scrape=true的声明

  1. service自动发现prometheus

要想自动发现集群中的 service,也需要我们在 service 的annotation区域添加:prometheus.io/scrape=true的声明

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/624758
推荐阅读
相关标签
  

闽ICP备14008679号