• consul-template服务发现与配置


    一、consul-template概述

    Consul-Template是一个守护进程,用于实时查询Consul集群信息

    Consul-Template可以更新文件系统上任意数量的指定模板,生成配置文件更新完成以后,可以选择运行shell命令执行更新操作,重新加载Nginx。

    Consul-Template可以查询Consul中的服务目录、Key、Key-values等。
    这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。

    二、示例

    1、安装

    解压后会有名称为 consul-template的一个可执行文件

     wget https://releases.hashicorp.com/consul-template/0.25.0/consul-template_0.25.0_linux_amd64.zip
    $ unzip consul-template_0.25.0_linux_amd64.zip 
    mv consul-template /usr/local/bin/
    consul-template -v
    consul-template v0.25.0 (99efa642)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、启动一个consul集群

    详情见前面的文章

    3、配置文件

    consul-template的配置文件config.json,放在当前目录:

    vim config.json

    consul = "127.0.0.1:8500"   //需要连接的consul
    
    template {
    
    source = "./nginx.ctmpl"     //文件模板
    destination = "./nginx.conf"   //需要生成的文件
    command = "pwd"  //执行命令
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    文件模板内容 ,这里从test01这个service服务中取出信息
    在这里插入图片描述

    vim nginx.ctmpl

    upstream http_backend {
      {{range service "test01"}}
       server {{.Address}}:{{.Port}};
       {{end}} 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    执行一次模板命令

    ./consul-template -config ./config.json -once
    
    • 1

    然后查看生成的文件
    cat nginx.conf

    upstream http_backend {
      
       server www.qq.com:80;
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    前端HTML事件
    vue前端导出Excel
    zeppelin安装python(使用pymysql包)
    c语言字符函数和字符串函数
    (Java)Mybatis学习笔记(四)
    数据结构---单链表
    手工架设安装教程:
    【ArcGIS Pro二次开发】:CC工具箱1.1.1更新_免费_安装即可用
    Leetcode 78. 子集
    U盘无法正常格式化?教你一个强力的办法
  • 原文地址:https://blog.csdn.net/qq_41475058/article/details/125621093