Chapter 2 springboot + archaius + consul (Configuration Management) and springbootarchaius

Source: Internet
Author: User

Chapter 2 springboot + archaius + consul (Configuration Management) and springbootarchaius

Archaius(Produced by netflix)

Features:

  • Allows you to Dynamically Retrieve configurations.
    • The principle is to read the content from the configuration source every 60 s (default, configurable), so that after the configuration file is modified, the modified content can take effect without restarting the service.
    • Prerequisites:Use the archaius API to read data. If you use the Environment and @ value annotations, You need to restart the service to make the new value take effect. Therefore, you often need to change the value and use the following code to read it, the values directly injected into spring bean do not need to be changed.Environment and @ value !!!
  • All configured operations are thread-safe.

Purpose:

  • Unified Configuration Management
  • Dynamic Configuration

 

1. Start consul and create KV

Start consul and create KV view: Chapter 20th springboot + consul (1)

Note:You can create kv manually..

The result is as follows:

Note:

  • Key:Service/"service name"/"service tag"/config
  • Value: configuration content in the application. properties File

 

2. Programming

1. Introduce jar

1 <! -- Archaius --> 2 <dependency> 3 <groupId> com. netflix. archaius </groupId> 4 <artifactId> archaius-core </artifactId> 5 <version> 0.6.6 </version> 6 </dependency> 7 <! -- Dynamic configuration, archaius underlying layer --> 8 <dependency> 9 <groupId> commons-configuration </groupId> 10 <artifactId> commons-configuration </artifactId> 11 <version> 1.8 </version> 12 </dependency>

Note: The introducedCommons-configurationIs the underlying implementation of archaius.

2. Build the source for reading the configuration of archaius

1 package com. microservice. archaius; 2 3 import java. io. stringReader; 4 import java. util. hashMap; 5 import java. util. map; 6 import java. util. properties; 7 8 import org. apache. commons. lang3.StringUtils; 9 10 import com. google. common. base. optional; 11 import com. netflix. config. pollResult; 12 import com. netflix. config. polledConfigurationSource; 13 import com. orbitz. consul. consul; 14 import com. orbitz. consul. keyValueClient; 15 16/** 17 * specifies the archaius to read the configuration source 18 */19 public class ConsulConfigurationSource implements PolledConfigurationSource {20 21 private String keyName; 22 23 public ConsulConfigurationSource (String keyName) {24 this. keyName = keyName; 25} 26 27/** 28 * by default, this method runs once every 60 s, 29 */30 @ Override31 public PollResult poll (boolean initial, object checkPoint) throws Exception {32 Consul consul = Consul. builder (). build (); 33 KeyValueClient kvClient = consul. keyValueClient (); 34 Optional <String> kvOpt = kvClient. getValueAsString (keyName); 35 String kvStr = StringUtils. EMPTY; 36 if (kvOpt. isPresent () {37 kvStr = kvOpt. get (); 38} 39 40 Properties props = new Properties (); 41 props. load (new StringReader (kvStr); // String-> Properties42 43 Map <String, Object> propMap = new HashMap <> (); 44 for (Object key: props. keySet () {45 propMap. put (String) key, props. get (key); 46} 47 return PollResult. createFull (propMap); 48} 49 50}

Note:

  • String-> properties use prop. load (new StringReader (str ));
  • The poll method is executed once every 60 s (default) (that is, the value is pulled from consul)

3. Configure the manager and dynamically read

1 @ ApiOperation ("get KV from consul by archaius") 2 @ RequestMapping (value = "/kv2/", method = RequestMethod. GET) 3 public void getKVByArchaius (@ RequestParam ("key") String key) throws IOException {4 5 PolledConfigurationSource source = new ConsulConfigurationSource (key ); // define the source of the read configuration 6 AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler (); // set 7 DynamicConfiguration configuration = new DynamicConfiguration (source, schedmanager); 8 9 ConfigurationManager. install (configuration); 10 11 DynamicStringProperty dsp = DynamicPropertyFactory. getInstance (). getStringProperty ("mysql. driverClassName "," zhaojigangDriver "); 12 System. out. println ("current time:" + LocalDateTime. now () + "--> value:" + dsp. get (); 13 try {14 Thread. sleep (60000); // sleep 60s15} catch (InterruptedException e) {16 e. printStackTrace (); 17} 18 System. out. println ("current time:" + LocalDateTime. now () + "--> value:" + dsp. get (); 19}

Steps:

  • Build DynamicConfiguration Based on the Data configuration source PolledConfigurationSource and the scheduler abstractpollingschedconfiguration (this class is actually a Property)
  • Add DynamicConfiguration to ConfigurationManager
  • Use DynamicPropertyFactory. getInstance (). getStringProperty (String key, String defaultValue) to dynamically read

 

Configuration Management and dynamic management in microservices are complete !!!

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.