一:ZooKeeper 连接配置类
-
@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).build();
-
client.start();
-
return client;
-
}
-
-
-
-
}
二: 读取ZooKeeper参数工具类
-
@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;
-
}
-
-
}
三: 其它配置类引用ZooKeeper中的参数信息举例
-
@Value("#{systemElConfig.getProperty('mailHost')}")
-
private String mailHost;
采用Spring EL 表达式 注解方式
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28624388/viewspace-2137860/
,如需转载,请注明出处,否则将追究法律责任。