traefik使用 -- consul kv动态配置

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: true
  sendAnonymousUsage: true

################################################################
# EntryPoints configuration
################################################################

# EntryPoints definition
#
# Optional
#
entryPoints:
  web:
    address: :80

  websecure:
    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:

  consul:
    rootKey: "traefik"
    endpoints:
      - "127.0.0.1:8500"



  # 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

后台服务

package main

import (
	"fmt"
	"net/http"
)

var HTTP_PRE = "/server_1"

func HelloHandle(w http.ResponseWriter, r *http.Request)  {
	_, _ = fmt.Fprint(w, " hello! this is go http server 1. port = 8888")
}


func main() {

	http.HandleFunc(HTTP_PRE + "/hello", HelloHandle)

	if e := http.ListenAndServe(":8888", nil); e!= nil{
		panic(e.Error())
	}
}

启动consul服务:

consul.exe agent -dev -ui 

向consul的kv存储中插入以下键值对:

consul kv put traefik/http/routers/myrouter-1/rule PathPrefix(`/server_1`)
consul kv put traefik/http/routers/myrouter-1/entrypoints/0	web
consul kv put traefik/http/routers/myrouter-1/service myservice-1
consul kv put traefik/http/services/myservice-1/loadbalancer/servers/0/url http://127.0.0.1:8888

启动两个go http服务

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

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

level=debug msg="Configuration received from provider consul: {"http":{"routers":{"myrouter-1":{"entryPoints":["web"],"service":"myservice-1","rule":"PathPrefix(`/server_1`)"}},"services":{"myservice-1":{"loadBalancer":{"servers":[{"url":"http://127.0.0.1:8888"}],"passHostHeader":true}}}}}" providerName=consul

浏览器访问 http://127.0.0.1/server_1/hello 请求被转发到后端的服务

参考链接:

https://www.consul.io/commands/kv

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

https://doc.traefik.io/traefik/reference/dynamic-configuration/kv/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值