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

traefik下载地址:https://github.com/traefik/traefik/releases

启动traefik的命令

traefik.exe --api.insecure=true  --configFile=traefik-sample.yml

配置文件 traefik-sample.yml

################################################################
#
# Configuration sample for Traefik v2.
#
# For Traefik v1: https://github.com/traefik/traefik/blob/v1.7/traefik.sample.toml
#
################################################################################################################################
# Global configuration
################################################################
global:checkNewVersion: truesendAnonymousUsage: true################################################################
# EntryPoints configuration
################################################################# EntryPoints definition
#
# Optional
#
entryPoints:web:address: :80websecure:address: :443################################################################
# Traefik logs configuration
################################################################# Traefik logs
# Enabled by default and log to stdout
#
# Optional
#
log:# Log level## Optional# Default: "ERROR"#level: DEBUG# Sets the filepath for the traefik log. If not specified, stdout will be used.# Intermediate directories are created if necessary.## Optional# Default: os.Stdout#
#  filePath: log/traefik.log# Format is either "json" or "common".## Optional# Default: "common"#
#  format: json################################################################
# Access logs configuration
################################################################# Enable access logs
# By default it will write to stdout and produce logs in the textual
# Common Log Format (CLF), extended with additional fields.
#
# Optional
#
#accessLog:# Sets the file path for the access log. If not specified, stdout will be used.# Intermediate directories are created if necessary.## Optional# Default: os.Stdout#
#  filePath: /path/to/log/log.txt# Format is either "json" or "common".## Optional# Default: "common"#
#  format: json################################################################
# API and dashboard configuration
################################################################# Enable API and dashboard
#
# Optional
#
api:# Enable the API in insecure mode## Optional# Default: false#insecure: true# Enabled Dashboard## Optional# Default: true#
#  dashboard: false################################################################
# Ping configuration
################################################################# Enable ping
#ping:# Name of the related entry point## Optional# Default: "traefik"#
#  entryPoint: traefik################################################################
# Docker configuration backend
################################################################providers:consulCatalog:refreshInterval: 30sdefaultRule: "PathPrefix(`/{{ .Name }}`)"endpoint:address: 127.0.0.1:8500scheme: http# Enable Docker configuration backend
#  docker:# Docker server endpoint. Can be a tcp or a unix socket endpoint.## Required# Default: "unix:///var/run/docker.sock"#
#    endpoint: tcp://10.10.10.10:2375# Default host rule.## Optional# Default: "Host(`{{ normalize .Name }}`)"#
#    defaultRule: Host(`{{ normalize .Name }}.docker.localhost`)# Expose containers by default in traefik## Optional# Default: true#
#    exposedByDefault: false

启动consul服务:

consul.exe agent -dev -ui 

启动go 服务,并注册到consul:

package mainimport ("fmt""net/http"_ "net/http/pprof""time"consulapi "github.com/hashicorp/consul/api"
)var port  = 12000
var http_pre = "/consulServerNode"// consul 服务端会自己发送请求,来进行健康检查
func consulCheck(w http.ResponseWriter, r *http.Request) {s := "consulCheck" + fmt.Sprint(time.Now()) + " remote:" + r.RemoteAddr + " " + r.URL.String()_, _ = fmt.Fprintln(w, s)
}func HelloHandle(w http.ResponseWriter, r *http.Request)  {_, _ = fmt.Fprint(w, " hello! port = 12000")
}func registerServer() {config := consulapi.DefaultConfig()client, err := consulapi.NewClient(config)if err != nil {panic(err)}registration := &consulapi.AgentServiceRegistration{ID:                "consulServerNode_1",Name:              "consulServerNode",Tags:              []string{"traefik-test-tag"},Port:              port,Address:           "127.0.0.1",}registration.Check = &consulapi.AgentServiceCheck{ // 健康检查HTTP:                           fmt.Sprintf("http://%s:%d%s", registration.Address, port, http_pre +"/check"),Timeout:                        "3s",Interval:                       "5s",  // 健康检查间隔DeregisterCriticalServiceAfter: "30s", //check失败后30秒删除本服务,注销时间,相当于过期时间}if err := client.Agent().ServiceRegister(registration);err != nil {panic(err)}http.HandleFunc(http_pre +"/check", consulCheck)http.HandleFunc(http_pre +"/hello", HelloHandle)if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err!= nil{panic(err)}}func main() {registerServer()
}

启动traefik服务,启动命令见文章开始处

启动时会看到这样的日志(为看的比较清晰,将一些\" 替换成了“)

level=debug msg="Configuration received from provider consulcatalog: {"http":{"routers":{"consul":{"service":"consul","rule":"PathPrefix(`/consul`)"},"consulServerNode":{"service":"consulServerNode","rule":"PathPrefix(`/consulServerNode`)"}},"services":{"consul":{"loadBalancer":{"servers":[{"url":"http://127.0.0.1:8300"}],"passHostHeader":true}},"consulServerNode":{"loadBalancer":{"servers":[{"url":"http://127.0.0.1:12000"}],"passHostHeader":true}}}},"tcp":{},"udp":{}}" providerName=consulcatalog

浏览器访问 http://127.0.0.1/consulServerNode/hello

参考链接:

https://doc.traefik.io/traefik/providers/consul-catalog/

traefik使用 -- consul catalog动态配置相关推荐

  1. traefik使用 -- consul kv动态配置

    consul下载地址: https://www.consul.io/downloads traefik下载地址:https://github.com/traefik/traefik/releases ...

  2. openresty+consul动态配置更新(服务变更发现)

    最近在做数据收集平台,用openresty往kafka里push数据,不管是kafka broker也好,还是一个配置也好,希望做到动态更新,不需要reload openresty.尤其是针对接口调用 ...

  3. nginx动态配置及服务发现那些事

    标题,  <闲聊nginx动态配置及服务发现的那些事> - 这次的准备闲聊关于nginx服务发现的话题,  按照我以往写文章的性子,估计会迁移一些主题.  毕竟单纯聊nginx和动态服务发 ...

  4. 微服务-Nacos动态配置中心

    前言 回想之前的项目,尤其是项目分多模块的时候,一般就会有一个公共包大多是相关工具类,因为比较独立使得其他模块调用方便,现在出来个需求为工具类参数进行动态控制,比如Redis工具类动态设定过期时间,除 ...

  5. ABP动态配置数据库连接字符串 Oracle11g数据库

    ABP动态配置数据库连接 Oracle11g数据库 ABP的仓储关于数据库连接字符串的配置是从web.config(app.config)的connectionStrings读取的. 我想实现的功能是 ...

  6. 解锁HLS开发|Demo(1):ARM动态配置FPGA的自定义IP

    微信公众号:佛系入门ZYNQ图像处理 "狂浪是一种态度" "八卦HLS" 本次Demo目的及功能 本次Demo原理分析 HLS开发->源文件设计 HLS开 ...

  7. ConfigBus:Twitter的动态配置实践

    动态配置能够在不重新启动应用程序的情况下更改正在运行的系统的行为和功能.理想的动态配置系统使服务开发人员和管理员能够方便地查看和更新配置,并高效可靠地向应用程序提供配置更新.它使组织能够快速.大胆地迭 ...

  8. 取代ZooKeeper,Twitter 的动态配置实践

    作者 | Twitter Engineering Blog 译者 | 谢丽 ConfigBus 是 Twitter 的动态配置系统,包括存储配置的数据库.将配置分发到 Twitter 数据中心中的机器 ...

  9. Consul Catalog

    2019独角兽企业重金招聘Python工程师标准>>> 什么是Consul? Consul是一个用来实现分布式系统的服务发现与配置的开源工具.他主要由多个组成部分: 服务发现:客户端 ...

最新文章

  1. Ubuntu 13.10 安装Terminalx 后更改默认终端设置
  2. Adam又要“退休”了?耶鲁大学团队提出AdaBelief,却引来网友质疑
  3. Angular CLI的安装和使用
  4. 【Python】Python迭代求解开平方算法
  5. 使用回调技术实现局部刷新
  6. linux那些内存可以释放,Linux释放内存
  7. 《长津湖之水门桥》定档大年初一 想看热度跻进春节档TOP3
  8. python3.8.4rc1_Python3.8.4rc1 官方正式版
  9. 我之所以抛弃Java而选择Kotlin的10个理由
  10. ActiveMQ常见配置
  11. python能做什么-大家都在学的 Python,可以用来干什么?
  12. 2021-07-19交互设计精髓读后感
  13. iexplore.exe命令行参数解释
  14. 从前慢-深入理解JVM-篇章1
  15. 微信小程序 用户中心界面(二)
  16. 计算机在线考试word,大学计算机考试word试题及答案.docx
  17. 我喜欢的photo,images....
  18. jump game ii
  19. 苹果 M1芯片运行Android模拟器
  20. 三博脑科医院:癫痫的治疗像是一场“对抗赛”

热门文章

  1. 电脑怎样进入磁盘管理
  2. 使用智邦国际ERP系统添加生产预测单方法
  3. Spring框架(JavaEE开发进阶Ⅲ)—基础和IoC
  4. MYSQL外键约束和表关联
  5. php面包屑源码,ZBlogPHP面包屑导航的完整代码分享
  6. 数字图像处理基础内容
  7. mac air上archlinux的安装及优化
  8. LayUI和Java后端:处理分期付款,自动计算期次、缴费日期、金额等
  9. 让我用Python写一个秒杀某宝商品抢购再迎双十二
  10. java jquery 文件下载_jQuery教程分享通过ajax下载文件