问题
I have created a new environment for my application and called it docker. I'm trying stuff out so I set it like this:
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?
回答1:
It seems that Micronaut is not able to locate application-docker.yml file and then it is using the default one.
Because you can use for example -Dmicronaut.environments=not-existing-profile
and even if it does not exist, it does not show any error.
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
...
How are you building the result jar? When you use the shadowJar task then it must contain everything.
Another option is to use MICRONAUT_ENVIRONMENTS
system variable:
export MICRONAUT_ENVIRONMENTS=docker
But this behaves the same way as -Dmicronaut.environments=docker
startup option.
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
来源:https://stackoverflow.com/questions/64238391/micronaut-not-connecting-to-db-in-yml