Micronaut 未连接到 yml 中的数据库 [英] Micronaut not connecting to db in yml

查看:70
本文介绍了Micronaut 未连接到 yml 中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序创建了一个新环境并将其命名为 docker.我正在尝试一些东西,所以我这样设置:

I have created a new environment for my application and called it docker. I'm trying stuff out so I set it like this:

应用程序-docker.yml

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
...

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

However when I try to run my app like this:

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

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

If fails... because it can't connect to localhost!

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)

为什么它试图连接到本地主机?我错过了什么?

Why is it trying to connect to localhost? What am i missing?

推荐答案

Micronaut 好像找不到 application-docker.yml 文件,然后就使用默认的了.

It seems that Micronaut is not able to locate application-docker.yml file and then it is using the default one.

>

因为您可以使用例如 -Dmicronaut.environments=not-existing-profile 并且即使它不存在,也不会显示任何错误.

Because you can use for example -Dmicronaut.environments=not-existing-profile and even if it does not exist, it does not show any error.

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

So, make sure you have application-docker.yml file in the src/main/resources directory and also that the file is really exported into the result jar during build and is located in the root of the jar archive:

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

你是如何构建结果 jar 的?当您使用 shadowJar 任务时,它必须包含所有内容.

How are you building the result jar? When you use the shadowJar task then it must contain everything.

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

Another option is to use MICRONAUT_ENVIRONMENTS system variable:

export MICRONAUT_ENVIRONMENTS=docker

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

But this behaves the same way as -Dmicronaut.environments=docker startup option.

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

Another option is to specify exact path to the application-docker.yml configuration file by the micronaut.config.files startup option:

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

这篇关于Micronaut 未连接到 yml 中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆