1

我正在尝试按照本教程https://github.com/cf-platform-eng/spring-boot-cities/tree/master/cities-service在 Cloud Foundry 中运行我的 Spring Boot 应用程序。我有本地 bosh-lite 安装

我的 Spring Boot 应用程序连接到 postgresql 作为数据库。我正在尝试连接到在我的本地(主机)上运行的 Postgresql 实例

如果部署到本地 tomcat,我的应用程序在本地运行良好。但是,当我将应用程序部署到 bosh-lite 时,它​​会失败并出现以下错误“没有唯一的服务处理接口错误”。我的代码与教程中提到的完全一样,除了我使用 Maven 来构建我的代码。

    2017-06-07T22:29:54.44+0530 [App/0] OUT Caused by: org.springframework.cloud.CloudException: No unique service matching interface javax.sql.DataSource found. Expected 1, found 0
2017-06-07T22:29:54.44+0530 [App/0] OUT     at org.springframework.cloud.Cloud.getSingletonServiceConnector(Cloud.java:149) ~[spring-cloud-core-1.2.3.RELEASE.jar!/:na]
2017-06-07T22:29:54.44+0530 [App/0] OUT     at org.saurav.cf.casestudy.employee.db.DataSourceConfiguration.dataSource(DataSourceConfiguration.java:20) ~[classes/:na]
2017-06-07T22:29:54.44+0530 [App/0] OUT     at org.saurav.cf.casestudy.employee.db.DataSourceConfiguration$$EnhancerBySpringCGLIB$$847aab5e.CGLIB$dataSource$0(<generated>) ~[classes/:na]
2017-06-07T22:29:54.44+0530 [App/0] OUT     at org.saurav.cf.casestudy.employee.db.DataSourceConfiguration$$EnhancerBySpringCGLIB$$847aab5e$$FastClassBySpringCGLIB$$2ad81e2b.invoke(<generated>) ~[classes/:na]
2017-06-07T22:29:54.44+0530 [App/0] OUT     at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
2017-06-07T22:29:54.44+0530 [App/0] OUT     at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
2017-06-07T22:29:54.44+0530 [App/0] OUT     at org.saurav.cf.casestudy.employee.db.DataSourceConfiguration$$EnhancerBySpringCGLIB$$847aab5e.dataSource(<generated>) ~[classes/:na]
2017-06-07T22:29:54.44+0530 [App/0] OUT     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]

我与 postgres 的连接工作正常。如果我不使用 spring cloud 连接器并从 application.properties 中正常读取值,则相同的应用程序可以正常工作。

我的 pom.xml`(具有 spring boot 云连接器和 postgrsql 驱动程序的依赖项)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.saurav.cf.casestudy</groupId>
    <artifactId>employeerest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>employeerest</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!--  <start-class>org.saurav.cf.casestudy.employee.EmployeerestApplication</start-class> -->
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

</dependency>



        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
      </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

`

[更新] 根据 Scott 在下面的回答,我查看了获取云连接器的 UPS 的要求。因为我的是用户提供的服务,所以我不能有标签和标签。但我添加了 jdbc url。现在我的应用程序因以下错误而失败

 Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

为此,我已"spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect" 在我的 application.properties 中进行了设置,但仍然无法正常工作

我还检查了我的数据库是否正在运行并且我的服务实例中的数据库设置是否正确。

下面是我的服务实例内容`

"VCAP_SERVICES": {
  "user-provided": [
   {
    "credentials": {
     "jdbcUrl": "jdbc:postgresql://10.0.2.2:5432/employee",
     "username": "postgres",
     "password": "admin",
     "postgresUri": "postgresql://10.0.2.2:5432/employee",
     "uri": "postgresql://10.0.2.2:5432/employee"
    },
    "label": "user-provided",
    "name": "postgresql-cf-service",
    "syslog_drain_url": "",
    "tags": [],
    "volume_mounts": []
   }
  ]
 }

` 最好的问候,索拉夫

4

2 回答 2

1

只是为其他读者总结整体解决方案。

如果您有一个场景,您希望让 Spring Boot 应用程序与数据库(托管在其他机器上)对话,然后从 Cloud Foundry 运行该应用程序。然后按照以下提示进行操作。

  1. 一旦您部署了 Spring Boot 应用程序并希望使用 Spring Boot 云连接器,请确保您的类路径中具有所有正确的依赖项(spring-boot-cloud-connector 和 jar.
  2. 如果您想将服务实例与应用程序绑定。确保凭据符合此规范http://cloud.spring.io/spring-cloud-connectors/spring-cloud-cloud-foundry-connector.html#_postgresql
  3. 在服务实例参数中确保使用 uri 参数不依赖于用户名和密码来构造 uri
  4. 确保您的数据库实例正在实际运行,并且您已创建应用程序服务组以允许来自云代工应用程序的出站通信。https://docs.cloudfoundry.org/concepts/asg.html#viewing

第 3 步和第 4 步是我犯的错误。更正后,应用程序工作正常。

最好的问候, 索拉夫

于 2017-06-08T15:49:45.487 回答
1

如果您将其包含spring-boot-starter-cloud-connectors为依赖项,则 Spring Cloud 连接器将尝试创建java.sql.DataSource连接到数据库所需的 bean。连接器通过寻找具有某些特征的服务绑定来做到这一点。该消息意味着连接器找不到适当类型的服务绑定。No unique service matching interface javax.sql.DataSource found

如果您想在该示例应用程序中使用连接器,那么您需要使用 Postgres 连接的详细信息创建用户提供的服务实例,并将该服务实例绑定到应用程序。

或者,您可以删除spring-boot-starter-cloud-connectors依赖项,而是spring.datasource使用application.yml.

这些选项在博客文章中有更详细的解释。

于 2017-06-07T18:50:13.670 回答