1. Consul介绍

Consul是一套开源的分布式服务发现和配置管理系统,支持多数据中心分布式高可用。Consul是HashiCorp( Vagrant的创建者)开发的一个服务发现与配置项目,用Go语言开发,基于 Mozilla Public License 2.0 的协议开源。

Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性:

  • 服务发现

    Consul的客户端可用提供一个服务,比如 api 或者mysql ,另外一些客户端可用使用Consul去发现一个指定服务的提供者.通过DNS或者HTTP应用程序可用很容易的找到他所依赖的服务。
  • 健康检查

    Consul客户端可用提供任意数量的健康检查,指定一个服务(比如:webserver是否返回了200 OK 状态码)或者使用本地节点(比如:内存使用是否大于90%). 这个信息可由operator用来监视集群的健康.被服务发现组件用来避免将流量发送到不健康的主机。
  • Key/Value存储

    应用程序可用根据自己的需要使用Consul的层级的Key/Value存储.比如动态配置,功能标记,协调,领袖选举等等,简单的HTTP API让他更易于使用。
  • 多数据中心

    Consul支持开箱即用的多数据中心.这意味着用户不需要担心需要建立额外的抽象层让业务扩展到多个区域。

2. 安装

下载页面中找到和你系统匹配的包。解压Consul zip包,复制consul二进制文件到系统PATH中包含的路径下,以确保它可以被执行。在Unix系统中,~/bin和/usr/local/bin是通常的安装路径,选择哪个依赖于你安装Consul给单个用户使用还是所有用户都可以使用。对于Windows系统,你可以安装到任意目录,不过不要忘记将安装目录加入到%PATH%中去。

2.1 验证安装

安装Consul后,通过打开新的终端回话并且输入consul是否可用来验证安装是否工作。通过执行consul你应该可以看到下面类似的输出:

$ 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

3. 启动Consul

3.1 开发模式运行:

$ consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.0.2'
Node ID: '5231963e-3453-ac4c-cf31-d13736402df9'
Node name: 'MantouMBP.local'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

你可以看到,Consul代理已经启动并且输出了一些日志信息。从日志信息中,你可以看到我们代理运行在服务器模式并且声明集群的leadship。另外,本地的成员已经被标记为一个健康的集群成员。

OS X用户注意:Consul使用你的机器名作为默认的节点名称。如果你的机器名包涵了点,那么DNS查询该节点会不能工作,为了避免这个问题,使用-node显式设置你的节点名称。

3.2 查看集群成员

如果你在另一个终端中运行 consul members ,你能看到Consul集群所有的节点,但是现在你只能看到一个成员(你自己的机器)

$ consul members
Node Address Status Type Build Protocol DC Segment
MantouMBP.local 127.0.0.1:8301 alive server 1.0.2 2 dc1 <all>

该命令输出显示你自己的节点,运行的地址,它的健康状态,它在集群中的角色,以及一些版本信息。另外元数据可以通过 -detailed 选项来查看。

members 命令选项的输出是基于 gossip协议 的并且其内容是最终一致。也就是说,在任何时候,你在本地代理看到的内容也许与当前服务器中的状态并不是绝对一致的。如果需要强一致性的状态信息,使用HTTP API向Consul服务器发送请求:

$ curl localhost:8500/v1/catalog/nodes
[
{
"ID": "bce12243-d825-ea69-2066-c3e3daae13fb",
"Node": "MantouMBP.local",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 5,
"ModifyIndex": 6
}
]

另外对于HTTP API,DNS接口也常被用来查询节点信息。注意你必须确信你的DNS能够找到Consul代理的DNS服务器,Consul代理的DNS服务器默认运行在8600端口。

$ dig @127.0.0.1 -p 8600 MantouMBP.local.node.consul

; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 MantouMBP.local.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29410
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available ;; QUESTION SECTION:
;MantouMBP.local.node.consul. IN A ;; ANSWER SECTION:
MantouMBP.local.node.consul. 0 IN A 127.0.0.1 ;; Query time: 3 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Thu Dec 28 22:24:05 2017
;; MSG SIZE rcvd: 61

4. 使用SpringCloud Consul组件

下面我们创建提供服务的客户端,并向Consul服务注册中心注册自己。

首先,使用IDEA创建一个Spring Initializr创建一个基本的Spring Boot应用并选择Consul Dependencies:

pom.xml中,配置如下:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!--consul中健康检查需要用到actuator,不添加会check failing-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在应用主类中通过加上@EnableDiscoveryClient注解:

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

配置application.properties:

Spring Boot的配置除了可以使用传统的propertiese文件外,还支持现在被广泛推荐使用的YAML文件。YAML文件使用类似大纲的缩进形式进行表示,结构更加清晰易读。

spring.application.name=client-service
server.port=2001
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

启动工程后,访问:http://localhost:8500/ui/#/dc1/services,

可以看到下面的页面,已经成功注册了服务。

服务发现的接口DiscoveryClient是Spring Cloud对服务治理做的一层抽象,所以可以屏蔽Eureka和Consul服务治理的实现细节,我们的程序不需要做任何改变,只需要引入不同的服务治理依赖,并配置相关的配置属性就能轻松的将微服务纳入Spring Cloud的各个服务治理框架中。

Spring Cloud Consul入门的更多相关文章

  1. Spring Cloud Consul 入门指引

    1 概述 Spring Cloud Consul 项目为 Spring Boot 应用程序提供了与 Consul 的轻松集成. Consul 是一个工具,它提供组件来解决微服务架构中一些最常见的挑战: ...

  2. Spring Cloud Consul 实现服务注册和发现

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...

  3. Spring Cloud 快速入门

     Spring Cloud快速入门 代码地址: https://gitee.com/gloryxu/spring-cloud-test EureKa:服务注册中心 添加依赖 <dependenc ...

  4. Spring Cloud Gateway入门

    1.什么是Spring Cloud GatewaySpring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技 ...

  5. Spring Cloud Consul使用——服务注册与发现(注册中心)

    整理自该文章 一.Consul 服务端接下来我们开发 Consul 的服务端,创建一个 spring-cloud-consul-producer 项目 1.添加依赖包 <dependencies ...

  6. Spring Cloud Consul

    1.2.0.RELEASE 该项目通过自动配置并绑定到Spring环境和其他Spring编程模型成语,为Spring Boot应用程序提供Consul集成.通过几个简单的注释,您可以快速启用和配置应用 ...

  7. Spring Cloud Consul 之Greenwich版本全攻略

    什么是Consul Consul是HashiCorp公司推出的开源软件,使用GO语言编写,提供了分布式系统的服务注册和发现.配置等功能,这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全 ...

  8. 服务注册发现、配置中心集一体的 Spring Cloud Consul

    前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...

  9. Spring Cloud Consul Config 知识点

    Spring Cloud Consul Config 是 Config Server 和 Client的替代方案. 搭建一个配置中心,可以选择的方案: Spring Cloud Config 或者 S ...

随机推荐

  1. 【new File(String Path)加载资源问题】

    2017-12-17   15:07:38  [原创-wx] 一.我们在用IO流加载资源的时候,创建文件资源 1 File file = New File("String Path" ...

  2. hdu1570(排列和组合公式的应用)

    题意: 给出字符A.则求全排列 A(n,m)=n!/(n-m)! 给出字符C.则求全组合 C(n,m)=n!/(m!*(n-m)!) http://acm.hdu.edu.cn/showproblem ...

  3. 重要经验五:block作为属性的注意事项

    现代ios应用的开发.不使用GCD和block,效率会减少非常多,在今年3月份之前,我在block的学习和使用方面,精力和经验都明显不足,在SF有个牛逼同事.不仅自己积累了一套库,并且对这个库持续进行 ...

  4. Python 爬虫 爬校花网!!

    爬虫:是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本 1.福利来了  校花网 ,首先说为什么要爬这个网站呢,第一这个网站简单爬起来容易不会受到打击,第二呢 你懂得... 1.第一步,需要下载 ...

  5. Spring Boot-------热部署

    热部署 热部署重要的是:添加一个jar包 第一步:在我们项目中的pom.xml文件中添加spring-boot-devtools这个jar包,Maven会帮我们自动下载的 <dependency ...

  6. 自学Python1.2-环境的搭建:Pycharm及python安装详细教程

    Python几乎可以在任何平台下运行,如我们所熟悉的:Windows/Unix/Linux/Macintosh 一.windows下安装Python 1. 从python官方网站:http://www ...

  7. Nginx干货(一)隐藏Nginx标识与版本号

    注:默认读者自己会安装nginx,不会的就搜一下怎么安装吧.说明一点.在make的时候可以使用make -j num这里面的num是你的cpu核心数.这样会快一点~~~意为以num个进程同时编译 首先 ...

  8. python小整数与str数据池,编码关系

    1.小数据池:数字,字符串 数字的范围-5 ---256 字符串:1,不能有特殊字符 2,s*20 还是一个地址,s*21以后都是两个地址 2.编码关系:ascii A : 00000010 8位 一 ...

  9. 指针变量的*p,p以及&p的区别

    23/7/2017  决定夯实基础,回顾指针的知识点,该博文转载于CSDN博主百家晓东,小部分修改,外加编译图引证 正文如下: 以下面的情况说明: int a = 5; int* p = &a ...

  10. Linux 运行级别

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/47 运行级别 不同运行级别的描述 运行级别0:系统停机状态,系统 ...