java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Cloud Consul选举机制

Spring Cloud Consul实现选举机制的代码工程

作者:HBLOG

Spring Cloud Consul 是 Spring Cloud 提供的对 HashiCorp Consul 的支持,它是一种基于服务网格的工具,用于实现服务注册、发现、配置管理和健康检查,本文给大家介绍了如何用Spring Cloud Consul实现选举机制,需要的朋友可以参考下

1.什么是Spring Cloud Consul?

Spring Cloud Consul 是 Spring Cloud 提供的对 HashiCorp Consul 的支持。它是一种基于服务网格的工具,用于实现服务注册、发现、配置管理和健康检查。 主要功能包括:

Spring Cloud Consul 的选举机制

Spring Cloud Consul 的选举机制基于 Consul 会话(Session)键值存储(Key/Value Store) 实现分布式领导选举。

工作原理:

2.环境搭建

run Consul Agent

docker run -d --name=dev-consul -p 8500:8500 consul

web ui

http://localhost:8500

3.代码工程

实验目标

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>LeaderElection</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Cloud Starter Consul Discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-core</artifactId>
        </dependency>

    </dependencies>

</project>

LeaderElectionConfig.java

package com.et;

import jakarta.annotation.PreDestroy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.web.client.RestTemplate;

@Configuration
public class LeaderElectionConfig {

    private static final String LEADER_KEY = "service/leader";
    private static final String CONSUL_URL = "http://localhost:8500";
    private String sessionId;


    @Bean
    @InboundChannelAdapter(value = "leaderChannel", poller = @Poller(fixedDelay = "5000"))
    public MessageSource<String> leaderMessageSource() {
        return () -> {
            // Implement logic to check if this instance is the leader
            boolean isLeader = checkLeadership();
            return MessageBuilder.withPayload(isLeader ? "I am the leader" : "I am not the leader").build();
        };
    }

    @Bean
    @ServiceActivator(inputChannel = "leaderChannel")
    public MessageHandler leaderMessageHandler() {
        return new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                System.out.println(message.getPayload());
                // Implement logic to perform leader-specific tasks
            }
        };
    }


    private final RestTemplate restTemplate = new RestTemplate();

    public LeaderElectionConfig() {
        this.sessionId = createSession();
    }

    private String createSession() {
        String url = CONSUL_URL + "/v1/session/create";
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<>("{\"Name\": \"leader-election-session\"}", headers);
        //ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
        //  PUT
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, entity, String.class);
        // Extract session ID from response
        return response.getBody().split("\"")[3]; // This is a simple way to extract the session ID
    }

    public boolean checkLeadership() {
        String url = CONSUL_URL + "/v1/kv/" + LEADER_KEY + "?acquire=" + sessionId;
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.PUT, entity, Boolean.class);
        return Boolean.TRUE.equals(response.getBody());
    }
    public void releaseLeadership() {
        String url = CONSUL_URL + "/v1/kv/" + LEADER_KEY + "?release=" + sessionId;
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.PUT, entity, Boolean.class);
        if (Boolean.TRUE.equals(response.getBody())) {
            System.out.println("Released leadership successfully");
        } else {
            System.out.println("Failed to release leadership");
        }
    }
    @PreDestroy
    public void onExit() {
        releaseLeadership();
    }
}

代码解释

LeaderElectionApplication.java

package com.et;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.integration.config.EnableIntegration;

@SpringBootApplication
@EnableDiscoveryClient
@EnableIntegration
public class LeaderElectionApplication {

    public static void main(String[] args) {
        SpringApplication.run(LeaderElectionApplication.class, args);
    }
}

配置文件

node1

server.port=8081
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.register=true
spring.application.name=leader-election-example
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}

node2

server.port=8082
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.register=true
spring.application.name=leader-election-example
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}

以上只是一些关键代码。

4.测试

启动node1节点

java -jar myapp.jar --spring.profiles.active=node1

启动node2节点

java -jar myapp.jar --spring.profiles.active=node2

通过控制台观察日志,其中只有一台机器能选为主机

以上就是Spring Cloud Consul实现选举机制的代码工程的详细内容,更多关于Spring Cloud Consul选举机制的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文