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