繁体   English   中英

尝试使用本地 spring 云连接器/local_configuration_connector 时找不到合适的云连接器

[英]No suitable cloud connector found while trying to use local spring cloud connector/local_configuration_connector

我正在尝试使用本地云 spring 连接器在本地环境中测试我的应用程序,然后才能将其部署到基于 CF 的云环境。 来自 Spring 链接

http://cloud.spring.io/spring-cloud-connectors/spring-cloud-connectors.html#_local_configuration_connector

我按照这个过程在项目资源目录中创建了名为 spring-cloud-bootstrap.properties 的属性文件。 它有以下内容

spring.cloud.properties 文件:C:\\Users\\IBM_ADMIN\\git\\ServiceXchange5\\ServiceXchange\\spring-cloud.properties

我确实在上面给出的路径中有文件 spring-cloud.properties。

从 spring 配置 bean 我有以下内容

@EnableWebMvc
@Configuration
@EnableAspectJAutoProxy
public class CloudServiceConfig extends AbstractCloudConfig {

@Bean
public DataSource getDataSource() throws AppException  {

org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig poolConfig = new PoolConfig(50, 100, 3000);
org.springframework.cloud.service.relational.DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, null);
return connectionFactory().dataSource(SX_DB_USED, dbConfig);

}

现在这个 DataSource bean 被注入到其他各个地方。 使用适当的属性文件,我希望将创建用于本地配置的云连接器 bean,我应该能够使用它为连接池的数据源添加更多配置。

但是,当我访问该应用程序时,它似乎没有激活本地配置连接器本身。

Initialization of bean failed; nested exception is org.springframework.cloud.CloudException: No suitable cloud connector found
[ERROR   ] SRVE0271E: Uncaught init() exception created by servlet [appServlet] in application [ServiceXchange]:  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cloudServiceConfig' defined in file [C:\Users\IBM_ADMIN\git\ServiceXchange5\ServiceXchange\target\classes\com\hcl\service\config\CloudServiceConfig.class]: Initialization of bean failed; nested exception is org.springframework.cloud.CloudException: No suitable cloud connector found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:2388)
at [internal classes]
Caused by: org.springframework.cloud.CloudException: No suitable cloud connector found
at org.springframework.cloud.CloudFactory.getCloud(CloudFactory.java:55)
at org.springframework.cloud.config.java.AbstractCloudConfig.setBeanFactory(AbstractCloudConfig.java:85)
at com.hcl.service.config.CloudServiceConfig$$EnhancerBySpringCGLIB$$9529c032.CGLIB$setBeanFactory$54(<generated>)
at com.hcl.service.config.CloudServiceConfig$$EnhancerBySpringCGLIB$$9529c032$$FastClassBySpringCGLIB$$6c6301dd.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanFactoryAwareMethodInterceptor.intercept(ConfigurationClassEnhancer.java:277)
at com.hcl.service.config.CloudServiceConfig$$EnhancerBySpringCGLIB$$9529c032.setBeanFactory(<generated>)
at org.springframework.context.annotation.ConfigurationClassPostProcessor$EnhancedConfigurationBeanPostProcessor.postProcessPropertyValues(ConfigurationClassPostProcessor.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
... 13 more

我不确定为什么未激活本地云连接器。 根据链接,它应该在检测到适当的属性文件后。

注意:我尝试将属性文件放在不同的位置,例如(直接在应用程序根目录下、web-inf/lib 中、资源中等)

有什么帮助吗?

在最近的一个项目中,我们遇到了同样的问题。 在云中一切正常,但在本地抛出错误“找不到合适的云连接器”。 为了排除依赖问题,在我们的例子中(Spring Boot 微服务要部署在 CloudFoundry 环境中)以下两个依赖(依赖管理管理的版本)就足够了。

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-spring-service-connector</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
</dependency>

应该使用的 CloudFoundryConnector 检查是否设置了环境变量VCAP_APPLICATION ,然后才将自己标记为合适,否则它什么都不做。 通常认为在本地环境中复制 VCAP_SERVICES 和 VCAP_APPLICATION 变量是一种反模式,但在我们的用例中,这正是使一切在本地尽可能接近云环境的解决方案。

对于最初的问题,这个答案可能为时已晚,但作为其他碰巧遇到相同问题的迷失灵魂的起点,它有望为其他人节省我们花费的调试时间。

如果您在本地运行,那么您可能无法获得 Cloud 实例,从而导致这种情况。 需要确保它是用于本地还是在云上,具体取决于使用情况。 就我而言,这是调用它的位置的问题。 因此,当部署在云上时,我添加了一个额外的方法来实例化 cloudfoundry (CloudFactory().getCloud())。

您是否使用 maven shade 插件来打包您的应用程序? 如果是这样,请确保有<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>到位。

据我分析,何时缺少 Cloud-Foundry 连接器覆盖/覆盖来自本地连接器的 ServiceLoaders。

当然,只有当您尝试运行本地构建的程序包时,才可以执行所有这些操作。 直接从 IDE(在我的例子中是 IntelliJ)它总是有效的。

暂无
暂无

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

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