使用zookeeper实现服务路由和负载均衡
[*]三个类:
ServiceAProvider
ServiceBProvider
ServiceConsumer
其中
ServiceAProvider提供的服务名service-A,指向IP为192.168.58.130
ServiceBProvider提供的服务名service-A,指向IP为192.168.58.131
当有消费者请求时,随机地选取service-A列表的服务器提供服务
ServiceConsumer 为消费者类
依赖:
viewsourceprint?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类的源码为:
viewsourceprint?01.package com.jamesfen.zookeeper;02.import java.net.InetAddress;03.import org.I0Itec.zkclient.ZkClient;04.public class ServiceAProvider{05. 06.private StringserviceName = "service-A";07. 08.//向zookeeper注册服务09.public void init() throws Exception{10.StringserverList = "192.168.58.11:2181";11.StringPATH ="/configcenter";//根节点路径12.ZkClientzkClient = 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.InetAddressaddr =InetAddress.getLocalHost();25.//Stringip= addr.getHostAddress().toString();26.Stringip = "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.ServiceAProviderservice = new ServiceAProvider();39.service.init();40. 41.Thread.sleep(1000*60*60*24);42.}43. 44.}
服务提供者ServiceBProvider类源码为
viewsourceprint?01.package com.jamesfen.zookeeper;02.import java.net.InetAddress;03.import org.I0Itec.zkclient.ZkClient;04.public class ServiceBProvider{05.//服务名仍然为A,这样是为了,一个服务名有两个台机器在服务,才能做负载均衡.06.private StringserviceName = "service-A";07. 08.//向zookeeper注册服务09.public void init() throws Exception{10.StringserverList = "192.168.58.11:2181";11.StringPATH ="/configcenter";//根节点路径12.ZkClientzkClient = 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.InetAddressaddr =InetAddress.getLocalHost();25.//Stringip= addr.getHostAddress().toString();26.Stringip = "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.ServiceBProviderservice = new ServiceBProvider();39.service.init();40. 41.Thread.sleep(1000*60*60*24);42.}43. 44.}
消费者类源码为:
viewsourceprint?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 StringserviceName ="service-A";12. 13.//初始化服务地址信息14.public void init(){ 15.StringzkServerList ="192.168.58.11:2181";16.StringSERVICE_PATH="/configcenter/"+serviceName;//服务节点路径17.ZkClientzkClient = 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("servicenot exist!");24.}25. 26.//注册事件监听27.zkClient.subscribeChildChanges(SERVICE_PATH,new IZkChildListener(){28.//@Override29.public void handleChildChange(StringparentPath, 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.Randomrdm = new Random(); 45.return rdm.nextInt(max-min+1)+min;46.} 47. 48.public static void main(String[]args)throws Exception{49.ServiceConsumerconsumer = new ServiceConsumer();50. 51.consumer.init();52.consumer.consume();53. 54.Thread.sleep(1000*60*60*24);55.}56. 57.}
页:
[1]