内容简介:版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com https://blog.csdn.net/isea533/article/details/84062301
版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com https://blog.csdn.net/isea533/article/details/84062301
本文仅为个人记录,方便以后查阅。
项目中使用了 dubbo,注册中心使用的 zookeeper,使用 zookeeper 实现了一个简单的分布式锁(依赖 curator),因为配置文件存在 dubbo.registry 配置,为了直接使用这个地址来创建分布式锁,写了一个简单的方法来提取 zookeeper 地址。
dubbo.registry 有多种配置方式,支持所有情况,下面是常见的例子和提取结果:
zookeeper://localhost:2181
zookeeper://localhost:2181?client=zkclient
zookeeper://localhost:2181?backup=localhost:2182,localhost:2183
zookeeper://localhost:2181?client=zkclient&backup=localhost:2182,localhost:2183
------------结果------------
Optional[localhost:2181]
Optional[localhost:2181]
Optional[localhost:2181,localhost:2182,localhost:2182]
Optional[localhost:2181,localhost:2183,localhost:2183]
import java.util.Optional;
public class ZookeeperURL {
public static final String PREFIX = "zookeeper://";
public static final String BACKUP = "backup=";
public static Optional<String> convertDubboRegistryToZookeeperURL(String dubboRegistry){
StringBuilder zookeeperURL = new StringBuilder();
if(dubboRegistry != null && dubboRegistry.startsWith(PREFIX)){
dubboRegistry = dubboRegistry.substring(PREFIX.length());
int index = dubboRegistry.indexOf("?");
if(index > 0){
zookeeperURL.append(dubboRegistry.substring(0, index));
dubboRegistry = dubboRegistry.substring(index + 1);
String[] dubboRegistries = dubboRegistry.split("&");
for (int i = 0; i < dubboRegistries.length; i++) {
if(dubboRegistries[i].startsWith(BACKUP)){
String[] backups = dubboRegistries[i].substring(BACKUP.length()).split(",");
for (int j = 0; j < backups.length; j++) {
zookeeperURL.append(",").append(backups[i]);
}
}
}
} else {
zookeeperURL.append(dubboRegistry);
}
return Optional.of(zookeeperURL.toString());
}
return Optional.empty();
}
public static void main(String[] args) {
System.out.println(convertDubboRegistryToZookeeperURL("zookeeper://localhost:2181"));
System.out.println(convertDubboRegistryToZookeeperURL("zookeeper://localhost:2181?client=zkclient"));
System.out.println(convertDubboRegistryToZookeeperURL("zookeeper://localhost:2181?backup=localhost:2182,localhost:2183"));
System.out.println(convertDubboRegistryToZookeeperURL("zookeeper://localhost:2181?client=zkclient&backup=localhost:2182,localhost:2183"));
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
谷歌时代的柏拉图
[美] 丽贝卡·戈尔茨坦 / 李鹏程 / 中信出版集团·新思文化 / 2017-12-10 / 69.00元
我愿意用我所有的科技去换取和苏格拉底相处的一个下午。 ——史蒂夫•乔布斯 谷歌时代,科技昌明,众声喧哗,哲学提出的许多问题,科学似乎都已经给出了答案。若是如此,为什么我们今天还需要哲学?这个由古希腊城邦时代的哲人苏格拉底和柏拉图开创的学科,真的过时了吗? 已经2400岁 的柏拉图对此有话要说。哲学家兼小说家、美国国家人文奖章获得者戈尔茨坦史海钩沉,从经典著作中复活了柏拉图,让他来......一起来看看 《谷歌时代的柏拉图》 这本书的介绍吧!
JS 压缩/解压工具
在线压缩/解压 JS 代码
RGB HSV 转换
RGB HSV 互转工具