2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
First of all, a Java project needs to have different environment configurations, and the corresponding configurations are automatically used when packaging. So, how to achieve this?
Create or add an application.yml file in the src/main/resources directory of your Spring Boot project. This is the default location where Spring Boot looks for configuration files. In this file, you can add common configurations.
Then, you can also create files such as application-test.yml, application-prod.yml, etc. in the same directory. Spring Boot will automatically recognize these profile-specific configuration files. You can define parameters for different environments in these files, such as database address, redis cache address, etc.
That is, in the src/main/resources directory, there may be several files like this:
application.yml
application-prod.yml
application-test.yml
Then, you can declare two configurations with the same variables but different values in application-test.yml and application-prod.yml. For example
application-test.yml:
mysql:
url: https://xx.test.com/mysql
application-prod.yml:
mysql:
url: https://xx.online.com/mysql
In the Java code, you can use this URL dynamically:
@Value("${mysql.url}")
protected String mysqlUrl;
Perfect.
So, how does the system automatically use the corresponding profile-specific.yml file when it is running? Please see below
Let's look at how environment variables are passed to the project layer by layer from the external network.
First of all, a Java project is usually packaged with Docker. So, let's start with Docker:
docker build -f assistant-web-api/qke/Dockerfile -t docker-registry.qiyi.virtual/mbd-ai/assistant-web-api-test:$tag . --build-arg dc_env=test
Key words,build-arg , set a parameter, dc_env=test
ARG dc_env
...
RUN mvn -T 8 -DdisableRpm=true -pl '!assistant-web-manager' clean package -Dmaven.test.skip=true -P${dc_env}
Note that -P means setting profile
Where is the -P added to the mvn command in the previous step? Of course, it is in the Maven configuration file, that is, pom.xml.
<profiles>
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
<log.env>test</log.env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
<log.env>prod</log.env>
</properties>
</profile>
</profiles>
Above, two environment variables are added. Each variable has properties set, namelyprofile.active
Where can this value be used?
In the main application.yml file use!
server:
profiles:
active: @profile.active@
Define which Profile to use based on the value of the profile.active variable from the previous step.
From now on, the variables defined by the docker package are passed to the main yml file. In this file, the configuration of the sub-yml file will be used according to the value of server.profiles.active. For example, if it is test, the configuration of application-test.yml will be loaded!!!
First, sync Maven. After syncing, check an environment.
Next, compile and package locally, and specify environment variables through -P:
Finally, to run, click on the upper right corner of the picture below, there is an Edit Configuration, a pop-up window will appear, in Active Profile, fill in the corresponding environment variables, such as test
Finally, you can click Run!