• 什么是 Consul

    ​ Consul是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全服务网格。Consul需要数据平面并支持代理和本机集成模型。Consul附带一个简单的内置代理,因此一切都可以开箱即用,但也支持第三方代理集成,如Envoy

  • Consul的主要特点

  • 服务发现:Consul 的客户可以注册服务,例如 apimysql,并且其他客户可以使用Consul来发现给定服务的提供者。使用 DNS 或 HTTP,应用程序可以轻松找到它们所依赖的服务。
  • 运行状况检查:Consul 客户端可以提供任意数量的运行状况检查,这些检查与给定服务(“是 Web 服务器返回 200 OK”)或本地节点(“内存利用率低于 90%”)相关联。运营商可以使用此信息来监控群集运行状况,服务发现组件使用该信息将流量路由远离不健康的主机。
  • KV 商店:应用程序可以将 Consul 的分层键/值存储用于任何目的,包括动态配置,功能标记,协调,领导者选举等。简单的 HTTP API 使其易于使用。
  • 安全服务通信:Consul可以为服务生成和分发 TLS 证书,以建立相互的 TLS 连接。 意图 可用于定义允许哪些服务进行通信。可以使用可以实时更改的意图轻松管理服务分段,而不是使用复杂的网络拓扑和静态防火墙规则。
  • 多数据中心:Consul 支持多个数据中心。这意味着 Consul 的用户不必担心构建额外的抽象层以扩展到多个区域。

Consul 是一个分布式,高度可用的系统。本节将介绍基础知识,故意省略一些不必要的细节,以便您快速了解Consul的工作原理。有关更多详细信息,请参阅 深入的体系结构概述

1、安装

  • 通过官网地址下载对应的安装包

  • 以 Linux 的为例,将安装包解压在任意喜欢的目录之中如: /usr/local

    在 /usr/local 建立 consul 目录

    将下载好的压缩包移动值该目录解压: unzip consul_1.4.4_linux_amd64.zip

    为了方便起见,将得到的 consul 命令文件拷贝一份到 /usr/local/bin 目录之中即可全局使用

    cp ./consul /usr/local/bin/

    如若无误在任意位置输入: consul version 即可看到对应的 consul 的版本信息

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

2、启动

  • 基本命令(前台启动)
consul agent -server -bind=192.168.xxx.134 -client=0.0.0.0 -bootstrap-expect=1 -data-dir=/usr/data/consul_data -node=server1

**-server ** 表示以服务端身份启动

**-bind ** 强制绑定到指定 IP

-client 指定客户端访问的 IP (consul 有丰富的 api 接口,这里的客户端指浏览器或调用方),0.0.0.0 表示不限制客户端 IP

-bootstrap-expect=3 表示 server 集群最低节点为 3,低于这个值将工作不正常(注:类似zookeeper一样,通常集群数为奇数,方便选举,consul采用的是raft算法)

-data-dir 表示指定数据的存放目录(该目录必须已存在)

-node 表示节点在 web ui 中显示的名称

启动成功后,不要关闭终端窗口,在浏览器之中访问:http://192.168.xxx.134:8500/ ,如若无误将会得到:

Consul Agent
  • 后台启动
nohup consul agent -server -bind=192.168.xxx.134 -client=0.0.0.0 -bootstrap-expect=1 -data-dir=/usr/data/consul_data -node=server1  > /dev/null 2>&1 &

3、启动 client 端

  • 与 Server 端类似,去除掉 -server 即可
nohup consul agent -bind=192.168.xxx.134 -http-port=8500 -dns-port=8600 -client=0.0.0.0 -data-dir=/usr/data/consul_data/ -node=client1  > /dev/null 2>&1 &
  • 指定 http 与 dns 端口号
nohup consul agent -bind=192.168.xxx.134 -http-port=7500 -dns-port=7600 -client=0.0.0.0 -data-dir=/usr/data/consul_data/ -node=client1  > /dev/null 2>&1 &

4、带 UI 启动

nohup consul agent -server -bind=192.168.xxx.134 -client=0.0.0.0 -bootstrap-expect=1 -data-dir=/usr/data/consul_data -node=server1 -ui > /dev/null 2>&1 &

5、搭建集群

搭建多个节点,在多个节点中启动 Consul ,通过 join 命令关联示例

例如: 将当前节点添加到 192.168.xxx.131 节点之中

consul join 192.168.xxx.131

成功后,会输出:Successfully joined cluster by contacting 1 node

查看集群:consul members

Node     Address               Status  Type    Build  Protocol  DC   Segment
server1 192.168.xxx.134:8301 alive server 1.4.4 2 dc1 <all>
server2 192.168.xxx.131:8301 alive server 1.4.4 2 dc1 <all>

6、服务注册/发现/注销

目前在 services 里,除了 consul 外,还没有其他服务,我们可以通过 service api 来进行注册,可以通过 postman、Insomnia 等 rest api工具或者 curl 命令,向 服务注册地址 进行注册,以 PUT 方式提交、Content-Type 为 application/json 格式,参数设置为以下 JSON 内容

{
"ID": "elasticsearchID",
"Service": "elasticsearch",
"Name":"my-service-name",
"Tags": [
"mytags=1"
],
"Meta": {
"elasticsearch_version": "4.0"
},
"Port": 8080,
"Address": "192.168.xxx.1",
"EnableTagOverride": false,
"Weights": {
"Passing": 10,
"Warning": 1
}
}

往 192.168.xxx.131 这台机器的注册一个名为 my-service-name,ID 为 elasticsearchID的服务

具体 API 解释与更多使用参考 官网

7、开发者模式

​ 前面的几步操作在开发的过程中操作起来的步骤相对繁琐,因此 consul 也为开发人员推出了开发者模式(即 dev) 使用方式及其简单,输入如下命令即可:

consul agent -dev

开发模式,自带了 web ui,当然此种方式也默认 IP 为: 127.0.0.1 ,所以直接输入:http://127.0.0.1:8500 访问 UI 页面

8、在 Spring Cloud 中使用 Consul

​ 创建一个 Spring Cloud 工程「microcloud」,在该工程中使用 Consul

  • 创建「microcloud」,配置父 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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.microcloud</groupId>
    <artifactId>microcloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
    <module>microcloud-consul-8501</module>
    </modules> <name>microcloud</name>

    <!-- FIXME change it to the project's website -->

    <url>http://www.example.com</url> <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.compiler.target>1.8</maven.compiler.target>

    <jdk.version>1.8</jdk.version>

    </properties> <dependencyManagement>

    <dependencies>

    <!--cloud父pom引用-->

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-dependencies</artifactId>

    <version>Greenwich.RELEASE</version>

    <type>pom</type>

    <scope>import</scope>

    </dependency>

    <!--支持cloud-->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-dependencies</artifactId>

    <version>2.1.4.RELEASE</version>

    <type>pom</type>

    <scope>import</scope>

    </dependency>

    </dependencies>

    </dependencyManagement> <build>

    <finalName>microcloud</finalName>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <configuration>

    <source>${jdk.version}</source> <!--源码的JDK版本-->

    <target>${jdk.version}</target>

    <encoding>${project.build.sourceEncoding}</encoding>

    </configuration>

    </plugin>

    </plugins>
    <span class="token comment">&lt;!--资源文件打包管理--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resources</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resource</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>directory</span><span class="token punctuation">&gt;</span></span>src/main/resources<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>directory</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>includes</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.properties<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.yml<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.xml<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.tld<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>includes</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>filtering</span><span class="token punctuation">&gt;</span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>filtering</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resource</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resource</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>directory</span><span class="token punctuation">&gt;</span></span>src/main/java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>directory</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>includes</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.properties<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.yml<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.xml<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span><span class="token punctuation">&gt;</span></span>**/*.tld<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>include</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>includes</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>filtering</span><span class="token punctuation">&gt;</span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>filtering</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resource</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resources</span><span class="token punctuation">&gt;</span></span>

    </build>

    </project>

  • 创建「microcloud-consul-8501」模块,配置 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>microcloud</artifactId>
    <groupId>com.microcloud</groupId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>microcloud-consul-8501</artifactId>
    <name>microcloud-consul-8501</name>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusions</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusion</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-tomcat<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusion</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusions</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--consul 引用--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-consul-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>

    </project>

  • 【microcloud-consul-8501】在 /src/main/resources 下创建配置文件 application.yml 配置文件,增加 Consul 配置

    server:
    port: 8501
    spring:
    cloud:
    consul:
    host: 127.0.0.1
    port: 8500
    discovery:
    tags: version=1.0,author=code
    health-check-path: /info # 为健康检查的url,由consul定时调用一次判断服务是否可用
    health-check-interval: 5s # 检查间隔时间
    instance-id: ${spring.application.name}:prod # 注册的服务ID
    service-name: ${spring.application.name} # 注册的服务名称
    prefer-ip-address: true # 是否使用指定IP地址,默认为 false
    ip-address: 127.0.0.1 # 指定 IP 地址 application:

    name: microcloud-consul-8501

  • 【microcloud-consul-8501】配置检查健康服务类

    package com.microcloud.controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    /**
    * @Auther: xxx
    */
    @RestController
    public class ConsulController {
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/echo"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">echo</span><span class="token punctuation">(</span><span class="token class-name">String</span> message<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token string">"Echo:"</span> <span class="token operator">+</span> message<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token comment">/**
    * consul 检查服务方法
    * @return
    */</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/info"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token string">"info"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    }

  • 【microcloud-consul-8501】 启动类

    package com.microcloud;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication; /**
    • @Auther: xxx

      */

      @SpringBootApplication

      public class Application {

      public static void main(String[] args) {

      SpringApplication.run(Application.class, args);

      }

      }

  • 启动,测试

    启动后可得到类似如下内容,即表示注册成功,可通过 Consul UI 地址查看到对应在服务信息

    Registering service with consul: NewService{id='microcloud-consul-8501-prod', name='microcloud-consul-8501', tags=[version=1.0, author=code, secure=false], address='127.0.0.1', meta=null, port=8501, enableTagOverride=null, check=Check{script='null', interval='5s', ttl='null', http='http://127.0.0.1:8501/info', method='null', header={}, tcp='null', timeout='null', deregisterCriticalServiceAfter='null', tlsSkipVerify=null, status='null'}, checks=null}

    访问: http://localhost:8500/v1/agent/services 将获取如下内容

    {
    "microcloud-consul-8501-prod": {
    "ID": "microcloud-consul-8501-prod",
    "Service": "microcloud-consul-8501",
    "Tags": [
    "version=1.0",
    "author=code",
    "secure=false"
    ],
    "Meta": {},
    "Port": 8501,
    "Address": "127.0.0.1",
    "Weights": {
    "Passing": 1,
    "Warning": 1
    },
    "EnableTagOverride": false
    }
    }
  • 【microcloud-customer-80】通过 RestTemplate 调用在 Consul 中注册的服务,创建并配置 「microcloud-customer-80」 模块

    配置 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>microcloud</artifactId>
    <groupId>com.microcloud</groupId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>microcloud-customer-80</artifactId>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>name</span><span class="token punctuation">&gt;</span></span>microcloud-customer-80<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>name</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- FIXME change it to the project's website --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>url</span><span class="token punctuation">&gt;</span></span>http://www.example.com<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>url</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-jetty<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusions</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusion</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-tomcat<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusion</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusions</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--consul 引用--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-consul-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>

    </project>

    创建 Rest 配置模板 Bean

    package com.microcloud.config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    /**
    * Rest 模板配置
    * @Auther: xxx
    */
    @Configuration
    public class RestConfig {
    @Bean
    public RestTemplate getRestTemplate() {
    return new RestTemplate();
    }
    }

    Rest控制服务

    package com.microcloud.controller;
    
    import org.springframework.cloud.client.ServiceInstance;

    import org.springframework.cloud.client.discovery.DiscoveryClient;

    import org.springframework.util.CollectionUtils;

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

    import org.springframework.web.bind.annotation.RequestMapping;

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

    import org.springframework.web.client.RestTemplate;

    import javax.annotation.Resource;

    import java.net.URI;

    import java.util.List;

    /**
    • @Auther: xxx

      */

      @RestController

      @RequestMapping(value = "/customer")

      public class CustomerController { @Resource

      private RestTemplate restTemplate;

      @Resource

      private DiscoveryClient discoveryClient; @GetMapping(value = "/info")

      public String getInfo(String echo) {

      String serviceName = "microcloud-consul-8501";

      List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);

      if (!CollectionUtils.isEmpty(instances)) {

      URI uri = instances.get(0).getUri();

      String result = restTemplate.getForObject(uri + "/echo?message=" + echo, String.class);

      return result;

      }

      return "";

      }

      }

    YAMl配置文件

    server:
    port: 80
    spring:
    application:
    name: customer
    cloud:
    consul:
    host: 127.0.0.1
    port: 8500

    服务启动

    package com.microcloud;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication; /**
    • @Auther: xxx

      */

      @SpringBootApplication

      public class Application {

      public static void main(String[] args) {

      SpringApplication.run(Application.class, args);

      }

      }

  • 在浏览器中输入 http://localhost/customer/info?echo=Hello Consul 观察返回结果

    如若无误会得到

    Echo:Hello Consul

  • 获取完整源码

    原文地址:https://www.jianshu.com/p/266f4c8c265c

    Consul 的安装与基本使用的更多相关文章

    1. consul的安装配置 一centos7环境

      centos7上consul的安装--新手笔记 环境 我用的是centos7, 用的是vmware 一 安装系统后首先要设置ip ifconfig eth0 →查看IP 不过输出的信息多一些 ifco ...

    2. Windows系统下consul的安装、启动、配置

      阅读时长:3分钟 操作系统:Windows10 一.consul的安装 首先在consul.exe文件目录下的地址栏中输入cmd. 接着输入consul指令,敲击回车安装consul. 安装成功后会有 ...

    3. 第二章 consul的安装和启动

      安装环境: mac:64bit(查看mac位数:打开终端-->"uname -a") consul_0.6.4_darwin_amd64.zip和consul_0.6.4_w ...

    4. Consul Windows 安装

      下载文件https://www.consul.io/downloads.html, 解压完毕后只有一个consul文件 consul 启动一个 Agent consul agent -server - ...

    5. consul windows安装

      Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License 2.0 的协议进行开 ...

    6. 详解consul的安装和配置

      Consul 简化了分布式环境中的服务的注册和发现流程,通过 HTTP 或者 DNS 接口发现.支持外部 SaaS 提供者等. consul提供的一些关键特性: service discovery:c ...

    7. consul的安装与使用

      consul官网下载地址 https://www.consul.io/downloads.html 下载后解压 在bat文件中增加如下内容,就可以双击启动了 start consul agent -d ...

    8. Mac OS、Ubuntu 安装及使用 Consul

      Consul 概念(摘录): Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,比如 Airbnb 的 SmartStac ...

    9. centos7上consul的集群安装

      centos7上consul的安装 ###一 下载 下载文件 wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_a ...

    随机推荐

    1. 解决Git - git push origin master 报错

      关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 欢迎大家关注我的微信公众号:「醉翁猫咪」 原因:github仓库中没有README.md文件 解决如下: 重新输入git push ...

    2. c++ 获取字符串中最长的回文子串

      #include <vector> #include <iostream> #include <string> using namespace std; strin ...

    3. Alpha冲刺(4/6)

      队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 摸鱼 提交记录(全组共用) 接下来的计划 沟通前后端成员,监督.提醒他们尽快完成各自的进度 学习如何评估代码质量 准备Al ...

    4. 20189220 余超《Linux内核原理与分析》第八周作业

      Linux内核如何装载和启动一个可执行程序 本章知识点 ELF(Executable and Linking Format)是一种对象文件的格式,用于定义不同类型的对象文件(Object files) ...

    5. crontab定时任务接入

      # 查看 $ crontab -l # 创建 $ crontab -e # 每分钟输出一次当前时间 * * * * * echo `date` >> /demo.log # 查看定时 $ ...

    6. 帆软 联合 创始人 数据可视化 中国 发展 FineReport FineBI

      丧心病狂!帆软公司的成立竟源于一个被初恋抛弃的程序员 - 大数据-炼数成金-Dataguru专业数据分析社区http://dataguru.cn/article-7500-1.html 帆软联合创始人 ...

    7. [转]小D课堂 - 零基础入门SpringBoot2.X到实战_汇总

      原文地址:https://www.cnblogs.com/wangjunwei/p/11392825.html 第1节零基础快速入门SpringBoot2.0 小D课堂 - 零基础入门SpringBo ...

    8. redis 锁的案例

      1: redis 锁 作为一种术装饰器使用 基本逻辑: 1:声明一个redislock类  定义生成锁和释放锁两个方法 2:生成锁使用了一个默认值 setnx ; 如果当前时间大于 第一次锁的生成时间 ...

    9. WebGL学习笔记(七):输入和动画

      目前为止,我们绘制出来的3D物体都是静止的,接下来我们需要让桌面上的小盒子可以根据我们按键(上下键)前进后退: 输入方面,监听按键和鼠标消息直接在document上添加对应的监听就行了: 动画这块,我 ...

    10. LeetCode_443. String Compression

      443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...