Spring执行器:使用多个自定义 RestTemplateBuilder bean 配置 CloudFoundryActuator 时出错
Spring Actuator: Error configuring CloudFoundryActuator with multiple custom RestTemplateBuilder beans
我们在 java @Configuration 注解 class 中配置两个单独的 RestTemplateBuilder bean,如下所示:
@Bean("internal_api")
public RestTemplateBuilder internalRestTemplateBuilder(@Autowired
OAuthConsumerOperations oAuthConsumerOperations) {
...
}
@Bean("external_api")
public RestTemplateBuilder externalRestTemplateBuilder(){
...
}
当我们将应用程序部署到 Pivotal Cloud Foundry Apps Manager 时,出现以下错误。它抱怨说发现了不止一个 RestTemplateBuilder bean,而它只需要一个。
2018-08-28T11:59:17.67-0400 [APP/PROC/WEB/0] 输出 {"timeMillis":1535471957677,"thread":"main","level" :"WARN","loggerName":"org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext","message":"Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/home/vcap/app/BOOT-INF/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/home/vcap/app/BOOT-INF/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cloudFoundryEndpointHandlerMapping' defined in class path resource [org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cloudFoundryEndpointHandlerMapping' parameter 1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' available: expected single matching bean but found 2: internal_api,external_api","endOfBatch":false,"loggerFqcn":"org.apache.commons.logging.impl.SLF4JLocationAwareLog" ,"contextMap":{},"threadId":1,"threadPriority":5}
这是一个错误吗?在 Cloud Foundry 上使用 spring actuator 时,不是都允许配置多个 RestTemplateBuilder beans 吗?有没有办法解决这个错误?如果不是,创建 RestTemplate bean 代替 RestTemplateBuilders 会起作用吗?如果是这样,那么 CloudFoundryActuatorAutoConfiguration class 是否需要在 Cloud Foundry 上部署时至少定义一个 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.
最好是您的应用可以 "typical",除非有充分的理由,否则我不会偏离这一点。所以我想你会想做这样的事情:
// 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 有一个前提条件,即期望只有一个存在。 Should/could 它支持多个案例?也许吧,但目前似乎没有这样做。
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 @Configuration 注解 class 中配置两个单独的 RestTemplateBuilder bean,如下所示:
@Bean("internal_api")
public RestTemplateBuilder internalRestTemplateBuilder(@Autowired
OAuthConsumerOperations oAuthConsumerOperations) {
...
}
@Bean("external_api")
public RestTemplateBuilder externalRestTemplateBuilder(){
...
}
当我们将应用程序部署到 Pivotal Cloud Foundry Apps Manager 时,出现以下错误。它抱怨说发现了不止一个 RestTemplateBuilder bean,而它只需要一个。
2018-08-28T11:59:17.67-0400 [APP/PROC/WEB/0] 输出 {"timeMillis":1535471957677,"thread":"main","level" :"WARN","loggerName":"org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext","message":"Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/home/vcap/app/BOOT-INF/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/home/vcap/app/BOOT-INF/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cloudFoundryEndpointHandlerMapping' defined in class path resource [org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cloudFoundryEndpointHandlerMapping' parameter 1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' available: expected single matching bean but found 2: internal_api,external_api","endOfBatch":false,"loggerFqcn":"org.apache.commons.logging.impl.SLF4JLocationAwareLog" ,"contextMap":{},"threadId":1,"threadPriority":5}
这是一个错误吗?在 Cloud Foundry 上使用 spring actuator 时,不是都允许配置多个 RestTemplateBuilder beans 吗?有没有办法解决这个错误?如果不是,创建 RestTemplate bean 代替 RestTemplateBuilders 会起作用吗?如果是这样,那么 CloudFoundryActuatorAutoConfiguration class 是否需要在 Cloud Foundry 上部署时至少定义一个 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.
最好是您的应用可以 "typical",除非有充分的理由,否则我不会偏离这一点。所以我想你会想做这样的事情:
// 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 有一个前提条件,即期望只有一个存在。 Should/could 它支持多个案例?也许吧,但目前似乎没有这样做。
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 会为您定义一个。不过,如果需要,您可以覆盖并创建自己的。