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 use Spring4+ZooKeeper to configure the center

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use Spring4+ZooKeeper configuration center". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One: ZooKeeper connection configuration class

@ Configuration

@ PropertySource ("classpath:context-datasource.properties")

Public class ZooKeeperElConfig {

@ Value ("${zookeeper.cluster.url}")

Private String zooKeeperClusterUrl

@ Value ("${zookeeper.root.path}")

Private String zooKeeperRootPath

@ Autowired

Private Environment environment

@ Bean

Public static PropertySourcesPlaceholderConfigurer propertyConfigure () {

Return new PropertySourcesPlaceholderConfigurer ()

}

@ Bean (name = "zkClient")

Public CuratorFramework zkClient () {

CuratorFramework client = CuratorFrameworkFactory.builder () .connectString (zooKeeperClusterUrl)

.sessionTimeoutMs (5000) .retryPolicy (new ExponentialBackoffRetry (1000, 3)) .namespace (zooKeeperRootPath) .retryPolicy ()

Client.start ()

Return client

}

}

Second: read ZooKeeper parameters tool class

@ Component ("systemElConfig")

Public class SystemElConfig {

@ Autowired

CuratorFramework zkClient

Public String getProperty (String propertyName) {

String property = ""

Try {

Property = new String (zkClient.getData () .forPath ("/" + propertyName))

}

Catch (Exception e) {

E.printStackTrace ()

}

Return property

}

}

Third: examples of parameter information in ZooKeeper referenced by other configuration classes

@ Value ("# {systemElConfig.getProperty ('mailHost')}")

Private String mailHost

Using Spring EL expression annotation method

This is the end of "how to use Spring4+ZooKeeper configuration Center". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report