SpringCloud实战十七:Spring Cloud Consul 服务发现与配置中心

Consul 用于实现分布式系统的服务发现与配置。与其他服务注册与发现相比,Consul更“一站式”,内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。使用起来也较为简单。Consul使用Go语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合。
Consul 下载地址:https://www.consul.io/downloads.html
我这边使用了windows 安装包 且用 dev 模式启动 consul,方便本地调式,启动命令:consul agent -dev
正式环境请使用 server 集群模式,可参照 https://www.cnblogs.com/yjmyzz/p/replace-eureka-with-consul.html

  • 启动后访问默认端口:localhost:8500,可看到 consul 本身和我新建的 consul-service 示例服务已经注册进去了
    在这里插入图片描述
代码实践

实验要达到的效果:A.实现服务注册与发现(从上图可以看到我的服务已经注册到consul上了),B.动态更新配置
下面主要是 consul 作为配置中心,进行动态配置的代码

  • 1.新建项目,引入 spring cloud consul 依赖

    org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.cloud spring-cloud-starter-consul-config org.springframework.boot spring-boot-starter-actuator
  • 2.添加 bootstrap.yml 与 application.yml 配置文件,动态配置相关的配置放到 bootstrap.yml中
    bootstrap.yml

    bootstrap.yml

    consul 使用动态配置,必须在bootstrap.yml中配置好动态配置项目的配置

    spring:
    cloud:
    consul:
    host: localhost
    port: 8500
    #enabled将此值设置为“false”禁用Consul配置
    config:
    enabled: true #默认是true –
    format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
    data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
    # watch选项为配置监视功能,主要监视配置的改变
    watch:
    enabled: true
    delay: 10000
    wait-time: 30

application.yml

spring:
  application:
    name: consul-service
  profiles:
    active: dev
  cloud:
    consul:
      # 服务发现配置
      discovery:
        # 启用服务发现
        enabled: true
        # 启用服务注册
        register: true
        # 服务停止时取消注册
        deregister: true
        # 表示注册时使用IP而不是hostname
        prefer-ip-address: true
        # 执行监控检查的频率
        health-check-interval: 30s
        # 设置健康检查失败多长时间后,取消注册
        health-check-critical-timeout: 30s
        # 健康检查的路径
        health-check-path: /actuator/info
        # 服务注册标识,格式为:应用名称+服务器IP+端口
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

server:
  port: 8899
  • 3.新建 StudentConfig.java 类,里面的属性值配置在 consul 配置中心中

    @ConfigurationProperties(prefix = “student”) //前缀:对应consul 配置中心的 student 前缀
    public class StudentConfig {
    private String name;
    private int age;
    private String sex;

    //getter and setter
    

  • 4.在启动主类上使用注解

    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableScheduling //启用后,会定时拉取配置
    @EnableConfigurationProperties({StudentConfig.class})
    public class ConsulServiceApplication {

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

    }

  • 5.新建 IndexController 控制器,在 consul 中动态变更配置后的访问数据接口查看配置是否更新

    @RefreshScope
    @RestController
    public class IndexController {

    @Value("${description:zy}")
    private String description;
    
    @Autowired
    private StudentConfig studentConfig;
    
    @RequestMapping("/description")
    public String testDescription() {
        System.out.println("description is : " + description);
        return description;
    }
    
    @RequestMapping("/config")
    public String testConfig() {
        System.out.println(studentConfig.toString());
        return studentConfig.toString();
    }
    

    }

  • 6.最重要一步,在 consul 上创建配置文件与设置内容,点击右侧 create 输入 config/consul-service,dev/data,配置一下内容,点击 save
    a、默认情况下,consul配置默认存储在/config文件夹中
    b、consul-service为spring.application.name值,dev为spring.profiles.active值,data为data-key值
    c、value部分是yml格式的配置,冒号后面有一个空格。
    d、consul中的配置和文件命名是有规范的,和bootstrap.yml中的设置一一对应。
    在这里插入图片描述

  • 7.启动项目
    a.访问 http://localhost:8899/description
    在这里插入图片描述
    b.访问 http://localhost:8899/config
    在这里插入图片描述
    可以看到配置信息已经拉取下来了,再在 consul 上更新配置,然后再次刷新页面,看配置是否动态更新了
    修改 student的信息
    c.再次访问 http://localhost:8899/config
    在这里插入图片描述
    可以看到动态更新配置

代码已上传至码云,源码,项目使用的版本信息如下:

  • SpringBoot 2.0.6.RELEASE
  • SpringCloud Finchley.SR2

参考了一些网友的文章
将consul注册为window服务 https://blog.csdn.net/qq_38565742/article/details/82015744
修改cosul本地端口 https://www.jianshu.com/p/66977b2cee69
使用consul搭建集群环境 https://www.cnblogs.com/yjmyzz/p/replace-eureka-with-consul.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值