在CircleCi的Spring Boot中访问PostgreSQL 9.6

7

我有一个使用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

如何正确配置数据库?我有些迷茫。


你的 DATABASE_URL 是什么? - StanislavL
@StanislavL:我添加了我尝试过的每个DATABASE_URL的列表。这是你想要的吗? - pupeno
PostgreSQL是否在Docker容器中运行,因此您需要使用容器IP(或名称)而不是localhost? - StanislavL
@StanislavL:也许不是,我不确定,但这似乎表明了相反的情况:https://circleci.com/docs/2.0/postgres-config/ - pupeno
你尝试过像上面链接中那样使用双引号将 - DATABASE_URL="postgresql://dashman_test@localhost:5434/dashman_test" 引用起来吗?你是否也尝试按照文档中的示例添加 POSTGRES_PASSWORD: "some password" - Vao Tsun
1个回答

9
以下几点应该能帮助您解决问题。

(1) 你在评论中提到的文档(this one)要么过时,要么是误导性的。下面这段话:

PostgreSQL 9.6 的默认用户、端口和测试数据库如下所示: postgres://ubuntu:@127.0.0.1:5432/circle_test

... 是不正确的

postgres:9.6 的实际默认值为:

  • 用户名:postgres
  • 密码:<empty>
  • 端口:5432
  • 数据库:postgres

您可以从应用程序访问 127.0.0.1 上的 postgres 实例。

您可以在此处找到有关默认值的更多信息,但在设置它们时有一个注意点(有关此问题的更多信息请参见(3))。


(2) 据我所知,对于postgres连接器的jdbc url,没有办法传递用户名\密码,因此您可能不仅需要告诉应用程序DATABASE_URL,还需要像DATABASE_USERDATABASE_PASSWORD这样的内容。

这部分取决于您的应用程序的具体情况,但对于具有默认数据库设置的典型Spring Boot应用程序,您希望最终获得以下设置:

spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=

(3) 或者,如果您的连接设置是硬编码的,则可能希望为 postgres 实例配置凭据。
不幸的是,尽管在使用 docker run 运行容器时设置 POSTGRES_* 环境变量可以正常工作,但在 .circleci/config.yml 中设置它们则无法工作。有一些开放式 bug 报告(12),描述了这个或类似的问题,我认为这可能是一个 bug。
幸运的是,在构建过程中安装 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;"

如果你想让你的步骤生效,可以参考这个完整的例子。请注意保留HTML标签。

使用机器执行程序手动运行postgres(请参考此页面上的最后一个例子)也是一个选择,但我自己没有尝试过。


(4) 我实际上尝试了自己配置,您可以查看具有 工作版本的repo。构建输出示例在此处

我使用了这个spring boot示例并将其用于postgres,仅保留相关测试,添加了circle ci以及其他微小的调整。它演示了通过环境变量配置应用程序和配置postgres实例的方法。

最有趣的部分是.circleci/config.yml(其中定义凭据env.变量并在postgres实例中创建用户\db):

版本:2 作业: 构建: Docker: - 镜像:maven:3.5.0-jdk-8 环境: DATABASE_URL:jdbc:postgresql://127.0.0.1:5432/databasename DATABASE_USER:username DATABASE_PASSWORD:password - 镜像:postgres:9.6
工作目录:~/repo
步骤: - 检出 - 运行:apt-get update -qq && apt-get install -y postgresql - 运行: 命令:| 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;" - 运行: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

谢谢。这个例子真的很有帮助,特别是你需要设置属性文件的部分,这正是我所缺少的。我会把它添加到答案中(以防例子被删除)。 - pupeno
@Pablo 目前没有删除它的计划,但是确实是个好主意。我在示例中添加了属性文件和 circleci 配置文件。 - Eugene Loy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接