1
头图

Hello everyone, I am Glacier~~

Recently, I found that many small partners have to recompile the entire project after modifying the SpringBoot configuration file, which greatly wastes development time. Many friends around me have been doing this all the time. So, is there any way to modify the configuration file without compiling the entire project? Let's listen to the glacier carefully~~

springboot configuration file

The most classic configuration file in SpringBoot is application.yml. In the process of packaging and deploying the SpringBoot project, the application.yml file will also be packaged into the project. At first glance, if we modify the configuration information of the application.yml file, do we need to recompile and package the entire SpringBoot project? This is too much trouble!

In fact, SpringBoot provides a mechanism to modify the configuration file of application.yml without recompiling and packaging the entire project. premise of 161002683dcc69 is: you can't write the configuration information to your Java code.

So, how can we avoid modifying the application.yml file and recompiling and packaging the entire project?

I believe that many friends can think of it at the first time. I can add SpringBoot configuration parameters when starting the project on the command line. Yes, this is indeed a way.

For example, if the port of our packaged project is 8080, and we need to change the port 8080 to 8888 at this time, then we can use the following command to change the port to 8888 when starting the SpringBoot project.

java -jar xxx.jar --server.port=8888

However, if we modify a lot of parameters, especially when many custom parameters are defined in the application.yml file, and these parameters need to be adjusted when deployed to each node in the distributed system, this type of modification on the command line The parameter configuration method is not so convenient.

So, today, Glacier is not talking about this way. Which way is that?

Don't worry, let's continue to look down~~

Load files dynamically

What Glacier is talking about here is that SpringBoot can dynamically load configuration files when the project is started. How do you do it? Let's look down together.

Directly speaking: puts the configuration file of the project outside the src directory, and specifies the location of the external configuration file in the service startup script

For example, when deploying the SpringBoot project in a Linux system, we can deploy it as follows.

(1) Put the application.yml file in the project source code package in another directory to prevent the project from recognizing it as a configuration file and read the content inside.

(2) Compile the packaged project, and upload the packaged file to the server ( Note: The application.yml has been placed in other directories before packaging, and the packaged jar package contains without the application.yml file) .

(3) If we use the shell script to start the jar package, we need to modify the content of the shell script. For example, the content of the script before modification is as follows.

#!/bin/sh
kill -9 `ps -ef|grep java | grep gat-0.0.1.jar | awk '{print $2}'` > /dev/null 2>&1
java -jar xxx.jar --server.port=8081 > /dev/null 2>&1 &

The content of the modified script is shown below.

#!/bin/sh
kill -9 `ps -ef|grep java | grep gat-0.0.1.jar | awk '{print $2}'` > /dev/null 2>&1
java -jar xxx.jar --spring.config.location=conf/application.yml --server.port=8888 > /dev/null 2>&1 &

Here, we have added the code --spring.config.location=conf/application.yml --server.port=8888 is the port number of the specified service. spring.config.location=conf/application.yml the parameter that specifies the location of the external application.yml configuration file. Adding this parameter to the command to run the jar package can specify the location of the configuration file other than the jar package.

here today, I’m Glacier, see you in the next issue~~


冰河
156 声望970 粉丝