用途
项目中使用了 dubbo,注册中心使用的 zookeeper,使用 zookeeper 实现了一个简单的分布式锁(依赖 curator),因为配置文件存在 dubbo.registry 配置,为了直接使用这个地址来创建分布式锁,写了一个简单的方法来提取 zookeeper 地址。
效果
dubbo.registry 有多种配置方式,支持所有情况,下面是常见的例子和提取结果:
?
1
2
3
4
5
6
7
8
9
|
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 ]
|
代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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" ));
}
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/isea533/article/details/84062301
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 67
-
2025-05-29 31
-
2025-06-04 93
-
2025-06-04 17
-
win10卸载xbox game bar(如何卸载xbox游戏)
2025-05-25 44
热门评论