简体   繁体   English

从CircleCi中的Spring Boot访问PostgreSQL 9.6

[英]Accessing PostgreSQL 9.6 from Spring Boot in CircleCi

I have a Spring Boot application that currently builds and runs tests in Heroku's CI and I'm trying to get it to work in Circle CI as well. 我有一个Spring Boot应用程序,目前在Heroku的CI中构建并运行测试,我也试图让它在Circle CI中运行。 My config file looks like this: 我的配置文件如下所示:

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

I tried various ways of defining DATABASE_URL to no effect: 我尝试了各种方法来定义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

Nothing seems to work, I always end up with this error: 似乎没有什么工作,我总是最终得到这个错误:

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

What's the proper way of configuring the database? 配置数据库的正确方法是什么? I'm a bit lost. 我有点迷茫。

Here are a few points that should help you to address the problem. 以下几点可以帮助您解决问题。


(1) The documentation you mentioned in comments ( this one ) is either outdated or plain misleading. (1)您在评论中提到的文件( 本文 )已过时或具有误导性。 This: 这个:

The default user, port, test database for PostgreSQL 9.6 are as follows: postgres://ubuntu:@127.0.0.1:5432/circle_test PostgreSQL 9.6的默认用户,端口,测试数据库如下:postgres:// ubuntu:@ 127.0.0.1:5432 / circle_test

... is not true . ...... 不是真的

The actual defaults for postgres:9.6 are: postgres:9.6的实际默认值postgres:9.6是:

  • username: postgres 用户名:postgres
  • password: <empty> 密码: <empty>
  • port: 5432 港口:5432
  • database: postgres 数据库:postgres

You can reach postgres instance from your application on 127.0.0.1 . 您可以在127.0.0.1上从您的应用程序访问postgres实例。

You can find more info about defaults here but there is a catch about setting them (more on this in (3) ). 你可以在这里找到更多关于默认值的信息,但有一个关于设置它们的问题(更多关于这个(3) )。


(2) As far as I know there is no way to pass usrename\\password in jdbc url for postgres connector, so you probably have to tell your application not only DATABASE_URL but also something like DATABASE_USER and DATABASE_PASSWORD . (2)据我所知,没有办法在jdbc url中为postgres连接器传递usrename \\ password ,所以你可能不仅要告诉应用程序DATABASE_URL ,还要告诉DATABASE_USERDATABASE_PASSWORD

This part is dependent on specifics of your application, but for typical spring boot application with default database settings you want to end up with the following settings: 此部分取决于您的应用程序的细节,但对于具有默认数据库设置的典型Spring启动应用程序,您希望最终得到以下设置:

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

(3) Alternatively, if your connection settings are hardcoded you probably want to configure credentials for postgres instance. (3)或者,如果您的连接设置是硬编码的,则可能需要为postgres实例配置凭据。

Unfortunately, even though setting POSTGRES_* environment variables when running container with docker run works fine, setting them in .circleci/config.yml does not work. 不幸的是,即使在运行带有.circleci/config.yml docker run容器时设置POSTGRES_*环境变量也能docker run工作,但在.circleci/config.yml中设置它们是.circleci/config.yml There are few open bug reports ( 1 , 2 ) describing this or similar issue and my money is on this being a bug. 很少有开放的bug报告( 12 )描述这种或类似的问题,我的钱是这个是一个错误。

Fortunately, you still can work around this by installing psql and creating desired user credentials during your build (default credentials still work). 幸运的是,您仍然可以通过在构建期间安装psql并创建所需的用户凭据来解决此问题(默认凭据仍然有效)。 Adding something like: 添加如下内容:

  - 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;"

... to your steps should do the trick (see full example here ). ...到你的steps应该做的伎俩(见这里的完整示例 )。

Using machine executor to run postgres manually (see last example on this page ) might be an option too, but I have not tried this one myself. 使用机器执行器手动运行postgres(参见本页上的最后一个示例)也可能是一个选项,但我自己没有尝试过这个。


(4) I actually tried configuring this for myself and you can check out repo with working version here . (4)我实际上尝试为自己配置这个,你可以在这里查看带有工作版本的 repo。 Build output example here . 在此构建输出示例

I used this spring boot sample and made it use postgres, left only relevant tests, added circle ci along with other minor tweaks. 我使用了这个春季靴子样本 ,并使用postgres,只留下相关测试,添加了圆圈ci以及其他小调整。 It demonstrates both configuring application via env. 它演示了通过env配置应用程序。 variables and configuring postgres instance. 变量和配置postgres实例。

The most interesting parts are .circleci/config.yml (where credentials env. variables are defined and user\\db gets created in postgres instance): 最有趣的部分是.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

... and application.properties (where credentials env. variables are used): ...和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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM