Zookeeper实现负载均衡

在Nginx和SpringCloud中都可以实现负载均衡,在Zookeeper中也可以实现负载均衡

 

Zookeeper实现负载均衡:

 

 

 

生产者集群,创建一个/path的父节点这个节点是持久节点,集群中的每个生产者分别在父节点中创建子节点(示例根据端口创建) 生产者一创建节点/path/producer1 生产者二创建节点/path/producer2|(节点的值都是ip地址和端口),该节点是临时节点,zookeeper断开连接则该节点消失。消费者可以通过获取zookeeper中/path父节点下面的所有子节点信息。然后根据自己的需求在本地实现负载均衡策略(ip绑定,权重,轮训)

 

java 操作zookeeper实现负载均衡

  导入所需要的依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

   创建socket服务器端模拟生产者,socket客户端作为消费者,客户端从zookeeper节点信息里面获取所有节点的ip和端口,来确定去连接哪一个socket服务器

  创建socket服务端:

package com.izk;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.I0Itec.zkclient.ZkClient;

//##ServerScoekt服务端
public class ZkServerScoekt implements Runnable {
    private int port = 8081;

    public static void main(String[] args) throws IOException {
        int port = 8081;
        ZkServerScoekt server = new ZkServerScoekt(port);
        Thread thread = new Thread(server);
        thread.start();
    }

    public ZkServerScoekt(int port) {
        this.port = port;
    }

    // 将服务信息注册到注册中心上去
    public void regServer() {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 6000, 1000);
        String path = "/member/server-" + port;
        //检测是否存在这个节点
        if (zkClient.exists(path)) {
            zkClient.delete(path);
        }
        String value="127.0.0.1:" + port;
        zkClient.createEphemeral(path, "127.0.0.1:" + port);
        System.out.println("##注册节点信息值###"+value);
    }

    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            regServer();
            System.out.println("Server start port:" + port);
            Socket socket = null;
            while (true) {
                socket = serverSocket.accept();
                new Thread(new ServerHandler(socket)).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (Exception e2) {

            }
        }
    }

}

 

 创建socket的客户端(消费者):

package com.izk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

public class ZkServerClient {
    // 从zookeeper中获取到的子节点的信息
    public static List<String> listServer = new ArrayList<String>();

    public static void main(String[] args) {
        initServer();
        ZkServerClient client = new ZkServerClient();
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String name;
            try {
                name = console.readLine();
                if ("exit".equals(name)) {
                    System.exit(0);
                }
                client.send(name);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 注册所有server
    public static void initServer() {
        listServer.clear();
        // 从zk获取最新获取的注册服务连接
        final String memberServerPath = "/member";
        final ZkClient zkClient = new ZkClient("127.0.0.1:2181", 6000, 1000);
        // 获当前下子节点
        List<String> children = zkClient.getChildren(memberServerPath);
        listServer.clear();
        for (String p : children) {
            // 读取子节点value值
            listServer.add((String) zkClient.readData(memberServerPath + "/" + p));
        }
        System.out.println("最新服务信息listServer:" + listServer.toString());
        // 订阅子节点事件
        zkClient.subscribeChildChanges(memberServerPath, new IZkChildListener() {
            // 子节点发生变化
            public void handleChildChange(String parentPath, List<String> childrens) throws Exception {
                listServer.clear();
                for (String subP : childrens) {
                    // 读取子节点value值
                    listServer.add((String) zkClient.readData(memberServerPath + "/" + subP));
                }
            }
        });
    }

    // 服务调用次数
    private static int count = 1;
    // 会员服务集群数量
    private static int memberServerCount = 2;

    // 获取当前server信息
    public static String getServer() {
        //轮训采用的是取模算法
        String serverName = listServer.get(count % memberServerCount);
        ++count;
        return serverName;
    }

    public void send(String name) {

        String server = ZkServerClient.getServer();
        String[] cfg = server.split(":");

        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            socket = new Socket(cfg[0], Integer.parseInt(cfg[1]));
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            out.println(name);
            while (true) {
                String resp = in.readLine();
                if (resp == null)
                    break;
                else if (resp.length() > 0) {
                    System.out.println("Receive : " + resp);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

package com.izk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

//ServerHandler
public class ServerHandler implements Runnable {
    private Socket socket;

    public ServerHandler(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            out = new PrintWriter(this.socket.getOutputStream(), true);
            String body = null;
            while (true) {
                body = in.readLine();
                if (body == null)
                    break;
                System.out.println("Receive : " + body);
                out.println("Hello, " + body);
            }

        } catch (Exception e) {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                out.close();
            }
            if (this.socket != null) {
                try {
                    this.socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                this.socket = null;
            }
        }
    }
}

 

posted on 2019-06-13 17:05  年少不知愁  阅读(3668)  评论(0编辑  收藏  举报