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>
<!-- mvnrepository/artifact/keeper/zookeeper -->
<dependency>
<groupId>keeper</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.ServerSocket;
import java.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 (ists(path)) {
zkClient.delete(path);
}
String value="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();
springcloud和springboot}
} 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.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 = adLine();
if ("exit".equals(name)) {
}
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 = Children(memberServerPath);
listServer.clear();
for (String p : children) {
/
/ 读取⼦节点value值
listServer.add((String) adData(memberServerPath + "/" + p));
}
System.out.println("最新服务信息listServer:" + String());
// 订阅⼦节点事件
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) adData(memberServerPath + "/" + subP));
}
}
});
}
// 服务调⽤次数
private static int count = 1;
// 会员服务集数量
private static int memberServerCount = 2;
// 获取当前server信息
public static String getServer() {
//轮训采⽤的是取模算法
String serverName = (count % memberServerCount);
++count;
return serverName;
}
public void send(String name) {
String server = Server();
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 InputStream()));
out = new OutputStream(), 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.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(InputStream())); out = new PrintWriter(OutputStream(), 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;
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论