@Slf4j
@Component
public class JetcdElectionService implements ApplicationListener<ContextRefreshedEvent> {
private final Client jetcdClient;
private final String electionNameText = "/testElection";
private final String firstNonLoopbackAddress;
private final AtomicReference<Watch.Watcher> watcher = new AtomicReference<>();
private final AtomicBoolean connectionExceptionFlag = new AtomicBoolean();
private final ExecutorService executor = Executors.newSingleThreadExecutor();
public JetcdElectionService(Client jetcdClient, @Value("${server.port}") int port) {
this.jetcdClient = jetcdClient;
this.firstNonLoopbackAddress = "127.0.0.1:" + port;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
new Thread(this::run).start();
}
private void run() {
ByteSequence electionName = this.getElectionName(this.electionNameText);
ByteSequence proposal = this.getProposal(this.firstNonLoopbackAddress);
log.info("[Election] 正在执行选举 [electionName: {}, proposal: {}]...", electionName.toString(), proposal.toString());
Election electionClient = this.jetcdClient.getElectionClient();
electionClient.observe(electionName, new LeaderElectionListener(this::handleLeaderResponse, this.executor));
LeaderResponse leader = null;
try {
leader = electionClient.leader(electionName).get(); // etcd中没有leader会报错
} catch (Exception ignored) {
}
if (leader == null) {
log.info("[Election] 检测到leader不存在,当前实例正在尝试参选...");
this.doElect(electionName, proposal);
}
}
private boolean doElect(ByteSequence electionName, ByteSequence proposal) {
Lease leaseClient = this.jetcdClient.getLeaseClient();
Election electionClient = this.jetcdClient.getElectionClient();
LeaseGrantResponse lease;
try {
lease = leaseClient.grant(15).get();
} catch (InterruptedException | ExecutionException e) {
log.error("[Election] 选举时发生异常,无法获得租约!", e);
return false;
}
long leaseID = lease.getID();
try {
electionClient.campaign(electionName, leaseID, proposal).get(5, TimeUnit.SECONDS);
// leader选举后进行自动续约,请求发送周期为: lease.ttl / 3
leaseClient.keepAlive(leaseID, new LeaseKeepaliveObserver());
log.info("[Election] 选举完成,当前实例当选成功!");
return true;
} catch (InterruptedException | ExecutionException e) {
log.error("[Election] 选举时发生异常,无法获得租约!", e);
return false;
} catch (TimeoutException e) {
log.info("[Election] 当前实例未能选中,停止参选中.");
return false;
}
}
private void handleLeaderResponse(LeaderResponse response) {
Watch watchClient = this.jetcdClient.getWatchClient();
KeyValue kv = response.getKv();
ByteSequence proposalKey = kv.getKey(); // ${electionName}/${随机字符串}
log.info("[Election] 选举完成,当选实例信息[key: {}, value: {}]", proposalKey.toString(), kv.getValue().toString());
Watch.Watcher oldWatcher = this.watcher.get();
if (oldWatcher != null) {
oldWatcher.close();
}
Watch.Watcher newWatcher = watchClient.watch(proposalKey, new LeaderWatchListener(this::handleLeaderChange, this::handleWatchError, this.executor));
this.watcher.compareAndSet(oldWatcher, newWatcher);
}
private void handleLeaderChange(WatchResponse response) {
ByteSequence electionName = this.getElectionName(this.electionNameText);
ByteSequence proposal = this.getProposal(this.firstNonLoopbackAddress);
Election electionClient = this.jetcdClient.getElectionClient();
List<WatchEvent> events = response.getEvents();
WatchEvent watchEvent = null;
if (events != null && !events.isEmpty()) {
watchEvent = events.get(0);
}
WatchEvent.EventType eventType = Optional.ofNullable(watchEvent).map(WatchEvent::getEventType).orElse(null);
if (this.connectionExceptionFlag.get()) {
log.info("[Election] [{}] 检测到与etcd连接异常,重新注册observe服务.", eventType);
electionClient.observe(electionName, new LeaderElectionListener(this::handleLeaderResponse, this.executor));
this.connectionExceptionFlag.compareAndSet(true, false);
} else {
log.info("[Election] [{}] 检测到leader变动事件,当前实例正在尝试参选...", eventType);
}
this.doElect(electionName, proposal);
}
private void handleWatchError(Throwable throwable) {
// 发现和etcd连接出现了异常
this.connectionExceptionFlag.compareAndSet(false, true);
}
public ByteSequence getElectionName(String electionNameText) {
return ByteSequence.from(electionNameText, StandardCharsets.UTF_8);
}
public ByteSequence getProposal(String firstNonLoopbackAddress) {
return ByteSequence.from(firstNonLoopbackAddress, StandardCharsets.UTF_8);
}
private static class LeaderElectionListener implements Election.Listener {
private final Consumer<LeaderResponse> leaderResponseConsumer;
private final ExecutorService executor;
public LeaderElectionListener(Consumer<LeaderResponse> leaderResponseConsumer, ExecutorService executor) {
this.leaderResponseConsumer = leaderResponseConsumer;
this.executor = executor;
}
@Override
public void onNext(LeaderResponse response) {
CompletableFuture.runAsync(() -> this.leaderResponseConsumer.accept(response), this.executor).exceptionally((e) -> {
if (e != null) {
e.printStackTrace();
}
return null;
});
}
@Override
public void onError(Throwable throwable) {
log.error(throwable.getMessage(), new RuntimeException(throwable));
}
@Override
public void onCompleted() {
}
}
private static class LeaderWatchListener implements Watch.Listener {
private final Consumer<WatchResponse> leaderChangeConsumer;
private final Consumer<Throwable> onErrorConsumer;
private final ExecutorService executor;
public LeaderWatchListener(Consumer<WatchResponse> leaderChangeConsumer, Consumer<Throwable> onErrorConsumer, ExecutorService executor) {
this.leaderChangeConsumer = leaderChangeConsumer;
this.onErrorConsumer = onErrorConsumer;
this.executor = executor;
}
@Override
public void onNext(WatchResponse response) {
CompletableFuture.runAsync(() -> this.leaderChangeConsumer.accept(response), this.executor).exceptionally((e) -> {
if (e != null) {
log.error(e.getMessage(), e);
}
return null;
});
}
@Override
public void onError(Throwable throwable) {
RuntimeException t = new RuntimeException(throwable);
log.error(throwable.getMessage(), t);
this.onErrorConsumer.accept(t);
}
@Override
public void onCompleted() {
}
}
private static class LeaseKeepaliveObserver implements StreamObserver<LeaseKeepAliveResponse> {
@Override
public void onNext(LeaseKeepAliveResponse value) {
}
@Override
public void onError(Throwable throwable) {
log.error(throwable.getMessage(), new RuntimeException(throwable));
}
@Override
public void onCompleted() {
}
}
}
Jetcd 实现主从选举(示例代码)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Bully 算法实现 设定集群中有三个节点,通过Bully算法实现选主。节点之间的通信使用的是自我实现的Remot...
- 一、db设置 主写,从读,可部署多个提高“读”速度。写后从库也更新 1)master记录数据库操作日志到Bina...
- 记录方法 css代码 html代码 纯CSS3书本打开动画加载特效[https://sc.chinaz.com/j...