Java Spring Boot如何了解Heroku';s数据库的URL配置变量?
短篇小说: Spring Boot如何准确解析Java Spring Boot如何了解Heroku';s数据库的URL配置变量?,java,spring,postgresql,spring-boot,heroku,Java,Spring,Postgresql,Spring Boot,Heroku,短篇小说: Spring Boot如何准确解析数据库URL配置变量?在本地PC上设置同名的环境属性无效。Spring启动库中涉及哪些类?对DATABASE\u URL进行全文搜索时不返回任何内容 说来话长: 我最近在Heroku上部署了一个简单的Spring Boot+JPA应用程序。My heroku已经配置了Postgres附加组件。 在第一次部署期间,我只想确认应用程序是否正确构建和启动。但令我惊讶的是,应用程序立即连接到了在Heroku的数据库URL配置变量中配置的Postgres数据库
数据库URL
配置变量?在本地PC上设置同名的环境属性无效。Spring启动库中涉及哪些类?对DATABASE\u URL
进行全文搜索时不返回任何内容
说来话长:
我最近在Heroku上部署了一个简单的Spring Boot+JPA应用程序。My heroku已经配置了Postgres附加组件。
在第一次部署期间,我只想确认应用程序是否正确构建和启动。但令我惊讶的是,应用程序立即连接到了在Heroku的数据库URL
配置变量中配置的Postgres数据库:
2018-11-30T09:28:51.054688+00:00 app[web.1]: 2018-11-30 09:28:51.054 INFO 4 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
这就引出了上面已经发布的问题
我使用的是Spring boot 2.1.0,一个由创建的,带有Web
+JPA
+H2
+PostgreSQL
<包括代码>H2,因为:
- 我想用嵌入式H2运行单元测试
- 大多数时候,我希望在本地运行嵌入H2的应用程序,但有时,我希望切换到Heroku的Postgres,以便验证某些特定于DB的内容(例如迁移)
spring.profiles.active=h2
spring.datasource.url= # H2 URL of the database.
spring.datasource.driver-class-name=# H2 JDBC driver. Auto-detected based on the URL by default.
spring.datasource.password= # Login password of the database.
spring.datasource.username= # Login username of the database.
spring.profiles.active=heroku
spring.datasource.url= # herokuURL of the database.
spring.datasource.driver-class-name=# herokuJDBC driver. Auto-detected based on the URL by default.
spring.datasource.password= # Login password of the database.
spring.datasource.username= # Login username of the database.
运行spring boot应用程序时,通过将h2配置文件设置为on激活:
java -jar -Dspring.profiles.active=h2 myApplication.jar
java -jar -Dspring.profiles.active=heroku myApplication.jar
名为的heroku属性文件应用程序heroku.properties:
spring.profiles.active=h2
spring.datasource.url= # H2 URL of the database.
spring.datasource.driver-class-name=# H2 JDBC driver. Auto-detected based on the URL by default.
spring.datasource.password= # Login password of the database.
spring.datasource.username= # Login username of the database.
spring.profiles.active=heroku
spring.datasource.url= # herokuURL of the database.
spring.datasource.driver-class-name=# herokuJDBC driver. Auto-detected based on the URL by default.
spring.datasource.password= # Login password of the database.
spring.datasource.username= # Login username of the database.
运行spring boot应用程序时,通过将heroku配置文件设置为on激活:
java -jar -Dspring.profiles.active=h2 myApplication.jar
java -jar -Dspring.profiles.active=heroku myApplication.jar
Spring不知道
数据库的URL
(据我所知)。但是它确实检测并自动使用Heroku Java buildpack在运行时设置的SPRING\u DATASOURCE\u URL
您可以在Heroku的文档中阅读有关此环境变量的更多信息,我将回答我自己的问题,用Heroku设置的环境属性的更多细节修改已接受的答案 假设Heroku应用程序包含环境变量:
=数据库\u URL
postgres://username:password@主机:端口/数据库
=JDBC\u数据库\u URL
JDBC:postgresql://host:port/database?user=username&password=password&sslmode=require
=JDBC\u数据库\u用户名
USERNAME
=JDBC\u数据库\u密码
密码
=与SPRING\u数据源\u用户名
JDBC\u数据库\u用户名
=与SPRING\u DATASOURCE\u PASSWORD
JDBC\u DATABASE\u PASSWORD
=与SPRING\u DATASOURCE\u URL
JDBC\u DATABASE\u URL
@RequestMapping("/env")
public @ResponseBody Map<String, String> env() {
return System.getenv();
}
@RequestMapping(“/env”)
public@ResponseBody映射环境(){
returnsystem.getenv();
}
小心,不要犯和我一样的错误:DATABASE\u URL
以postgres
开头,而postgres的JDBC前缀是postgresql
。使用SPRING\u DATASOURCE\u URL
和前缀JDBC:postgres:///code>将导致以下错误:
Driver org.h2.Driver声称不接受jdbcUrl,jdbc:postgres://....