SpringBoot implements data collection in the configuration center

Need pain points:

    Large and medium-sized projects involve many applications and configure a lot of parameter data. Each time you modify the configuration file, you need to develop and modify the configuration file, and submit release notes (or work tickets). Operation and maintenance re-modify the configuration file according to the modified content, and restart the project. In the process, it is easy to modify the configuration information incorrectly.

Configuration Center:

    In order to solve this kind of problem, the system puts forward the requirement of component "configuration center", stores the configuration information used by the system in the database, and loads it when the application restarts (or the application automatically reloads the database of the configuration center every 60 seconds).

Springboot construction ideas:

First look at the default springboot method

@SpringBootApplication
@Controller
public class LiangApplication extends SpringBootServletInitializer{
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView model = new ModelAndView();
		model.setViewName("index");
		return model;
	}
	/**
	 * Note: This main method is the startup method of springboot, and its startup is only run execution
	 * If it is changed to the configuration center, it needs to be able to load first, the resources/application.properties file of the current project
	 * Adopting the incremental design idea, on the basis of reading the project configuration information, the database configuration is read, and the project initialization information is combined together.
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(LiangApplication.class, args);
	}
}

Let's take a look at the adjusted method. This method is only a case of properties without a database connection.

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@SpringBootApplication
public class DubbobootApplication {
	public static void main(String[] args) {
		/**
		 * Ideas
		 * Read configuration file information into properties
		 * The information of the database adopts the design of key-value, and the information in the database is cached in the properties.
		 * Or put it into a public class that specifically handles reading the database here.
		 */
		Properties properties = new Properties();
		InputStream inputStream = DubbobootApplication.class.getClassLoader().getResourceAsStream("liang.properties");
		try {
			properties.load(inputStream);
			properties.setProperty("liang","---liang");
		} catch (IOException e) {
			e.printStackTrace ();
		}
		SpringApplication app = new SpringApplication(DubbobootApplication.class);
		app.setDefaultProperties(properties);
		app.run( args);
	}
}
Read the advantages of properties, use springboot to call the annotations of properties by yourself, which is convenient to use.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324578181&siteId=291194637