|
| 1 | +package demo.master; |
| 2 | + |
| 3 | +import demo.ZookeeperProperties; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Random; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import javax.annotation.PostConstruct; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.apache.curator.framework.CuratorFramework; |
| 10 | +import org.apache.curator.framework.recipes.leader.LeaderSelector; |
| 11 | +import org.apache.curator.framework.recipes.leader.LeaderSelectorListener; |
| 12 | +import org.apache.curator.framework.state.ConnectionState; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.context.annotation.Profile; |
| 15 | +import org.springframework.http.ResponseEntity; |
| 16 | +import org.springframework.web.bind.annotation.GetMapping; |
| 17 | +import org.springframework.web.bind.annotation.PathVariable; |
| 18 | +import org.springframework.web.bind.annotation.RestController; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author zacconding |
| 22 | + * @Date 2018-12-14 |
| 23 | + * @GitHub : https://github.com/zacscoding |
| 24 | + */ |
| 25 | +@Slf4j(topic = "[MASTER-SLAVE]") |
| 26 | +@Profile("master-slave") |
| 27 | +@RestController |
| 28 | +public class MasterSlaveTaskController { |
| 29 | + |
| 30 | + private ZookeeperProperties zookeeperProperties; |
| 31 | + private CuratorFramework curatorFramework; |
| 32 | + private String mutexPath = "/mutex/select/leader/job/"; |
| 33 | + private String fixedTaskMutexPath = "/mutex/select/leader/job/A"; |
| 34 | + private LeaderSelector leaderSelector; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + public MasterSlaveTaskController(CuratorFramework curatorFramework, |
| 38 | + ZookeeperProperties zookeeperProperties) { |
| 39 | + |
| 40 | + this.curatorFramework = curatorFramework; |
| 41 | + this.zookeeperProperties = zookeeperProperties; |
| 42 | + } |
| 43 | + |
| 44 | + @PostConstruct |
| 45 | + private void setUp() { |
| 46 | + leaderSelector = new LeaderSelector( |
| 47 | + curatorFramework, |
| 48 | + fixedTaskMutexPath, |
| 49 | + new LeaderSelectorListener() { |
| 50 | + @Override |
| 51 | + public void takeLeadership(CuratorFramework internalCuratorFramework) |
| 52 | + throws Exception { |
| 53 | + |
| 54 | + log.info("this app`s client : {}", curatorFramework); |
| 55 | + log.info("[{}] task leadership.... client : {}" |
| 56 | + , zookeeperProperties.getClientId(), internalCuratorFramework); |
| 57 | + |
| 58 | + TimeUnit.SECONDS.sleep(2L); |
| 59 | + |
| 60 | + System.exit(-1); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void stateChanged(CuratorFramework curatorFramework, |
| 65 | + ConnectionState connectionState) { |
| 66 | + |
| 67 | + log.info("this app`s client : {}", curatorFramework); |
| 68 | + log.info("[{}] state changed...... >> {}" |
| 69 | + , zookeeperProperties.getClientId(), connectionState); |
| 70 | + } |
| 71 | + } |
| 72 | + ); |
| 73 | + leaderSelector.start(); |
| 74 | + leaderSelector.autoRequeue(); |
| 75 | + |
| 76 | + Thread leaderChecker = new Thread(() -> { |
| 77 | + try { |
| 78 | + while (!Thread.currentThread().isInterrupted()) { |
| 79 | + log.info("[Check leader - {}] id : {}, leader id : {}, isLeader : {}\n{}" |
| 80 | + , zookeeperProperties.getClientId(), |
| 81 | + leaderSelector.getId(), |
| 82 | + leaderSelector.getLeader().getId(), |
| 83 | + leaderSelector.getLeader().isLeader(), |
| 84 | + Arrays.toString(leaderSelector.getParticipants().toArray()) |
| 85 | + ); |
| 86 | + |
| 87 | + TimeUnit.SECONDS.sleep(1L); |
| 88 | + } |
| 89 | + } catch (Exception e) { |
| 90 | + Thread.currentThread().interrupt(); |
| 91 | + } |
| 92 | + }); |
| 93 | + leaderChecker.start(); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + @GetMapping("/master-slave/push/task/{taskNumber}") |
| 98 | + public ResponseEntity<Boolean> pushTask(@PathVariable("taskNumber") long taskNumber) { |
| 99 | + log.info("## [Push task. client id : {} | task number : {}\n{}", |
| 100 | + zookeeperProperties.getClientId(), taskNumber, curatorFramework); |
| 101 | + |
| 102 | + new LeaderSelector(curatorFramework, mutexPath + taskNumber, |
| 103 | + new LeaderSelectorListener() { |
| 104 | + @Override |
| 105 | + public void takeLeadership(CuratorFramework internalCuratorFramework) |
| 106 | + throws Exception { |
| 107 | + log.info("## [{}] - taskLeadership at client : {} - {}" |
| 108 | + , zookeeperProperties.getClientId(), taskNumber, internalCuratorFramework); |
| 109 | + |
| 110 | + int sleep = new Random().nextInt(3) + 1; |
| 111 | + TimeUnit.SECONDS.sleep(sleep); |
| 112 | + |
| 113 | + log.info("## [{}] - complete task : {}" |
| 114 | + , zookeeperProperties.getClientId(), taskNumber); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void stateChanged(CuratorFramework curatorFramework, |
| 119 | + ConnectionState connectionState) { |
| 120 | + log.info("## [{}] stateChanged at client >> {}" |
| 121 | + , zookeeperProperties.getClientId(), connectionState); |
| 122 | + } |
| 123 | + }).start(); |
| 124 | + |
| 125 | + return ResponseEntity.ok(Boolean.TRUE); |
| 126 | + } |
| 127 | +} |
0 commit comments