Spring Cloud 微服务之Consul(十)

22 篇文章 1 订阅
18 篇文章 0 订阅

一、什么是Spring Cloud Consul?

  • Spring Cloud Consul 为 SpringBoot 应用提供了 Consul的支持,Consul既可以作为注册中心使用,也可以作为配置中心使用。
  • Spring Cloud Consul 是HashiCorp公司推出的开源软件,提供了微服务系统中的服务治理、配置中心、控制总线等功能。
  • Spring Cloud Consul 的这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。

二、支持哪些功能?

  • 支持服务治理

    Consul作为注册中心时,微服务中的应用可以向Consul注册自己,并且可以从Consul获取其他应用信息。

  • 支持客户端负责均衡

    包括Ribbon和Spring Cloud LoadBalancer。

  • 支持Zuul

    当Zuul作为网关时,可以从Consul中注册和发现应用。

  • 支持分布式配置管理

    Consul作为配置中心时,使用键值对来存储配置信息。

  • 支持控制总线

    可以在整个微服务系统中通过 Control Bus 分发事件消息。

三、常见注册中心对比

  • zookeeper

    基于Zab协议,Zookeeper可以用于构建具备数据强一致性的服务注册与发现中心,而与此相对的是牺牲了服务的可用性和增加了注册服务所需要的时间。

  • eureka

    Eureka中没有使用任何的数据强一致性算法保证不同集群间的Server数据一致,仅仅通过数据拷贝的方式实现注册中心数据的最终一致性,虽然放弃了数据的强一致性但是换来了Server的高可用性,降低了注册的代价,提高了集群运行的健壮性。

  • consul

    它是基于Raft算法提供了数据强一致性的注册中心服务,但是由于Leader节点会承担所有的处理工作,所以加大了注册和发现的代价,降低了服务的可用性。通过Gossip协议,Consul可以很好地监控Consul集群的运行状态,同时可以方便通知各类事件到所有的服务节点。

    在这里插入图片描述

四、搭建注册中心

  • 服务端项目

    • 下载

      https://www.consul.io/downloads

      在这里插入图片描述

    • 运行

      # 查看版本
      consul --version
      
      # 启动服务
      consul agent -dev 
      
    • 访问

      http://localhost:8500
      

      在这里插入图片描述

  • 客户端项目

    • 添加依赖

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-consul-discovery</artifactId>
      </dependency>
      <!--consul监控的必须依赖-->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      
    • application.yml

      server:
        port: 8081
        
      spring:
        application:
          name: consul-service-8081
        cloud:
          consul: 
            host: localhost
            port: 8500
            discovery:
              service-name: ${spring.application.name}
      
    • 测试

      http://localhost:8500
      

      在这里插入图片描述

五、搭建配置中心

  • 添加依赖

    <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>
    
  • 添加配置

    • application.yml

      spring:
        profiles:
          active: dev
      
    • bootstrap.yml

      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: ':' 
              # 配置key的名字,由于Consul是K/V存储,配置存储在对应K的V中
              data-key: data 
      
  • 添加Controller

    @RestController
    // 动态刷新(Consul使用其自带的Control Bus 实现了一种事件传递机制,从而实现了动态刷新功能)
    @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"
    

    在这里插入图片描述

  • 访问

    http://localhost:9101/configInfo
    

    会打印出我们配置的value信息: config info for dev


【源码地址】:GitHub

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小吉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值