首页 > 其他分享 >Consul简单使用以及集群选举原理

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

时间:2023-02-17 16:22:05浏览次数:54  
标签:选举 Consul springframework 集群 import org public

目录

是什么

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算法来实现领导者选举机制,保证了集群中节点的高可用性和数据的一致性。

标签:选举,Consul,springframework,集群,import,org,public
From: https://www.cnblogs.com/rb2010/p/17130579.html

相关文章

  • HDFS数据(跨集群)迁移
    一、数据迁移使用场景1.冷热集群数据同步、分类存储2.整体数据整体搬迁3.数据准实时同步(备份)二、考量因素1.网络传输带宽及时间,是否会影响现有业务2.性能,单机?多......
  • docker nacos 集群 部署
    准备安装mysql(192.168.1.101)建库nacos_config安装docker(yum方式)安装nacosdocker(参看前一篇)集群部署方案三台Linux服务器nacos-server-01192.168.1.136nac......
  • IoT 边缘集群基于 Kubernetes Events 的告警通知实现(二):进一步配置
    上一篇文章IoT边缘集群基于KubernetesEvents的告警通知实现目标告警恢复通知-经过评估无法实现原因:告警和恢复是单独完全不相关的事件,告警是Warning级别......
  • 快速部署一个K8s集群——kubernetes v1.26,kubeadm方式
    快速部署一个K8s集群——kubernetesv1.26,kubeadm方式1、前置知识点1.1生产环境可部署Kubernetes集群的两种方式目前生产部署Kubernetes集群主要有两种方式:•kubea......
  • docker-compose搭建redis-Cluster集群
    环境选择三台机器创建相同目录级:/database/redis/在目录里创建文件:viredis-cluster.tmpl查看防火墙--如果防火墙的状态是打开的记得开端口文件配置redis-clust......
  • 云原生-k8s-云原生开源K8s系统,面向大规模生产集群
    11月4日,在腾讯数字生态大会上,腾讯宣布了云原生领域一项重磅开源进展——K8s多集群管理项目Clusternet正式开源。Clusternet由腾讯联合多点生活、QQ音乐、富途证券、......
  • hadoop+hive+mysql+sqoop+spark完全分布式集群搭建
    hadoop+hive+mysql+sqoop+spark完全分布式集群搭建零、配置网络(固定ip)(可以不做,但是后面关闭后ip会重复变动,后面步骤中有再次提到,后面操作在做)1.固定ip因centos 7 ip......
  • IoT 边缘集群基于 Kubernetes Events 的告警通知实现
    背景边缘集群(基于树莓派+K3S)需要实现基本的告警功能。边缘集群限制CPU/内存/存储资源紧张,无法支撑至少需要2GB以上内存和大量存储的基于Prometheus的完整监......
  • redis搭建集群
    ##################    添加从库点给指定主库:[[email protected]_7001]$./bin/redis-cli-a'jJAV0kTokNb8iZvwfqniCxmFZEsbOH5n'--clusterad......
  • hadoop集群配置
    进入hadoop的etc目录下找到配置文件cd /opt/module/hadoop-3.1.3/etc/hadoop   配置core-site.xml指定NameNode的地址指定hadoop数据的存储目录<configurati......