我正在尝试按照本教程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": []
}
]
}
` 最好的问候,索拉夫