Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure and manage ZooKeeper

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to configure and manage ZooKeeper. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The main goal is: the Spring PropertyPlaceholderConfigurer configuration file loader integrates ZooKeeper to achieve remote configuration reading.

Configuration Management (Configuration Management).

In cluster services, you may encounter a problem: when you need to modify the configuration, you have to modify each instance, which is tedious and error-prone. Of course, you can use scripts to solve the problem, but this is not the best solution.

OK,Let's go!

Let's look at the project structure first.

ZooKeeperPropertyPlaceholderConfigurer.java

Inherit org.springframework.beans.factory.config.PropertyPlaceholderConfigurer and rewrite processProperties (beanFactoryToProcess, props) to complete the implementation of remote configuration loading

Package org.bigmouth.common.zookeeper.config.spring; import java.io.UnsupportedEncodingException;import java.util.Properties; import org.apache.commons.lang.StringUtils;import org.bigmouth.common.zookeeper.config.Config;import org.bigmouth.common.zookeeper.config.ZooKeeperConfig;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class ZooKeeperPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {public static final String PATH = "zoo.paths" @ Override protected void processProperties (ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {super.processProperties (beanFactoryToProcess, props); try {fillCustomProperties (props); System.out.println (props);} catch (Exception e) {/ / Ignore e.printStackTrace () } private void fillCustomProperties (Properties props) throws Exception {byte [] data = getData (props); fillProperties (props, data);} private void fillProperties (Properties props, byte [] data) throws UnsupportedEncodingException {String cfg = new String (data, "UTF-8") If (StringUtils.isNotBlank (cfg)) {/ / complete should also need to be handled: multiple configurations, value containing =, ignoring the beginning of the # sign String [] cfgItem = StringUtils.split (cfg, "="); props.put (cfgItem [0], cfgItem [1]);} private byte [] getData (Properties props) throws Exception {String path = props.getProperty (PATH) Config config = new ZooKeeperConfig (); return config.getConfig (path);}}

Config.java

Configure the operation interface

Package org.bigmouth.common.zookeeper.config; public interface Config {byte [] getConfig (String path) throws Exception;}

Startup.java

Program startup entry

Package org.bigmouth.common.zookeeper.config; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Startup {public static void main (String [] args) {new ClassPathXmlApplicationContext ("classpath:/config/applicationContext.xml");}}

ZooKeeperConfig.java

Implementation of configuration operation interface ZooKeeper

Package org.bigmouth.common.zookeeper.config; import org.apache.curator.framework.CuratorFramework;import org.apache.zookeeper.data.Stat; public class ZooKeeperConfig implements Config {@ Override public byte [] getConfig (String path) throws Exception {CuratorFramework client = ZooKeeperFactory.get (); if (! exists (client, path)) {throw new RuntimeException ("Path" + path + "does not exists.");} return client.getData () .forPath (path) } private boolean exists (CuratorFramework client, String path) throws Exception {Stat stat = client.checkExists () .forPath (path); return! (stat = = null);}}

ZooKeeperFactory.java

Manage ZooKeeper client connections

Package org.bigmouth.common.zookeeper.config; import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry; public class ZooKeeperFactory {public static final String CONNECT_STRING = "172.16.3.42 public static final int BASE_SLEEP_TIMEMS 2181172.16.3.65"; public static final int MAX_RETRIES = 3; public static final int BASE_SLEEP_TIMEMS = 3000 Public static final String NAME_SPACE = "cfg"; public static CuratorFramework get () {RetryPolicy retryPolicy = new ExponentialBackoffRetry (BASE_SLEEP_TIMEMS, MAX_RETRIES); CuratorFramework client = CuratorFrameworkFactory.builder () .connectString (CONNECT_STRING) .retryPolicy (retryPolicy) .namespace (NAME_SPACE) .build (); client.start (); return client }}

ApplicationContext.xml

The configuration loader uses the ZooKeeperPropertyPlaceholderConfigurer we created ourselves because it overrides the processProperties method. This method reads the remote configuration.

Classpath:application.properties

Application.properties

Project configuration file, in which all configurations should be saved in ZooKeeper except for configuring the ZooKeeper server address and the node to be read.

Zoo.paths=/properties

Set up ZooKeeper data

Log in to ZooKeeper to add a configuration item for the node / cfg/properties:

As shown in the figure: I created a node / cfg/properties and set the content to: jdbc.driver=org.postgresql.Driver

Run Startup.java

OK, zoo.paths is in the local application.properties file, jdbc.driver is in the remote ZooKeeper server.

Jar packages that need to be relied upon in the project

Commons-lang commons-lang 2.4 org.springframework spring-core 3.0.3.RELEASE org.springframework spring-context 3.0.3.RELEASE org.springframework spring-tx 3.0.3.RELEASE org.springframework spring-context-support 3.0.3.RELEASE org.apache.zookeeper zookeeper 3.4.6 org.apache.curator curator-framework 2.4.2 org.apache.curator curator-recipes 2 .4.2 this is the end of the article on how to configure and manage ZooKeeper. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report