Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@SentinelRestTemplate fallback doesn't work and NullPointerException throws #2044

Closed
topsoft-support opened this issue Apr 20, 2021 · 2 comments · Fixed by #2146
Closed

@SentinelRestTemplate fallback doesn't work and NullPointerException throws #2044

topsoft-support opened this issue Apr 20, 2021 · 2 comments · Fixed by #2146
Labels
area/sentinel spring cloud alibaba sentinel

Comments

@topsoft-support
Copy link

我们鼓励使用英文,如果不能直接使用,可以使用翻译软件,您仍旧可以保留中文原文。
We recommend using English. If you are non-native English speaker, you can use the translation software.

Which Component
Sentinel
Describe the bug
The RestTempte configuration:

@Bean
@LoadBalanced
@SentinelRestTemplate(blockHandler = "handleBlock", blockHandlerClass = RestTemplateBlock::class, fallback = "handleFallback", fallbackClass = RestTemplateFallback::class)
open fun restTemplate(clientHttpRequestFactory: ClientHttpRequestFactory): RestTemplate {
	val restTemplate = RestTemplate(clientHttpRequestFactory)
	restTemplate.messageConverters.add(MappingJackson2HttpMessageConverterImpl())
	return restTemplate
}

fallbackClass Configuration:

class RestTemplateFallback {
    companion object {
        @JvmStatic
        fun handleFallback(request: HttpRequest, body: ByteArray, execution: ClientHttpRequestExecution, exception: BlockException): ClientHttpResponse {
            println(">>> RestTemplateFallback.handleFallback")
            return SentinelClientHttpResponse()
        }
    }
}

The service service-res doesn't exist or already offline .

 val resp = restTemplate.getForEntity("http://service-res/res/api/ms/app", Result::class.java)

The NullPointerException occured:

java.lang.NullPointerException: null
	at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:64) ~[spring-web-5.3.5.jar:5.3.5]
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:807) ~[spring-web-5.3.5.jar:5.3.5]
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777) ~[spring-web-5.3.5.jar:5.3.5]
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711) ~[spring-web-5.3.5.jar:5.3.5]
	at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:361) ~[spring-web-5.3.5.jar:5.3.5]

I trace the class SentinelProtectInterceptor.intercept
found that the exception has be catched and the IllegalStateException is not BlockException, so it do nothing.
java.lang.IllegalStateException: No instances available for service-res
image

I think that sentinel with openfeign work fine, why RestTemplate not,and I saw the SentinelInvocationHandler's process:

image

I am confused !!!

I configed the SentinelRestTemplate fallbackClass, if can't access the service ,it shouldn't run the method handleFallback?

<org.springframework.boot.version>2.4.4</org.springframework.boot.version>
<org.springframework.data.version>2.4.4</org.springframework.data.version>
<org.springframework.cloud.version>2020.0.2</org.springframework.cloud.version>
<com.alibaba.cloud.version>2021.1</com.alibaba.cloud.version>
@sczyh30 sczyh30 added the area/sentinel spring cloud alibaba sentinel label Apr 25, 2021
@ankeway
Copy link

ankeway commented Jun 1, 2021

See
#1346
4c5bf77

@theonefx
Copy link
Collaborator

theonefx commented Jul 7, 2021

My solution is to simply throw the exception when it can't be handled.

#2146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/sentinel spring cloud alibaba sentinel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants