简体   繁体   English

无法注入LoadBalanceed带注释的OAuth2RestTemplate

[英]Cannot inject LoadBalanced annotated OAuth2RestTemplate

I am using Spring Cloud Angel.SR4. 我正在使用Spring Cloud Angel.SR4。 My Configuration class for creating an OAuth2RestTemplate bean is as follows: 我的用于创建OAuth2RestTemplate bean的Configuration类如下:

@Configuration
public class OAuthClientConfiguration {
    @Autowired
    private MyClientCredentialsResourceDetails resource;

    public OAuthClientConfiguration() {
    }

    @Bean
    @Qualifier("MyOAuthRestTemplate")
    public OAuth2RestTemplate restTemplate() {
        return new OAuth2RestTemplate(this.resource);
    }
}

This configuration is totally fine since I am using this RestTemplate in a Feign RequestInterceptor for injecting access tokens to the feign requests. 因为我在Feign RequestInterceptor使用此RestTemplate来为假请求注入访问令牌,所以此配置完全正常。 The problem is that when I annotate an autowired OAuth2RestTemplate with @LoadBalanced the dependency injection engine raises a NoSuchBeanDefinitionException exception. 问题是,当我注释的自动装配Autowired OAuth2RestTemplate@LoadBalanced依赖注入引擎引发一个NoSuchBeanDefinitionException例外。 For example, the following would raise an exception: 例如,以下将引发异常:

@LoadBalanced
@Autowired
@Qualifier("MyOAuthRestTemplate")
private OAuth2RestTemplate restTemplate;

and when I remove the @LoadBalanced , everything works fine. 当我删除@LoadBalanced ,一切正常。 What is wrong with @LoadBalanced ? @LoadBalanced什么问题? Do I need any additional configurations (I already have @EnableEurekaClient )? 我是否需要任何其他配置(我已经拥有@EnableEurekaClient )?

I found a workaround. 我找到了解决方法。 The problem was that I had misunderstood the @LoadBalanced annotation. 问题是我误解了@LoadBalanced批注。 This is just a qualifier for the auto-created load-balanced RestTemplate bean, and it would not create a proxy around the annotated RestTemplate for injecting the load balancing capability. 这只是自动创建的负载平衡RestTemplate bean的RestTemplate ,它不会在带注释的RestTemplate周围创建代理以注入负载平衡功能。

After seeing this https://github.com/spring-cloud/spring-cloud-commons/blob/v1.0.3.RELEASE/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerAutoConfiguration.java , I revised my OAuth2RestTemplate bean definition as follows, and it solved the problem. 看到这个https://github.com/spring-cloud/spring-cloud-commons/blob/v1.0.3.RELEASE/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer之后/LoadBalancerAutoConfiguration.java ,我如下修改了OAuth2RestTemplate bean定义,它解决了这个问题。

@Bean
@Qualifier("MyOAuthRestTemplate")
public OAuth2RestTemplate restTemplate(RestTemplateCustomizer customizer) {
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(this.resource);
    customizer.customize(restTemplate);
    return restTemplate;
}

I used @LoadBalanced with restTemplate in spring cloud with ribbon behind the scenes. 我在春天的云中使用@LoadBalanced和restTemplate在幕后使用功能区。

adding @LoadBalanced in the bean definition it works like this: 在bean定义中添加@LoadBalanced,其工作方式如下:

in my class i have 在我班上,我有

@Autowired  
@LoadBalanced  
@Qualifier("bookRepositoryServiceRestTemplate") private RestTemplate bookRepositoryServiceRestTemplate;

and in my configuration class i have: 在我的配置类中,我有:

@Configuration
public class ServiceConfig {

    @Bean
    @LoadBalanced
    public RestTemplate bookRepositoryServiceRestTemplate(SpringClientFactory clientFactory, LoadBalancerClient loadBalancer){
        RibbonClientHttpRequestFactory ribbonClientHttpRequestFactory = new RibbonClientHttpRequestFactory(clientFactory,loadBalancer);
        return new RestTemplate(ribbonClientHttpRequestFactory);
    }
    ....

}

this works for me 这对我有用

I hope that this can help 我希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM