@SentinelRestTemplate注解未生效
- Dominant language
- Java
- Stars
- 23.1k
- Forks
- 8.1k
- PR merge metrics
- No merged PRs in 30d
Description
## Issue Description
@SentinelRestTemplate 注解未生效
请求的地址服务端关闭后,测试请求抛异常退出了,并没有走熔断处理
抛出异常
```
java.lang.NullPointerException
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:55)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:414)
```
如果去掉@SentinelRestTemplate,抛出 Connection timed out异常
### Describe what happened (or what feature you want)
- 配置类
```java
@Configuration
public class RestClientConfig {
@Bean
@SentinelRestTemplate(blockHandlerClass = FuseHandlerUtil.class, fallbackClass = FuseHandlerUtil.class,
blockHandler = "blockHandleException", fallback = "fallBackHandleException")
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
```
- resttemplate封装
```java
@Component
public class RestClient {
@Autowired
private RestTemplate restTemplate;
public String sentPost() {
String resp = restTemplate.postForObject("http://localhost:8080/hello/normal",null,
String.class, new Object());
return resp;
}
}
```
- 熔断处理
```java
public final class FuseHandlerUtil {
private static Logger logger = LoggerFactory.getLogger(FuseHandlerUtil.class);
private FuseHandlerUtil(){}
public static ClientHttpResponse blockHandleException(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution, BlockException exception) {
logger.info("触发了限流处理");
try {
return execution.execute(request, body);
} catch (IOException e) {
logger.info("限流处理失败");
logger.error(e.getMessage());
return null;
}
}
public static ClientHttpResponse fallBackHandleException(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution, BlockException exception) {
logger.info("触发了降级处理");
try {
return execution.execute(request, body);
} catch (IOException e) {
logger.info("降级处理失败");
logger.error(e.getMessage());
return null;
}
}
}
```
- 测试类
```java
@SpringBootTest
@RunWith(SpringRunner.class)
public class RestClientTest {
Logger logger = LoggerFactory.getLogger(RestClientTest.class);
@Autowired
private RestClient restClient;
@Test
public void clientTest() throws InterruptedException {
logger.info("客户端测试");
for(int i = 0; i < 100; i++) {
String rs = restClient.sentPost();
logger.info("result:{}", rs);
Thread.sleep(1000);
}
}
}
```
### Tell us your environment
jdk1.8
spring boot 2.1.6.RELEASE
spring web 5.1.8.RELEASE
sentinel 2.1.0.RELEASE
Contributor guide
Research direction
Reproduce the failure from RestClientTest using the RestClientConfig bean and its @SentinelRestTemplate annotation, with the unavailable localhost service described in the issue. Trace why the configured block and fallback handlers do not handle the request error; done means the request reaches the expected fallback or circuit-breaking behavior instead of terminating with the shown exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100