Consul简单使用以及集群选举原理

是什么

Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。
提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
它具有很多优点。包括: 基于 raft 协议,比较简洁; 支持健康检查, 同时支持 HTTP 和 DNS 协议 支持跨数据中心的 WAN 集群 提供图形界面 跨平台,支持 Linux、Mac、Windows

功能有哪些

服务注册中心、健康监测、K\V存储、多数据中心、可视化管理

Consul服务搭建

本次在windows下演示单机模式,集群搭建参考官网或者问问chatGPT

image-20230217153543125

下载

根据系统环境下载对应的安装包,windows直接就是一个.exe文件

运行

  1. 在exe目录中打开cmd,执行命令
consul agent -dev    
#只能本地(localhost:8500\127.0.0.1:8500)可以访问
#要想通过ip可以访问,使用下面的使用即可
consul agent -dev   -client 0.0.0.0 -ui  
#指定ip可以访问
  1. 浏览器打开http://127.0.0.1:8500/

    能打开即为成功

生产者消费者服务搭建

生产者&消费者POM

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>3.0.2</version>
        </dependency>

生产者Yml

server:
  port: 7777
spring:
  application:
    name: my-producer #注册到consul的服务名称
  cloud:
    consul:
      host: 10.20.30.94:8500
      port: 8500
      discovery:
        service-name: ${spring.application.name}

消费者Yml

server:
  port: 6655
spring:
  application:
    name: my-customer-6655 #注册到consul的服务名称
  cloud:
    consul:
      host: 10.20.30.94:8500
      port: 8500
      discovery:
        service-name: ${spring.application.name}

消费者java代码

Config

package com.rb.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextBean
{
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}

Controller

package com.rb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CustomerController {
    public static final String URL = "http://my-producer";
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/customer/getMethod")
    public String getMethod(String val){
        String result = restTemplate.getForObject(URL+"/producer/getMethod?val="+val, String.class);
        return result;
    }
}

生产者java代码

Controller

package com.rb.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerController {
    @GetMapping("/producer/getMethod")
    public String getMethod(String val){
        return val+"7777";
    }
}

启动类

package com.rb;

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

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class,args);
    }
}

测试

启动生产者&消费者

如全部搭建成功在Consul页面可看到

image-20230217160728425

并且通过消费者可访问到生产者的方法

Consul集群选举原理

Consul使用基于Raft协议的一致性算法来实现分布式集群中的领导者选举机制。Raft算法是一种分布式一致性算法,它将集群中的节点划分为领导者、跟随者和候选人。

当一个节点启动时,它首先成为候选人,并向其他节点发送投票请求。如果一个节点接受了该候选人的请求,它会将投票返回给该候选人。如果该候选人收到了集群中大多数节点的投票,它将成为新的领导者。

一旦选举完成,Consul中的所有更改将由新的领导者发起,并通过Raft协议进行复制。在领导者失效或网络分区等情况下,节点将启动新的选举流程,以选择新的领导者。

需要注意的是,为了保证集群的可用性,Consul集群中应该至少有3个节点。这样,在发生节点失效或网络分区等情况时,集群仍然能够正常运行,并保持数据的一致性。

总之,Consul使用Raft算法来实现领导者选举机制,保证了集群中节点的高可用性和数据的一致性。

posted @ 2023-02-17 16:14  RollBack2010  阅读(87)  评论(0编辑  收藏  举报