Consul注册中心(windows)安装及启动

一、本地下载与安装

https://www.consul.io/downloads.html   下载地址

1、本地下载下来后解压压缩包

2、找到consul.exe目录,右键找到“在此处打开命令窗口个”,输入命令:consul agent -dev -client=0.0.0.0 代表的是开发者模式启动,-client=0.0.0.0 代表所有ip都可以访问到当前服务。

正常启动的节目如下图:

3、访问后台管理界面:http://ip:8500, 界面如下所示

4,项目中关于Consul的相关配置:

spring:
  application:
    # 应用名称
    name:channel-user
  # 引用环境配置,多个间用逗号分隔
  profiles:
    active: dev
  cloud:
    consul:
      host: 192.168.28.254
      port: 8500
      # 服务注册&发现
      discovery:
        # 服务发现是否开启(默认true)
        enabled: true
        # 服务注册是否开启(默认true)
        register: true
        # 实例ID唯 一
        instance-id: ${spring.application.name}:${server.port:8080}:${random.value}
        # 指定服务的名称
        service-name: ${spring.application.name}
        #  在注册时使用IP
        prefer-ip-address: true
        # HTTP健康检查(默认10秒)
        health-check-interval: 5s
        # 超过N秒健康检查不过,从consul中删除
        health-check-critical-timeout: 5s
        # 只查询有效的记录
        query-passing: true
      # 分布式配置中心
      config:
        # 指定consul配置的文件夹前缀
        prefix: config
        # 指定consul配置的配置文件名
        data-key: data
        # 指定consul配置的配置文件格式
        format: YAML
        # 配置文件父路径,所有项目共用的配置 ${..config.prefix}/${..config.default-context}/${..config.data-key}
        default-context: blog
        # 快速失败,false:将导致配置模块记录一个警告而不是抛出异常;这将允许应用程序继续正常启动
        fail-fast: true
        #healthCheckPath: /health

Consul还可以作为项目的分布式配置中心,在上面的bootstrap.yml中我们配置了以下四个参数:
应用名称(spring.application.name):channel-user,
环境配置(spring.application.profiles.active):dev,
配置的文件夹前缀(spring.cloud.consul.config.prefix):config,
配置文件名(spring.cloud.consul.config.data-key):data
此时我们对应的Consul上的配置文件路径则为:/config/channel-user,dev/data。点击Cousul的Key/Value菜单,添加此路径配置,SpringBoot的application.xml中的配置可以放到此路径中,此时我们用Consul作为分布式配置中心也完成了。

猜你喜欢

转载自blog.csdn.net/u014635374/article/details/108451639