3

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

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 configuaration 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,并且我应该能够使用它来为 DataSource 添加更多配置以进行连接池。

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

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 中、资源中等)

这里有什么帮助吗?

4

3 回答 3

9

在最近的一个项目中,我们遇到了同样的问题。在云中一切正常,但在本地抛出错误“未找到合适的云连接器”。为了排除依赖问题,在我们的例子中(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 变量是一种反模式,但在我们的用例中,这正是使一切在本地尽可能接近云环境的解决方案。

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

于 2018-03-15T16:57:12.023 回答
1

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

于 2020-01-22T21:19:44.537 回答
0

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

据我分析,当缺少此功能时,Cloud-Foundry 连接器会覆盖/覆盖 Local-Connector 中的 ServiceLoaders。

当然,所有这一切都只有在您尝试运行本地构建的包时。直接来自 IDE(在我的例子中是 IntelliJ)它总是有效的。

于 2017-05-22T14:19:07.443 回答