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 won't add interceptor when RestTemplate annotated by @Bean annotation and construct in @Configuration class #227

Closed
jsjchai opened this issue Dec 30, 2018 · 13 comments
Assignees
Labels
area/sentinel spring cloud alibaba sentinel kind/bug Mark as bug
Milestone

Comments

@jsjchai
Copy link

jsjchai commented Dec 30, 2018

RestTemplate配置
image
调用接口("http://test-provider/test"是一个不存在的地址)
image
debug 后发现restTemplate对象中没有SentinelProtectInterceptor过滤器
image
image

spring cloud aliliba version 0.2.1.RELEASE
spring cloud version Finchley.RELEASE

@fangjian0423
Copy link
Contributor

Hi, which spring boot version are you using?

Spring Cloud Finchley is compatibility with 2.0.X

@zhendong590
Copy link
Contributor

please show flowrule.json's content

@jsjchai
Copy link
Author

jsjchai commented Dec 30, 2018

compatibility

spring boot version 2.0.6.RELEASE
image

@jsjchai
Copy link
Author

jsjchai commented Dec 30, 2018

how flowrule.json's content

没有配置限流规则,配置了一个不存在url

@zhendong590
Copy link
Contributor

zhendong590 commented Dec 30, 2018

how flowrule.json's content

没有配置限流规则,配置了一个不存在url

You can have a try

step1: add sentinel settings in application.properties

spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

step 2: add flowrule.json

[
 {
    "resource": "http://test-provider/test",
    "controlBehavior": 0,
    "count": 0,
    "grade": 1,
    "limitApp": "default",
    "strategy": 0
  }
]

step3: restart application
step4:curl localhost:{server.port}/testException

@fangjian0423
Copy link
Contributor

Hi @jsjchai .

Maybe you can see this example: https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example

restTemplate have SentinelProtectInterceptor, you can try to debug to verify it.

I think maybe you write wrong logic somewhere.

@jsjchai
Copy link
Author

jsjchai commented Dec 30, 2018

flowrule.json的内容如何

没有配置限流规则,配置了一个不存在的URL

你可以尝试一下

步骤1:在application.properties中添加sentinel设置

spring.cloud.sentinel.datasource.ds1.file.file = classpath:flowrule.json
 spring.cloud.sentinel.datasource.ds1.file.data-type = json
 spring.cloud.sentinel.datasource.ds1.file.rule- type = flow

第2步:添加flowrule.json

[
 {
    “ resource ”: “ http:// test-provider / test ”,
     “ controlBehavior ”: 0,
     “ count ”: 0,
     “ grade ”: 1,
     “ limitApp ”: “ default ”,
     “ strategy ”: 0
  }
]

step3:重启应用程序
step4:curl localhost:{server.port} / testException

应该不是这个问题,启动时候没有加载到SentinelProtectInterceptor
https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinel#resttemplate-%E6%94%AF%E6%8C%81

@jsjchai
Copy link
Author

jsjchai commented Dec 30, 2018

Hi @jsjchai .

Maybe you can see this example: https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example

restTemplate have SentinelProtectInterceptor, you can try to debug to verify it.

I think maybe you write wrong logic somewhere.

官方源码中的例子我运行成功了,自己写的demo没有加载到SentinelProtectInterceptor,这个过滤器是什么时候加载的

@fangjian0423
Copy link
Contributor

@jsjchai SentinelBeanPostProcessor, you can try to debug in it.

@jsjchai
Copy link
Author

jsjchai commented Dec 30, 2018

SentinelBeanPostProcessor

image

我获取到的beanDefinition.getSource()是 org.springframework.core.type.classreading.MethodMetadataReadingVisitor
不懂spring源码,求大佬解释一下我拿到为什么是MethodMetadataReadingVisitor,而不是StandardMethodMetadata

@jsjchai
Copy link
Author

jsjchai commented Dec 30, 2018

我在类上面添加了@configuration这个注解, 注入 的restTemplate 自动转化成 MethodMetadataReadingVisitor而不是StandardMethodMetadata,放到@SpringBootApplication这个注解下注入就是StandardMethodMetadata

@fangjian0423
Copy link
Contributor

Hi @jsjchai .

You can define it in a class annotated by @SpringBootApplication.

I will see why it happened and fix it next version.

@fangjian0423 fangjian0423 self-assigned this Dec 30, 2018
@fangjian0423
Copy link
Contributor

我在类上面添加了@configuration这个注解, 注入 的restTemplate 自动转化成 MethodMetadataReadingVisitor而不是StandardMethodMetadata,放到@SpringBootApplication这个注解下注入就是StandardMethodMetadata

A bean annotated by @SpringBootApplication scan by AnnotatedBeanDefinitionReader.

AnnotatedBeanDefinitionReader build AnnotatedGenericBeanDefinition when register bean.

AnnotatedGenericBeanDefinition using StandardAnnotationMetadata to assign to its property metadata in constructor.

This is an entrance bean of Spring Boot Application.

A bean annotated by @Configuration will register in ConfigurationClassParser and this parser will using ClassPathBeanDefinitionScanner to scan components with @ComponentScans or @ComponentScan annotation.

Spring using SimpleMetadataReader to get annotationMetadata in ClassPathBeanDefinitionScanner#doScan. This annotationMetadata is AnnotationMetadataReadingVisitor.

SimpleMetadataReader is MetadataReader implementation based on an ASM org.springframework.asm.ClassReader.

@fangjian0423 fangjian0423 changed the title 使用@SentinelRestTemplate注解异常时未触发handleException方法 @SentinelRestTemplate won't add interceptor when RestTemplate annotated by @Bean annotation and construct in @Configuration class Jan 3, 2019
flystar32 added a commit that referenced this issue Jan 4, 2019
@fangjian0423 fangjian0423 added this to the 0.2.2 milestone Jan 9, 2019
@fangjian0423 fangjian0423 added area/sentinel spring cloud alibaba sentinel kind/bug Mark as bug labels Mar 22, 2019
theonefx pushed a commit to theonefx/spring-cloud-alibaba that referenced this issue Aug 2, 2021
theonefx pushed a commit to theonefx/spring-cloud-alibaba that referenced this issue Aug 2, 2021
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 kind/bug Mark as bug
Projects
None yet
Development

No branches or pull requests

3 participants