使用zookeeper实现服务路由和负载均衡
三个类:ServiceAProviderServiceBProviderServiceConsumer其中ServiceAProvider提供的服务名service-A,指向IP为192.168.58.130ServiceBProvider提供的服务名service-A,指向IP为192.168.58.131当有消费者请求时,随机地选取ser
·
-
三个类:
ServiceAProvider
ServiceBProvider
ServiceConsumer
其中
ServiceAProvider提供的服务名service-A,指向IP为192.168.58.130
ServiceBProvider提供的服务名service-A,指向IP为192.168.58.131
当有消费者请求时,随机地选取service-A列表的服务器提供服务
ServiceConsumer 为消费者类
依赖:
01.
<dependency>
02.
<groupId>org.apache.zookeeper</groupId>
03.
<artifactId>zookeeper</artifactId>
04.
<version>
3.4
.
5
-cdh5.
1.0
</version>
05.
</dependency>
06.
<dependency>
07.
<groupId>org.apache.helix</groupId>
08.
<artifactId>helix-core</artifactId>
09.
<version>
0.6
.
4
</version>
10.
</dependency>
github源码下载地址:
https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zookeeper
详见下面的代码:
服务提供者ServiceAProvider类的源码为:
01.
package
com.jamesfen.zookeeper;
02.
import
java.net.InetAddress;
03.
import
org.I0Itec.zkclient.ZkClient;
04.
public
class
ServiceAProvider {
05.
06.
private
String serviceName =
"service-A"
;
07.
08.
//向zookeeper注册服务
09.
public
void
init()
throws
Exception{
10.
String serverList =
"192.168.58.11:2181"
;
11.
String PATH =
"/configcenter"
;
//根节点路径
12.
ZkClient zkClient =
new
ZkClient(serverList);
13.
boolean
rootExists = zkClient.exists(PATH);
14.
if
(!rootExists){
15.
zkClient.createPersistent(PATH);
16.
}
17.
//判断是否存在,不存在则创建服务节点
18.
boolean
serviceExists = zkClient.exists(PATH +
"/"
+ serviceName);
19.
if
(!serviceExists){
20.
zkClient.createPersistent(PATH +
"/"
+ serviceName);
21.
}
22.
23.
//註冊當前服務
24.
InetAddress addr =InetAddress.getLocalHost();
25.
//String ip= addr.getHostAddress().toString();
26.
String ip =
"192.168.58.130"
;
27.
28.
//創建當前服務器節點
29.
zkClient.createEphemeral(PATH +
"/"
+ serviceName +
"/"
+ ip);
30.
31.
System.out.println(
"提供的服务节点名称为:"
+PATH +
"/"
+ serviceName +
"/"
+ ip);
32.
}
33.
//提供服务
34.
public
void
provide(){
35.
36.
}
37.
public
static
void
main(String[]args)
throws
Exception {
38.
ServiceAProvider service =
new
ServiceAProvider();
39.
service.init();
40.
41.
Thread.sleep(
1000
*
60
*
60
*
24
);
42.
}
43.
44.
}
服务提供者ServiceBProvider类源码为
01.
package
com.jamesfen.zookeeper;
02.
import
java.net.InetAddress;
03.
import
org.I0Itec.zkclient.ZkClient;
04.
public
class
ServiceBProvider {
05.
//服务名仍然为 A,这样是为了,一个服务名有两个台机器在服务,才能做负载均衡.
06.
private
String serviceName =
"service-A"
;
07.
08.
//向zookeeper注册服务
09.
public
void
init()
throws
Exception{
10.
String serverList =
"192.168.58.11:2181"
;
11.
String PATH =
"/configcenter"
;
//根节点路径
12.
ZkClient zkClient =
new
ZkClient(serverList);
13.
boolean
rootExists = zkClient.exists(PATH);
14.
if
(!rootExists){
15.
zkClient.createPersistent(PATH);
16.
}
17.
18.
boolean
serviceExists = zkClient.exists(PATH +
"/"
+ serviceName);
19.
if
(!serviceExists){
20.
zkClient.createPersistent(PATH +
"/"
+ serviceName);
//創建服務節點
21.
}
22.
23.
//註冊當前服務
24.
InetAddress addr =InetAddress.getLocalHost();
25.
//String ip= addr.getHostAddress().toString();
26.
String ip =
"192.168.58.131"
;
27.
28.
//創建當前服務器節點
29.
zkClient.createEphemeral(PATH +
"/"
+ serviceName +
"/"
+ ip);
30.
31.
System.out.println(
"提供的服务节点名称为:"
+PATH +
"/"
+ serviceName +
"/"
+ ip);
32.
}
33.
//提供服务
34.
public
void
provide(){
35.
36.
}
37.
public
static
void
main(String[]args)
throws
Exception {
38.
ServiceBProvider service =
new
ServiceBProvider();
39.
service.init();
40.
41.
Thread.sleep(
1000
*
60
*
60
*
24
);
42.
}
43.
44.
}
消费者类源码为:01.
package
com.jamesfen.zookeeper;
02.
import
java.util.ArrayList;
03.
import
java.util.List;
04.
import
java.util.Random;
05.
import
org.I0Itec.zkclient.IZkChildListener;
06.
import
org.I0Itec.zkclient.ZkClient;
07.
public
class
ServiceConsumer {
08.
09.
private
List<String> serverList =
new
ArrayList<String>();
10.
11.
private
String serviceName =
"service-A"
;
12.
13.
//初始化服务地址信息
14.
public
void
init(){
15.
String zkServerList =
"192.168.58.11:2181"
;
16.
String SERVICE_PATH=
"/configcenter/"
+serviceName;
//服务节点路径
17.
ZkClient zkClient =
new
ZkClient(zkServerList);
18.
19.
boolean
serviceExists =zkClient.exists(SERVICE_PATH);
20.
if
(serviceExists){
21.
serverList =zkClient.getChildren(SERVICE_PATH);
22.
}
else
{
23.
throw
new
RuntimeException(
"service not exist!"
);
24.
}
25.
26.
//注册事件监听
27.
zkClient.subscribeChildChanges(SERVICE_PATH,
new
IZkChildListener(){
28.
//@Override
29.
public
void
handleChildChange(String parentPath, List<String> currentChilds)
throws
Exception{
30.
serverList = currentChilds;
31.
}
32.
});
33.
}
34.
35.
36.
//消费服务
37.
public
void
consume(){
38.
//通过负责均衡算法,得到一台服务器进行调用
39.
int
index = getRandomNum(
0
,
1
);
40.
System.out.println(
"调用"
+ serverList.get(index)+
"提供的服务:"
+ serviceName);
41.
}
42.
43.
public
int
getRandomNum(
int
min,
int
max){
44.
Random rdm =
new
Random();
45.
return
rdm.nextInt(max-min+
1
)+min;
46.
}
47.
48.
public
static
void
main(String[] args)
throws
Exception {
49.
ServiceConsumer consumer =
new
ServiceConsumer();
50.
51.
consumer.init();
52.
consumer.consume();
53.
54.
Thread.sleep(
1000
*
60
*
60
*
24
);
55.
}
56.
57.
}
更多推荐
已为社区贡献1条内容
所有评论(0)