![](/img/trans.png)
[英]Connection Refused Error in Cloud Foundry Spring Boot application
[英]No unique service maching interface error in Spring Boot Cloud Foundry error
我正在嘗試遵循本教程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的連接正常。 如果我不使用彈簧雲連接器並從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網址。 現在我的應用失敗,出現以下錯誤
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
為此,我在application.properties中設置了"spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect"
,但它仍然無法正常工作
我還檢查了我的數據庫是否正在運行,並且服務實例中的數據庫設置正確。
以下是我的服務實例內容`
"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": []
}
]
}
最好的問候,索拉夫
如果您將spring-boot-starter-cloud-connectors
作為依賴項包括在內,那么Spring Cloud Connectors將嘗試創建連接數據庫所需的java.sql.DataSource
bean。 連接器通過查找具有某些特征的服務綁定來實現此目的。 “ No unique service matching interface javax.sql.DataSource found
消息表示連接器找不到適當類型的服務綁定。
如果要在該示例應用程序中使用連接器,則需要使用Postgres連接的詳細信息創建用戶提供的服務實例 ,並將該服務實例綁定到該應用程序。
或者,你可以刪除spring-boot-starter-cloud-connectors
的依賴,轉而使用配置數據庫連接spring.datasource
在性能application.yml
。
這些選項在博客文章中有更詳細的說明。
只是為了總結其他讀者的整體解決方案。
如果您想使用Spring Boot應用程序,請與數據庫(位於其他計算機上)對話,然后從cloud Foundry運行該應用程序。 然后按照以下一些技巧進行操作。
步驟3和4是我犯的錯誤。 更正后,該應用程序將正常運行。
最好的問候,索拉夫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.