java - Spring 执行器 : Error configuring CloudFoundryActuator with multiple custom RestTemplateBuilder beans

标签 java spring spring-boot cloud-foundry spring-boot-actuator

我们在 java @Configuration 注解的类中配置两个单独的 RestTemplateBuilder bean,如下所示:

@Bean("internal_api")
public RestTemplateBuilder internalRestTemplateBuilder(@Autowired 
OAuthConsumerOperations oAuthConsumerOperations) {
...    
}

@Bean("external_api")
public RestTemplateBuilder externalRestTemplateBuilder(){
...
}

当我们将应用程序部署到 Pivotal Cloud Foundry Apps Manager 时,我们收到以下错误。它提示说,当它只需要一个 RestTemplateBuilder bean 时,却发现了多个 RestTemplateBuilder bean。

2018-08-28T11:59:17.67-0400 [APP/PROC/WEB/0] OUT {"timeMillis":1535471957677,"thread":"main","level":"WARN","loggerName":“org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext”,“message”:“上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“documentationPluginsBootstrapper”的bean时出错URL [jar:file:/home/vcap/app/BOOT-INF/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]:表达了不满足的依赖关系通过构造函数参数1;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建URL [jar:file:/home/vcap/app/BOOT-INF/lib/springfox-spring 中定义的名称为“webMvcRequestHandlerProvider”的bean时出错-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]:通过构造函数参数0表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建在类路径资源 [org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfiguration.class] 中定义的名为“cloudFoundryEndpointHandlerMapping”的 bean 时出错:通过方法“cloudFoundryEndpointHandlerMapping”表达的依赖关系不满足参数1;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为“org.springframework.boot.web.client.RestTemplateBuilder”的合格 bean 可用:期望单个匹配 bean,但发现 2:internal_api,external_api","endOfBatch": false,"loggerFqcn":"org.apache.commons.logging.impl.SLF4JLocationAwareLog","contextMap":{},"threadId":1,"threadPriority":5}

这是一个错误吗?在Cloud Foundry上使用Spring Actuator时,不是都允许配置多个RestTemplateBuilder bean吗?有办法解决这个错误吗?如果没有,创建 RestTemplate beans 而不是 RestTemplateBuilders 可以吗?如果是这种情况,那么在 Cloud Foundry 上部署时,CloudFoundryActuatorAutoConfiguration 类是否需要至少定义一个 RestTemplateBuilder bean?

最佳答案

我的 0.02 美元,但我认为你不应该创建 RestTemplateBuilder 实例。 RestTemplateBuilder 的文档说:

In a typical auto-configured Spring Boot application this builder is available as a bean and can be injected whenever a RestTemplate is needed.

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/client/RestTemplateBuilder.html

如果您的应用程序能够“典型”,那就最好了,除非有充分的理由,否则我不会偏离这一点。所以我认为你想做这样的事情:

// this is created & injected by Spring Boot
@Autowired
private RestTemplateBuilder restTemplateBuilder;

@Bean("internal_api")
public RestTemplate internalRestTemplate(@Autowired OAuthConsumerOperations oAuthConsumerOperations) {
    return restTemplateBuilder.build();  // add additional config
}

@Bean("external_api")
public RestTemplate externalRestTemplate(){
    return restTemplateBuilder.build();  // add additional config
}

具体解决您的问题:

Is this a bug? Are you not all allowed to configure multiple RestTemplateBuilder beans when using spring actuator on Cloud Foundry? Is there a way around this error?

我不能明确地说,但 Actuator 似乎有一个前提条件,即期望只存在一个。它应该/可以支持多个案例吗?也许吧,但目前似乎还没有这样做。

If not, would creating RestTemplate beans instead of RestTemplateBuilders work?

我相信这是正确的方法,见上文。

If that is the case, then does the CloudFoundryActuatorAutoConfiguration class require at least one RestTemplateBuilder bean to be defined when deployed on Cloud Foundry?

不,看起来 Spring Boot 会为你定义一个。不过,如果需要,您可以覆盖并创建自己的。

关于java - Spring 执行器 : Error configuring CloudFoundryActuator with multiple custom RestTemplateBuilder beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52079819/

相关文章:

java - 删除后 TreeMap 迭代器行为不一致

java - Spring MVC :How to get file path into Controller?

java - Spring - 将自定义实例传递给构造函数

java - Spring JPA 查询只有一个参数,不区分大小写,但有多个参数

java - 具有子状态的 Spring 状态机生成器抛出错误 - "Initial state not set"

java - 覆盖 Azure 应用程序配置存储中的属性

JavaFX线程与Java线程同步

java - XSD xs :choice shouldn't allow empty choice

java - 在文件中搜索java中txt文件中的特定句子

java - 在 spring-boot 中为 Graphite 千分尺事件添加前缀