Micronaut未连接到yml中的db

问题描述

我为我的应用程序创建了一个新环境,并将其称为docker。我正在尝试将东西设置为:

application-docker.yml

micronaut:
  application:
    name: time
  server:
    netty:
      access-logger:
        enabled: true
        logger-name: access-logger

datasources:
  default:
    url: jdbc:postgresql://db:5432/postgres
    driverClassName: org.postgresql.Driver
    username: postgres
    password: postgres
    schema-generate: CREATE_DROP
    dialect: POSTGRES
    schema: time
jpa.default.properties.hibernate.hbm2ddl.auto: update

flyway:
  datasources:
    default:
      enabled: true
      schemas: time
...

但是,当我尝试这样运行我的应用程序时:

java -jar target/timeshare-0.1.jar -Dmicronaut.environments=docker -Dcom.sun.management.jmxremote -Xmx128m

如果失败...是因为它无法连接到本地主机!

08:11:00.949 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
08:11:02.013 [main] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285)

为什么要尝试连接到本地主机?我想念什么?

解决方法

Micronaut似乎无法找到 application-docker.yml 文件,然后使用默认文件。

因为可以使用-Dmicronaut.environments=not-existing-profile这样的示例,即使它不存在,也不会显示任何错误。

因此,请确保在 src / main / resources 目录中具有 application-docker.yml 文件,并且在执行过程中该文件确实已导出到结果jar中构建并位于jar存档的根目录中:

target/timeshare-0.1-all.jar
├── com
├── META-INF
├── org
├── application-docker.yml
├── application.yml
├── logback.xml
...

您如何构建结果罐?当您使用 shadowJar 任务时,它必须包含所有内容。


另一种选择是使用MICRONAUT_ENVIRONMENTS系统变量:

export MICRONAUT_ENVIRONMENTS=docker

但这与-Dmicronaut.environments=docker启动选项的行为相同。


另一种选择是通过micronaut.config.files启动选项来指定 application-docker.yml 配置文件的确切路径:

java -jar target/timeshare-0.1-all.jar -Dmicronaut.config.files=/some/external/location/application-docker.yml

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...