Sentinel icon indicating copy to clipboard operation
Sentinel copied to clipboard

当添加流控规则后,控制台无法监控应用

Open wsdwk opened this issue 10 months ago • 3 comments

Bug 描述

在使用sentinel的组件时,碰到了控制台无法监控应用。

环境

环境:"java11,spring boot2.7"

sentinel的依赖是:

<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2021.0.5.0</version>
        </dependency> 

配置文件为:

spring:
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8858
        port: 8719 # Sentinel客户端暴露的监控端口,默认为8719
        client-ip: 127.0.0.1
      enabled: true

sentinel控制台与项目运行在本地电脑上。

问题详细:

在项目中编写了sentinel的流控规则后,控制台就无法监控应用,但是流控规则是生效的,就是控制台监控不了应用了,但是把流控规则去掉后就能监控应用了

Image


在代码中设置sentinel的流控与熔断规则:

@Component
public class SentinelRulesManager {

    @PostConstruct
    public void initRules() {
        initFlowRules();  // 当我注释这个流控规则后,控制台就能监控应用了
        initDegradeRules();
    }

    // 限流规则
    public void initFlowRules() {
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("listQuestionVOByPage");
        flowRule.setCount(2);
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule.setLimitApp("default");
        flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT);
        FlowRuleManager.loadRules(Collections.singletonList(flowRule));

    }

    // 降级规则
    public void initDegradeRules() {
        // 单 IP 查看题目列表熔断规则
        DegradeRule slowCallRule = new DegradeRule("listQuestionVOByPage")
                .setGrade(CircuitBreakerStrategy.SLOW_REQUEST_RATIO.getType())
                .setCount(0.2) // 慢调用比例大于 20%
                .setTimeWindow(10) // 熔断持续时间 60 秒
                .setStatIntervalMs(30 * 1000) // 统计时长 30 秒
                .setMinRequestAmount(5) // 最小请求数
                .setSlowRatioThreshold(3); // 响应时间超过 3 秒

        DegradeRule errorRateRule = new DegradeRule("listQuestionVOByPage")
                .setGrade(CircuitBreakerStrategy.ERROR_RATIO.getType())
                .setCount(0.1) // 异常率大于 10%
                .setTimeWindow(10) // 熔断持续时间 60 秒
                .setStatIntervalMs(30 * 1000) // 统计时长 30 秒
                .setMinRequestAmount(5); // 最小请求数

        // 加载规则
        DegradeRuleManager.loadRules(Arrays.asList(slowCallRule, errorRateRule));
    }
}

错误信息:

然后控制台就报错如下:

2025-04-18 12:43:20.872 ERROR 51776 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher   : Failed to fetch metric from <http://127.0.0.1:8720/metric?startTime=1744951392000&endTime=1744951398000&refetch=false> (ConnectionException: Connection refused: no further information)
2025-04-18 12:43:27.867 ERROR 51776 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher   : Failed to fetch metric from <http://127.0.0.1:8720/metric?startTime=1744951399000&endTime=1744951405000&refetch=false> (ConnectionException: Connection refused: no further information)
2025-04-18 12:43:34.873 ERROR 51776 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher   : Failed to fetch metric from <http://127.0.0.1:8720/metric?startTime=1744951406000&endTime=1744951412000&refetch=false> (ConnectionException: Connection refused: no further information)
2025-04-18 12:43:37.613 ERROR 51776 --- [pool-1-thread-1] c.a.c.s.d.client.SentinelApiClient       : HTTP request failed: http://127.0.0.1:8720/jsonTree?type=root
java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946) ~[na:na]
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvent(DefaultConnectingIOReactor.java:171) ~[httpcore-nio-4.4.6.jar!/:4.4.6]
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:145) ~[httpcore-nio-4.4.6.jar!/:4.4.6]
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:348) ~[httpcore-nio-4.4.6.jar!/:4.4.6]
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:194) ~[httpasyncclient-4.1.3.jar!/:4.1.3]
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64) ~[httpasyncclient-4.1.3.jar!/:4.1.3]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

2025-04-18 12:43:37.613 ERROR 51776 --- [nio-8858-exec-8] c.a.c.s.d.client.SentinelApiClient       : Error when fetching items from api: jsonTree -> root

java.util.concurrent.ExecutionException: java.net.ConnectException: Connection refused: no further information
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073) ~[na:na]
at com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient.fetchItems(SentinelApiClient.java:383) ~[classes!/:na]
at com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient.fetchResourceOfMachine(SentinelApiClient.java:450) ~[classes!/:na]
at com.alibaba.csp.sentinel.dashboard.controller.ResourceController.fetchResourceChainListOfMachine(ResourceController.java:70) ~[classes!/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]

wsdwk avatar Apr 18 '25 05:04 wsdwk

检查你本地的端口, 你配置的是8719, 连的却是http://127.0.0.1:8720

bryanxtong avatar Apr 22 '25 02:04 bryanxtong

解决了吗 我也遇到同样的问题 不编写sentinel的流控规则就可以被服务发现 编写之后就没了

yi-aspecta avatar Jun 13 '25 08:06 yi-aspecta

看下你程序配置的transport端口没被占用,并且能通过url访问,在dashboard中也可以看到正常显示的端口(一个下拉框显示的port)

bryanxtong avatar Jun 13 '25 08:06 bryanxtong