使用zookeeper实现服务路由和负载均衡

三个类:

ServiceAProvider

ServiceBProvider

ServiceConsumer

其中

ServiceAProvider提供的服务名service-A,指向IP为192.168.58.130

ServiceBProvider提供的服务名service-A,指向IP为192.168.58.131

当有消费者请求时,随机地选取service-A列表的服务器提供服务

ServiceConsumer 为消费者类

依赖:

<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.5-cdh5.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.helix</groupId>
			<artifactId>helix-core</artifactId>
			<version>0.6.4</version>
		</dependency>


github源码下载地址:

https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zookeeper


详见下面的代码:

服务提供者ServiceAProvider类的源码为:

package com.jamesfen.zookeeper;
import <span id="15_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="15_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=java&k0=java&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="15" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">java</span></a></span>.net.InetAddress;
import org.I0Itec.zkclient.ZkClient;
public class ServiceAProvider {

	 private String serviceName = "service-A";
	   
	    //向zookeeper注册服务
	    public void init() throws Exception{
	        String <span id="16_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="16_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=server&k0=server&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="16" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">server</span></a></span>List = "192.168.58.11:2181";
	        String PATH ="/configcenter";//根节点路径
	        ZkClient zkClient = new ZkClient(serverList);
	        boolean rootExists = zkClient.exists(PATH);
	        if(!rootExists){
	            zkClient.createPersistent(PATH);
	        }
	      //判断是否存在,不存在则创建服务节点
	        boolean serviceExists = zkClient.exists(PATH + "/" + serviceName);
	        if(!serviceExists){
	            zkClient.createPersistent(PATH + "/" + serviceName);
	        }
	        
	        //註冊當前服務
	        InetAddress addr =InetAddress.getLocalHost();
	        //String ip= addr.getHostAddress().toString();
	        String ip = "192.168.58.130";
	        
	        //創建當前服務器節點
	        zkClient.createEphemeral(PATH + "/" + serviceName + "/" + ip);
	        
	        System.out.println("提供的服务节点名称为:"+PATH + "/" + serviceName + "/" + ip);
	    }
	    //提供服务
	    public void provide(){
	        
	    }
	    public static void main(String[]args) throws Exception {
	        ServiceAProvider service = new ServiceAProvider();
	        service.init();
	        
	        Thread.sleep(1000*60*60*24);
	    }

}

服务提供者ServiceBProvider类源码为

package com.jamesfen.zookeeper;
import <span id="10_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=java&k0=java&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="10" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">java</span></a></span>.net.InetAddress;
import org.I0Itec.zkclient.ZkClient;
public class ServiceBProvider {
   //服务名仍然为 A,这样是为了,一个服务名有两个台机器在服务,才能做<span id="11_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=%B8%BA%D4%D8%BE%F9%BA%E2&k0=%B8%BA%D4%D8%BE%F9%BA%E2&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="11" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">负载均衡</span></a></span>.
	private String serviceName = "service-A";
	   
    //向zookeeper注册服务
    public void init() throws Exception{
        String <span id="12_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=server&k0=server&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="12" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">server</span></a></span>List = "192.168.58.11:2181";
        String PATH ="/configcenter";//根节点路径
        ZkClient zkClient = <span id="13_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=new&k0=new&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="13" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">new</span></a></span> ZkClient(serverList);
        boolean rootExists = zkClient.exists(PATH);
        if(!rootExists){
            zkClient.createPersistent(PATH);
        }
        
        boolean serviceExists = zkClient.exists(PATH + "/" + serviceName);
        if(!serviceExists){
            zkClient.createPersistent(PATH + "/" + serviceName);//創建服務節點
        }
        
        //註冊當前服務
        InetAddress addr =InetAddress.getLocalHost();
        //String ip= addr.getHostAddress().toString();
        String ip = "192.168.58.131";
        
        //創建當前服務器節點
        zkClient.createEphemeral(PATH + "/" + serviceName + "/" + ip);
        
        System.out.println("提供的服务节点名称为:"+PATH + "/" + serviceName + "/" + ip);
    }
    //提供服务
    public void provide(){
        
    }
    public static void main(String[]args) throws Exception {
        ServiceBProvider service = <span id="14_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=new&k0=new&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="14" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">new</span></a></span> ServiceBProvider();
        service.init();
        
        Thread.sleep(1000*60*60*24);
    }

}

消费者类 源码 为:

package com.jamesfen.zookeeper;
import <span id="0_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="0_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=java&k0=java&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="0" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">java</span></a></span>.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
public class ServiceConsumer {

	private List<String> <span id="1_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=server&k0=server&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="1" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">server</span></a></span>List = <span id="2_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=new&k0=new&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="2" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">new</span></a></span> ArrayList<String>();
	   
    private String serviceName ="service-A";
   
    //初始化服务地址信息
    public void init(){     
        String zkServerList ="192.168.58.11:2181";
        String SERVICE_PATH="/configcenter/"+serviceName;//服务节点路径
        ZkClient zkClient = new ZkClient(zkServerList);
        
        boolean serviceExists =zkClient.exists(SERVICE_PATH);
        if(serviceExists){
            <span id="3_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=server&k0=server&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="3" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">server</span></a></span>List =zkClient.getChildren(SERVICE_PATH);
        }else{
            throw <span id="4_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=new&k0=new&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="4" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">new</span></a></span> RuntimeException("service not exist!");
        }
        
        //注册事件监听
        zkClient.subscribeChildChanges(SERVICE_PATH,new IZkChildListener(){
            //@Override
            public void handleChildChange(String parentPath, List<String> currentChilds)throws Exception{
                serverList = currentChilds;
            }      
        });
    }
   
   
    //消费服务
    public void consume(){
        //通过负责均衡<span id="5_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=%CB%E3%B7%A8&k0=%CB%E3%B7%A8&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="5" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">算法</span></a></span>,得到一台<span id="6_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=%B7%FE%CE%F1%C6%F7&k0=%B7%FE%CE%F1%C6%F7&kdi0=0&luki=5&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="6" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">服务器</span></a></span>进行调用
        int index = getRandomNum(0,1);      
        System.out.println("调用" + <span id="7_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=server&k0=server&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="7" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">server</span></a></span>List.get(index)+"提供的服务:" + serviceName);
    }
   
    public int getRandomNum(int min,int <span id="8_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="8_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=max&k0=max&kdi0=0&luki=3&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="8" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">max</span></a></span>){  
        Random rdm = <span id="9_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=c0f10e83569e90ad&k=new&k0=new&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=ad909e56830ef1c0&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1686059%2Ehtml&urlid=0" target="_blank" mpid="9" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">new</span></a></span> Random();  
        return rdm.nextInt(max-min+1)+min;
    }  
   
    public static void main(String[] args)throws Exception {
        ServiceConsumer consumer = new ServiceConsumer();   
               
        consumer.init();
        consumer.consume();
        
        Thread.sleep(1000*60*60*24);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值