07.Spring Cloud Consul:服务治理与配置中心

Spring Cloud Consul:服务治理与配置中心

Consul 简介

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

  1. 支持服务治理:Consul作为注册中心时,微服务中的应用可以向Consul注册自己,并且可以从Consul获取其他应用信息;
  2. 支持客户端负责均衡:包括Ribbon和Spring Cloud LoadBalancer;
  3. 支持Zuul:当Zuul作为网关时,可以从Consul中注册和发现应用;
  4. 支持分布式配置管理:Consul作为配置中心时,使用键值对来存储配置信息;
  5. 支持控制总线:可以在整个微服务系统中通过 Control
  6. Bus 分发事件消息。

使用起来也较 为简单。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

官网下载Consul,地址:https://www.consul.io/downloads

在这里插入图片描述

下载完成后只有一个exe文件,双击运行;

在命令行中输入以下命令可以查看版本号:

consul --version

查到的版本信息如下:

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

使用开发模式启动:

consul agent -dev 

访问Consul的首页

http://localhost:8500

在这里插入图片描述

改造user-service和ribbon-service

创建consul-user-service模块和consul-ribbon-service模块

修改相关依赖

把原来的Eureka注册发现的依赖改为Consul的,并添加SpringBoot Actuator的依赖

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

修改配置文件application.yml

将Eureka的注册发现配置改为Consul的:

server:
  port: 7206
spring:
  application:
    name: consul-user-service
  cloud:
    consul: #Consul服务注册发现配置
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

运行两个consul-user-service(SpringCloud中一套代码启动两个服务(修改端口号即可))和一个consul-ribbon-service,在Consul页面上可以看到如下信息:
在这里插入图片描述

负载均衡演示

多次调用接口:http://localhost:7308/user/1 ,可以发现两个consul-user-service的控制台交替打印如下信息
在这里插入图片描述
在这里插入图片描述

使用Consul作为配置中心

创建consul-config-client

在pom文件中添加依赖

   	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consul-config-client</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

添加application.yml

启用的是dev环境的配置:

spring:
  profiles:
    active: dev

添加bootstrap.yml

里面主要是对Consul的配置功能进行配置

server:
  port: 9101
spring:
  application:
    name: consul-config-client
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: consul-config-client
      config:
        enabled: true #是否启用配置中心功能
        format: yaml #设置配置值的格式
        prefix: config #设置配置所在目录
        profile-separator: ':' #设置配置的分隔符
        data-key: data #配置key的名字,由于ConsulK/V存储,配置存储在对应KV

创建ConfigClientController

从Consul配置中心中获取配置信息

/**
  * @author dnydys
  * @description
  * @updateTime 2022/1/23
*/
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

在Consul中添加配置

添加配置存储的key:

config/consul-config-client:dev/data

添加配置存储的value:

config:
  info: "config info for dev"

在这里插入图片描述

启动consul-config-client

调用接口查看配置信息:http://localhost:9101/configInfo
结果为:

config info for dev

Consul的动态刷新配置

我们只要修改下Consul中的配置信息,再次调用查看配置的接口,就会发现配置已经刷新。回想下在使用Spring Cloud Config的时候,我们需要调用接口,通过Spring Cloud Bus才能刷新配置。Consul使用其自带的Control Bus 实现了一种事件传递机制,从而实现了动态刷新功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值