代码已上传到Github,有兴趣的同学可以下载来看看(git版本号:45eb8ccf3ebe3872194dd7161eaf41780207a8a7
):https://github.com/ylw-github/Zookeeper-Demo
现在很多时候我们的服务需要7*24小时工作,假如一台机器挂了,我们希望能有其它机器顶替它继续工作。此类问题现在多采用master-salve模式,也就是常说的主从模式,正常情况下主机提供服务,备机负责监听主机状态,当主机异常时,可以自动切换到备机继续提供服务(这里有点儿类似于数据库主库跟备库,备机正常情况下只监听,不工作),这个切换过程中选出下一个主机的过程就是master选举。
对于以上提到的场景,传统的解决方式是采用一个备用节点,这个备用节点定期给当前主节点发送ping包,主节点收到ping包后会向备用节点发送应答ack,当备用节点收到应答,就认为主节点还活着,让它继续提供服务,否则就认为主节点挂掉了,自己将开始行使主节点职责。如图1所示:
下面我们开始来编写代码:
1.添加maven依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.ElectionMaster标志类(记录是否为主节点):
package com.ylw.zookeeper.slave; import org.springframework.stereotype.Component; @Component public class ElectionMaster { // 服务器info信息 是否存活 public static boolean isSurvival; }
3.MyApplicationRunner(节点监听以及节点操作):
package com.ylw.zookeeper.slave; import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class MyApplicationRunner implements ApplicationRunner { // 创建zk连接 ZkClient zkClient = new ZkClient("192.168.162.131:2181"); private String path = "/election"; @Value("${server.port}") private String serverPort; public void run(ApplicationArguments args) throws Exception { System.out.println("项目启动完成..."); createEphemeral(); // 创建事件监听 zkClient.subscribeDataChanges(path, new IZkDataListener() { // 节点被删除 public void handleDataDeleted(String dataPath) throws Exception { // 主节点已经挂了,重新选举 System.out.println("主节点已经挂了,重新开始选举"); createEphemeral(); } public void handleDataChange(String dataPath, Object data) throws Exception { } }); } private void createEphemeral() { try { zkClient.createEphemeral(path, serverPort); ElectionMaster.isSurvival = true; System.out.println("serverPort:" + serverPort + ",选举成功...."); } catch (Exception e) { ElectionMaster.isSurvival = false; } } }
4.启动类及Controller:
IndexController:
package com.ylw.zookeeper.slave; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IndexController { @Value("${server.port}") private String serverPort; @RequestMapping("/getServerInfo") public String getServerInfo() { return "serverPort:" + serverPort + (ElectionMaster.isSurvival ? "选举为主服务器" : "该服务器为从服务器"); } }
App启动类:
package com.ylw.zookeeper.slave; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5.application.yml(参与选举的3台服务器,8080、8081、8082):
server: port: 8080 #port: 8081 #port: 8082
6.创建3个APP(菜单栏Edit Configurations…):
7.运行服务:
运行8080服务:
运行8081服务:
运行8082服务:
8.测试:
浏览器打开:
- http://localhost:8080/getServerInfo
- http://localhost:8081/getServerInfo
- http://localhost:8082/getServerInfo
可以看到8080为Master,其余两个为从服务器:
现在把8080服务器关闭,可以看到8082服务器选举为主服务器了:
修改配置,再次打开8080服务器,可以看到8080服务器被选为从服务器了: