Spring Cloud(二) Consul 服务治理实现

Stella981
• 阅读 632

Spring Cloud Consul 项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具有分布式、高可用、高扩展性。

Consul 简介

Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式”
,内置了服务注册与发现框 架、具有以下性质:

  • 分布一致性协议实现、
  • 健康检查、
  • Key/Value存储、
  • 多数据中心方案,

不再需要依赖其他工具(比如ZooKeeper等)。

使用起来也较 为简单。Consul使用Go语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合 。
基于 Mozilla Public License 2.0 的协议进行开源. Consul 支持健康检查,并允许 HTTP 和 DNS 协议调用 API 存储键值对.
一致性协议采用 Raft 算法,用来保证服务的高可用. 使用 GOSSIP 协议管理成员和广播消息, 并且支持 ACL 访问控制.

Consul 的使用场景

  • docker 实例的注册与配置共享
  • coreos 实例的注册与配置共享
  • vitess 集群
  • SaaS 应用的配置共享
  • 与 confd 服务集成,动态生成 nginx 和 haproxy 配置文件

Consul 的优势

使用 Raft 算法来保证一致性, 比复杂的 Paxos 算法更直接. 相比较而言, zookeeper 采用的是 Paxos, 而 etcd 使用的则是 Raft.
支持多数据中心,内外网的服务采用不同的端口进行监听。 多数据中心集群可以避免单数据中心的单点故障,而其部署则需要考虑网络延迟, 分片等情况等. zookeeper 和 etcd 均不提供多数据中心功能的支持.
支持健康检查. etcd 不提供此功能.
支持 http 和 dns 协议接口. zookeeper 的集成较为复杂, etcd 只支持 http 协议.
官方提供web管理界面, etcd 无此功能.

Consul 的角色

client: 客户端, 无状态, 将 HTTP 和 DNS 接口请求转发给局域网内的服务端集群.server: 服务端, 保存配置信息, 高可用集群, 在局域网内与本地客户端通讯, 通过广域网与其他数据中心通讯. 每个数据中心的 server 数量推荐为 3 个或是 5 个.

由于Spring Cloud Consul项目的实现,我们可以轻松的将基于Spring Boot的微服务应用注册到Consul上,并通过此实现微服务架构中的服务治理。

搭建环境

参考

要想利用Consul提供的服务实现服务的注册与发现,我们需要搭建Consul Cluster 环境。

在Consul方案中,每个提供服务的节点上都要部署和运行Consul的agent,所有运行Consul agent节点的集合构成Consul Cluster。

Consul agent有两种运行模式:Server和Client。这里的Server和Client只是Consul集群层面的区分,与搭建在Cluster之上 的应用服务无关。

以Server模式运行的Consul agent节点用于维护Consul集群的状态,官方建议每个Consul Cluster至少有3个或以上的运行在Server mode的Agent,Client节点不限。

环境配置如下:

Centos 7.3

主机名称

IP

作用

是否允许远程访问

node1

192.168.252.121

consul server

node2

192.168.252.122

consul client

node3

192.168.252.123

consul client

关闭防火墙

systemctl stop firewalld.service

Consul 最新版的下载地址: 
https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip

下载,然后unzip 解压,得到唯一,一个可执行文件

cd /opt/
wget https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip
unzip consul_1.0.1_linux_amd64.zip
cp consul /usr/local/bin/

查看是否安装成功

[root@node1 opt]# consul

出现如下结果,表示安装成功

Usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    event          Fire a new event
    exec           Executes a command on Consul nodes
    force-leave    Forces a member of the cluster to enter the "left" state
    info           Provides debugging information for operators.
    join           Tell Consul agent to join cluster
    keygen         Generates a new encryption key
    keyring        Manages gossip layer encryption keys
    kv             Interact with the key-value store
    leave          Gracefully leaves the Consul cluster and shuts down
    lock           Execute a command holding a lock
    maint          Controls node or service maintenance mode
    members        Lists the members of a Consul cluster
    monitor        Stream logs from a Consul agent
    operator       Provides cluster-level tools for Consul operators
    reload         Triggers the agent to reload configuration files
    rtt            Estimates network round trip time between nodes
    snapshot       Saves, restores and inspects snapshots of Consul server state
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

检查版本

[root@node1 opt]# consul version

Consul v1.0.1
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

Consul常用命令

命令

解释

示例

agent

运行一个consul agent

consul agent -dev

join

将agent加入到consul集群

consul join IP

members

列出consul cluster集群中的members

consul members

leave

将节点移除所在集群

consul leave

consul agent 命令的常用选项

-data-dir

  • 作用:指定agent储存状态的数据目录
  • 这是所有agent都必须的
  • 对于server尤其重要,因为他们必须持久化集群的状态

-config-dir

  • 作用:指定service的配置文件和检查定义所在的位置
  • 通常会指定为”某一个路径/consul.d”(通常情况下,.d表示一系列配置文件存放的目录)

-config-file

  • 作用:指定一个要装载的配置文件
  • 该选项可以配置多次,进而配置多个配置文件(后边的会合并前边的,相同的值覆盖)

-dev

  • 作用:创建一个开发环境下的server节点
  • 该参数配置下,不会有任何持久化操作,即不会有任何数据写入到磁盘
  • 这种模式不能用于生产环境(因为第二条)

-bootstrap-expect

  • 作用:该命令通知consul server我们现在准备加入的server节点个数,该参数是为了延迟日志复制的启动直到我们指定数量的server节点成功的加入后启动。

-node

  • 作用:指定节点在集群中的名称
  • 该名称在集群中必须是唯一的(默认采用机器的host)
  • 推荐:直接采用机器的IP

-bind

  • 作用:指明节点的IP地址
  • 有时候不指定绑定IP,会报Failed to get advertise address: Multiple private IPs found. Please configure one. 的异常

-server

  • 作用:指定节点为server
  • 每个数据中心(DC)的server数推荐至少为1,至多为5
  • 所有的server都采用raft一致性算法来确保事务的一致性和线性化,事务修改了集群的状态,且集群的状态保存在每一台server上保证可用性
  • server也是与其他DC交互的门面(gateway)

-client

  • 作用:指定节点为client,指定客户端接口的绑定地址,包括:HTTP、DNS、RPC
  • 默认是127.0.0.1,只允许回环接口访问
  • 若不指定为-server,其实就是-client

-join

  • 作用:将节点加入到集群

-datacenter(老版本叫-dc,-dc已经失效)

  • 作用:指定机器加入到哪一个数据中心中

启动服务

我们尝试一下:

-dev表示开发模式运行,使用-client 参数可指定允许客户端使用什么ip去访问,例如-client 192.168.252.121 表示可以使用

http://192.168.252.121:8500/ui/ 去访问。

consul agent -dev -client 192.168.252.121

Spring Cloud(二) Consul 服务治理实现

Consul 的高可用

Consul Cluster集群架构图如下:

Spring Cloud(二) Consul 服务治理实现

这边准备了三台Centos 7.3的虚拟机,主机规划如下,供参考:

主机名称

IP

作用

是否允许远程访问

node1

192.168.252.121

consul server

node2

192.168.252.122

consul client

node3

192.168.252.123

consul client

搭建步骤

命令参数,参看上面详细介绍

在 node1 机器上启动 Consul

cd /opt/
mkdir data
consul agent -data-dir /opt/data -node=192.168.252.121 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.121 -server -bootstrap-expect 1 > /dev/null 2>&1 &

在 node2 机器上启动 Consul,并且将node2节点加入到node1节点上

cd /opt/
mkdir data
consul agent -data-dir /opt/data -node=192.168.252.122 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.122 -join=192.168.252.121 > /dev/null 2>&1 &

在 node3 机器上启动 Consul,并且将node3节点加入到node1节点上

cd /opt/
mkdir data
consul agent -data-dir /opt/data -node=192.168.252.123 -bind=0.0.0.0 -datacenter=dc1 -ui  -client=192.168.252.123 -join=192.168.252.121 > /dev/null 2>&1 &

在node1上查看当前集群节点:

consul members -rpc-addr=192.168.252.123:8400  

consul leave -rpc-addr=192.168.252.123:8400  

http://192.168.252.121:8500/ui/ 去访问。

Spring Cloud(二) Consul 服务治理实现

项目示例

新建项目:spring-cloud-consul-client

添加依赖

在项目 spring-cloud-consul-client pom.xml中引入需要的依赖内容:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

开启服务注册

客户端注册Consul时,它提供有关自身的元数据,如主机和端口,ID,名称和标签。默认情况下,将创建一个HTTP 检查,每隔10秒Consul命中/health端点。如果健康检查失败,则服务实例被标记为关键。

package io.ymq.example.consul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

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

配置文件

application.yml配置文件中增加如下信息:如果Consul客户端位于localhost:8500以外,则需要配置来定位客户端

spring:
  application:
    name: consul-client
  cloud:
    consul:
      host: 192.168.252.121
      port: 8500
      discovery:
        healthCheckPath: /
        healthCheckInterval: 5s

如果Consul客户端位于localhost:8500以外的位置,则需要配置来定位客户端。例:

host: 192.168.252.121
port: 8500

HTTP健康检查路径 INSTALL

“10s”和“1m”分别表示10秒和1分

discovery:
    healthCheckPath: ${management.context-path}/health
    healthCheckInterval: 15s

启动服务

spring-cloud-consul-client 项目根目录下,执行mvn clean package,把target 目录下 生成的 jar spring-cloud-consul-client-0.0.1-SNAPSHOT.jar 上传服务器,发布项目

打包命令

mvn clean package

发布命令

nohup java -jar spring-cloud-consul-client-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &

访问服务

http://192.168.252.121:8500/ui/#/dc1/nodes/192.168.252.121

Spring Cloud(二) Consul 服务治理实现

Spring Cloud(二) Consul 服务治理实现

Spring Cloud(二) Consul 服务治理实现

通过上图HTTP健康检查,可以看到服务检测正常

源码下载

GitHub:https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul

码云:https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
Consul微服务的配置中心体验篇
SpringCloudConsul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具有分布式、高可用、高扩展性ConsulConsul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul
Wesley13 Wesley13
2年前
Mysql半同步加orchestrator
Github基于Orchestrator,Consul和GLB实现高可用性目标。1.orchestrator用来运行故障监听和故障恢复。我们使用了如下图所示的一个跨数据中心的orchestrator/raft。2.Hashicorp公司的用于服务发现的Consul。使用Consul的KV存储器写入集群主节点的身份。对于每个集群,都有一套KV记录
Stella981 Stella981
2年前
Spring Cloud Spring Boot mybatis 企业分布式微服务云(二)服务注册与发现(Consul)
SpringCloudConsulSpringCloudConsul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。它包含了下面几个特性:服务发现健康检查Key/Value存储
Easter79 Easter79
2年前
SpringCloud consul 微服务(注册到主机名的问题)
目前项目在使用consul做服务注册与发现,做SpringSecurityOAuth2权限认证的authorization\_code模式的时候发现一个异常坑爹的问题这是开始的服务注册代码块bootstrap.yml:spring:cloud:consul:port:8500
Easter79 Easter79
2年前
SpringCloud注册中心高可用搭建
SpringCloud的注册中心可以由Eureka、Consul、Zookeeper、ETCD等来实现,这里推荐使用SpringCloudEureka来实现注册中心,它基于Netfilix的Eureka做了二次封装,完成分布式服务中服务治理的功能,微服务系统中的服务注册与发现都通过这个注册中心来进行管理。引入EurekaServer依赖
Stella981 Stella981
2年前
Consul 基本概念,同类比较和内部原理
这个文章我们主要来说一下Consul的基本概念,以及其实现的内部原理,和Eureka的比较。\1.什么是Consul?Consul是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全服务网格。Consul需要数据平面并支持代理和本机集成模型。Consul
Stella981 Stella981
2年前
Consul服务治理发现学习记录
Consul简介Consul是一个服务网格(微服务间的TCP/IP,负责服务之间的网络调用、限流、熔断和监控)解决方案,它是一个一个分布式的,高度可用的系统,而且开发使用都很简便。它提供了一个功能齐全的控制平面,主要特点是:服务发现、健康检查、键值存储、安全服务通信、多数据中心。除了Consul之外,还有Eureka、Zoo
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这