spring 云侦探和 Oauth2Template

spring cloud sleuth and Oauth2Template

我们正在使用带有 oauth2 作为安全机制的微服务。 目前我们正在使用 OAuth2RestTemplate 调用其他微服务,如下所示:

template.postForObject("http://"+MY_DISCOVERY_NAME+"/path/to/restservice", params, Void.class);

我们正在使用 @Autowired 注入 OAuth2RestTemplate,如下所示:

  @Configuration
  public class ApplicationConfig {

      @Autowired
      OAuth2RestTemplate oauth2Resttemplate;
      ...
      @Bean
      public MyBean getMyBean() {
          MyBeanImpl myBean = new MyBeanImpl();
          oauth2Resttemplate.setErrorHandler(getErrorHandler());
          myBean.setTemplate(oauth2Resttemplate);
          return myBean;
      }
      ...
  }

因此,我们的下一步是使呼叫可追踪。 我们想使用 spring 云侦探。

所以我添加了如下依赖:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>

之后 Spring 无法再自动装配 OAuth2RestTemplate:

Caused by: java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.client.OAuth2RestTemplate

在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 中抛出 IllegalArgumentException:

            @Override
            protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
                 ...

                            }
                            if (value != null) {
                                    ReflectionUtils.makeAccessible(field);
                                    field.set(bean, value);
                            }
                    }
                ...

field.set(bean, value); 导致以下异常:

java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.client.OAuth2RestTemplate field my.package.ApplicationConfig.oauth2Resttemplate to com.sun.proxy.$Proxy120

如何将 OAuth2RestTemplate 与 sleuth 结合使用?

谢谢

最大

问题是我连接了 OAuth2RestTemplate 而不是接口 OAuth2RestOperations。

连接 OAuth2RestOperations 对我有用。