用途

成都創(chuàng)新互聯(lián)是一家專業(yè)提供同安企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、做網(wǎng)站、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為同安眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
項(xiàng)目中使用了 dubbo,注冊中心使用的 zookeeper,使用 zookeeper 實(shí)現(xiàn)了一個(gè)簡單的分布式鎖(依賴 curator),因?yàn)榕渲梦募嬖?dubbo.registry 配置,為了直接使用這個(gè)地址來創(chuàng)建分布式鎖,寫了一個(gè)簡單的方法來提取 zookeeper 地址。
效果
dubbo.registry 有多種配置方式,支持所有情況,下面是常見的例子和提取結(jié)果:
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 ------------結(jié)果------------ 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"));
}
}總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
文章標(biāo)題:從dubbozookeeper注冊地址提取出zookeeper地址的方法
本文來源:http://www.chinadenli.net/article40/gcsoho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、建站公司、App開發(fā)、手機(jī)網(wǎng)站建設(shè)、定制開發(fā)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)