Unified Configuration Center

Source: Internet
Author: User

Before my 2015 second half of the summary mentioned that our project adopted a micro-service model, that is, the system according to a certain technology and business cut into separate small systems, such as our product is an e-commerce system, then can be divided into: front-end WAP, front-end API, commodity management system, procurement system, Master Data Management System, User Center management, price management system, promotion management system, order management system, Inventory management system, store management system and so on, the last statistic data is Dubbo service up to 18, the Web system has 3, front-end WAP site A. These systems need to be connected to a variety of resources, such as service address, database, cache, file system, message queue, etc., and the configuration items used in general projects are roughly the following two categories: resources and specific business related.

Application Scenarios for configuration center:

    • There are multiple systems within the company, such as our web site plus Dubbo Services total more than 20, and the system between the basic technical framework and a certain degree of connectivity
    • A set of systems need to configure multiple environments, we have development environment, test environment, pre-launch environment, online environment

The core problem that the configuration center needs to solve is the problem that the unified management of multiple system configuration information is difficult, here I care about the following features:

    • Loading data from zookeeper into the bean Manager
    • Solve multi-environment value problems, development environment, test environment, production environment
    • Zookeeper configuration and local configuration compatibility issues, by certain means can determine whether to use zookeeper information or local information, such as local debugging is very useful
    • Zookeeper update issues After a configuration item has changed

Here is a Baidu disconf map, the project is more powerful, interested to study:

First we look at the configuration items that are used in the system, and there are generally two ways to use them:

A: In some XML configuration files, such as:

<accesslog= "true"  name= "Dubbo"  port= "${zk.port } "/>

B: In the program, it is generally obtained by @value this annotation, for example, we can write a configuration class to load configuration items:

@Service  Public class mmsconfig {    @Value ("${es.cluster.name}")    private  String esclustername;      Public String Getesclustername () {        return  esclustername;}


There are two ways to use @value annotations here:

    • @Value ("${es.cluster.name}")
    • @Value ("#{configproperties[' Es.cluster.name '}")

To understand these two uses, you need to know the following classes:

    • Propertiesfactorybean: The Official document here is: allows for making a properties file from a classpath location available as properties ins Tance in a bean factory. Can is used to populate any beans property of type Properties via a bean reference. Supports loading from a properties file and/or setting local properties on the this factorybean. The created Properties instance be is merged from loaded and local values. If neither a location nor local properties is set, an exception'll is thrown on initialization. is to read the configuration information from the specified document and load it into the system. It can use the second method above in the program.
    • Beanfactorypostprocessor: The direct point is to provide the bean with the property value management
    • Propertyplaceholderconfigurer, the implementation of the Beanfactorypostprocessor interface, this class is more advanced, mainly replace the point character ${...}, it is not only loaded from the file, Also search for related keys from system variables and environment variables
    • Preferencesplaceholderconfigurer, it's a subclass of Propertyplaceholderconfigurer.


Figuring out the logic for the system to take value from the configuration file, it's not hard to understand the unified Configuration center, but to do something about loading the configuration items to get the updated configuration items as we intended. Here we apply an already very mature product zookepper, and its data results are similar to the following:


The core function is to get the configuration items from the Zookepper and load them into the system variables. Let's see if the configuration item in zookeeper is loaded into the system, according to Propertyplaceholderconfigurer's function description, it will load the configuration from three places, we choose to load the zookeeper configuration into the system variable, The core code follows two steps:

    • Get a map of a configuration item from zookeeper, no code is posted here.
    • Populate the map with one of the system variables, as long as the system variables have these values, then we can directly access our property values in the top-most way.
 private  void  Setsystemproperys ( Configcenter cc, map<string, Object> config) { for   =cc.get (key);  if  (Key.contains (".")              =key.substring (1 if  (Value==null   = "" ;         } system.setproperty (key, value); }    }


How does the configuration of different environments work?
The above feature only mentions how to load the configuration in the Zookepper into the system, then how to load the correct configuration according to the current environment, it is only necessary to pass an environment change at system startup, the configuration center according to the injected environment variable value to determine which environment should be loaded data. If it's a non-Web project, we just need to add an environment change parameter to the command that starts the service:-dmaven.test.skip=true clean Install-devn=sim, if it's a Web project, We can do this through the Servelt configuration file and finally get the parameters through Servletcontexstlistener, as shown in the following procedure:

Write a custom servletcontextlistener that is primarily used to obtain variables from the system startup environment and to provide the configuration center with

 Public classWanmeicontextloaderlistenerImplementsServletcontextlistener {@Override Public voidcontextinitialized (Servletcontextevent sce) {String EVN= System.getproperty ("EVN"); if(EVN = =NULL|| Evn.equals ("") ) {EVN= Sce.getservletcontext (). Getinitparameter ("EVN"); if(EVN = =NULL) {EVN= "QA"; } system.setproperty ("EVN", EVN); }} @Override Public voidcontextdestroyed (Servletcontextevent sce) {//TODO auto-generated Method Stub            }}

How do I update the values in a bean after the configuration items in zookeeper have changed?

We can use the guava provided by the Enventbus to solve, subscribe to a Zookeeper Update event to update the system changes can be, Datachangeevent is a custom class, to implement Automatic Updates need to write some callback methods, You can also refer to the following project: Https://github.com/jamesmorgan/ReloadablePropertiesAnnotation

Datachangeevent datachangeevent=New  datachangeevent (map, DataChangeEvent.DataType.REMOTE, DataChangeEvent.ChangeType.DELETE);        Configoption.getenventbus (). Post (datachangeevent);

How to configure it?

Only need to add a depends-on on the propertyplaceholderconfigurer, the purpose is to let its first execute our backdoor, the other use is not affected, basically do not need to modify the original code.

 <BeanID= "Initspringproperties"class= "Config.center.spring.SpringPropertyInjectSupport"Lazy-init= "false"Init-method= "Init">        < Propertyname= "Confignamespaces"value= "/configcenter/mms" />    </Bean>    <BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"depends-on= "Initspringproperties">        < Propertyname= "Locations">            <List>            </List>        </ Property>        < Propertyname= "fileencoding"value= "UTF-8" />    </Bean>

This article refers to:

http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/

Https://github.com/knightliao/disconf

Unified Configuration Center

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.