[英]Accessing PostgreSQL 9.6 from Spring Boot in CircleCi
我有一個Spring Boot應用程序,目前在Heroku的CI中構建並運行測試,我也試圖讓它在Circle CI中運行。 我的配置文件如下所示:
version: 2
jobs:
build:
docker:
- image: circleci/jdk8:0.1.1
- image: postgres:9.6
working_directory: ~/repo
environment:
# Customize the JVM maximum heap limit
JVM_OPTS: -Xmx3200m
TERM: dumb
steps:
- checkout
- run: chmod +x gradlew
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "build.gradle" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: ./gradlew dependencies
- save_cache:
paths:
- ~/.m2
key: v1-dependencies-{{ checksum "build.gradle" }}
# run tests!
- run: ./gradlew test
我嘗試了各種方法來定義DATABASE_URL無效:
jobs:
build:
docker:
- image: circleci/jdk8:0.1.1
environment:
- DATABASE_URL=postgresql://dashman_test@localhost:5433/dashman_test
- image: postgres:9.6
environment:
- POSTGRES_USER=dashman_test
- POSTGRES_DB=dashman_test
jobs:
build:
docker:
- image: circleci/jdk8:0.1.1
environment:
- DATABASE_URL=postgresql://dashman_test@localhost:5434/dashman_test
- image: postgres:9.6
environment:
- POSTGRES_USER=dashman_test
- POSTGRES_DB=dashman_test
jobs:
build:
docker:
- image: circleci/jdk8:0.1.1
environment:
DATABASE_URL: postgresql://dashman_test@localhost:5434/dashman_test
- image: postgres:9.6
environment:
POSTGRES_USER: dashman_test
POSTGRES_DB: dashman_test
TEST_DATABASE_URL: postgresql://ubuntu@localhost/circle_test?sslmode=disable
DATABASE_URL: postgresql://ubuntu@localhost/circle_test?sslmode=disable
DATABASE_URL: postgres://ubuntu:@127.0.0.1:5433/circle_test
DATABASE_URL: postgres://localhost:5433/dashman_test
DATABASE_URL: postgresql://ubuntu@localhost:5434/circle_test?sslmode=disable
DATABASE_URL: postgres://dashman_test:KnDnHtzneyTzps0WuYr35r9@localhost:5433/dashman_test
似乎沒有什么工作,我總是最終得到這個錯誤:
tech.dashman.dashmanserver.models.AccountTest > create FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException
tech.dashman.dashmanserver.models.UserTest > create FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException
tech.dashman.dashmanserver.DashmanserverApplicationTests > contextLoads FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException
配置數據庫的正確方法是什么? 我有點迷茫。
以下幾點可以幫助您解決問題。
(1)您在評論中提到的文件( 本文 )已過時或具有誤導性。 這個:
PostgreSQL 9.6的默認用戶,端口,測試數據庫如下:postgres:// ubuntu:@ 127.0.0.1:5432 / circle_test
...... 不是真的 。
postgres:9.6
的實際默認值postgres:9.6
是:
<empty>
您可以在127.0.0.1
上從您的應用程序訪問postgres實例。
你可以在這里找到更多關於默認值的信息,但有一個關於設置它們的問題(更多關於這個(3) )。
(2)據我所知,沒有辦法在jdbc url中為postgres連接器傳遞usrename \\ password ,所以你可能不僅要告訴應用程序DATABASE_URL
,還要告訴DATABASE_USER
和DATABASE_PASSWORD
。
此部分取決於您的應用程序的細節,但對於具有默認數據庫設置的典型Spring啟動應用程序,您希望最終得到以下設置:
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=
(3)或者,如果您的連接設置是硬編碼的,則可能需要為postgres實例配置憑據。
不幸的是,即使在運行帶有.circleci/config.yml
docker run
容器時設置POSTGRES_*
環境變量也能docker run
工作,但在.circleci/config.yml
中設置它們是.circleci/config.yml
。 很少有開放的bug報告( 1 , 2 )描述這種或類似的問題,我的錢是這個是一個錯誤。
幸運的是,您仍然可以通過在構建期間安裝psql
並創建所需的用戶憑據來解決此問題(默認憑據仍然有效)。 添加如下內容:
- run: apt-get update -qq && apt-get install -y postgresql
- run:
command: |
psql -h 127.0.0.1 -U postgres -c "CREATE DATABASE databasename;"
psql -h 127.0.0.1 -U postgres -c "CREATE USER username WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE databasename TO username;"
...到你的steps
應該做的伎倆(見這里的完整示例 )。
使用機器執行器手動運行postgres(參見本頁上的最后一個示例)也可能是一個選項,但我自己沒有嘗試過這個。
(4)我實際上嘗試為自己配置這個,你可以在這里查看帶有工作版本的 repo。 在此構建輸出示例 。
我使用了這個春季靴子樣本 ,並使用postgres,只留下相關測試,添加了圓圈ci以及其他小調整。 它演示了通過env配置應用程序。 變量和配置postgres實例。
最有趣的部分是.circleci/config.yml
(其中定義了憑證env。變量,並在postgres實例中創建了user \\ db):
version: 2 jobs: build: docker: - image: maven:3.5.0-jdk-8 environment: DATABASE_URL: jdbc:postgresql://127.0.0.1:5432/databasename DATABASE_USER: username DATABASE_PASSWORD: password - image: postgres:9.6 working_directory: ~/repo steps: - checkout - run: apt-get update -qq && apt-get install -y postgresql - run: command: | psql -h 127.0.0.1 -U postgres -c "CREATE DATABASE databasename;" psql -h 127.0.0.1 -U postgres -c "CREATE USER username WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE databasename TO username;" - run: mvn test
...和application.properties
(使用憑證環境變量):
spring.h2.console.enabled=false logging.level.org.hibernate.SQL=error spring.datasource.url=${DATABASE_URL} spring.datasource.username=${DATABASE_USER} spring.datasource.password=${DATABASE_PASSWORD} spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database=POSTGRESQL spring.datasource.platform=postgres spring.jpa.show-sql=true spring.database.driverClassName=org.postgresql.Driver
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.