靜態配置 traefik.yml
# 流量入口
entryPoints:
# 定義入口,任意起名
web:
# 監聽埠
address: :80
forwardedHeaders:
insecure: true
# http:
# redirections:
# #轉發到另一個入口
# entryPoint:
# to: websecure
# scheme: https
# 443入口,任意起名
websecure:
address: :443
forwardedHeaders:
insecure: true
# 動態配置
providers:
file:
filename: gateway/dynamic_conf.yml
# 作業日志
log:
filePath: gateway/traefik.log
# 訪問日志
accessLog:
filePath: gateway/access.log
# 保留在記憶體中的日志行數
bufferingSize: 100
fields:
names:
# 設定本地時區,不然默認是0時區時間輸出
StartLocal: keep
StartUTC: drop
api:
# false為啟用安全訪問
insecure: false
動態配置 dynamic_conf.yml
http:
routers:
#定義一個路由,任意起名
#專案前端
project-ui:
# 匹配規則,滿足規則后會執行中間件,在呼叫服務
rule: "Host(`project.com`)"
service: project-ui
# 匹配權重,0是無權重,越大越優先匹配
# priority: 0
middlewares:
- breaker
- limit
- errorpage
# 專案后端
project-backend:
# PathPrefix 路徑前綴 匹配 project.com/backend/*
rule: "Host(`project.com`) && PathPrefix(`/backend`)"
service: project-backend
middlewares:
- breaker
- limit
# 訪問 /backend/*,實際請求轉發的是 localhost:8082/*
- strip
- errorpage
# 服務中心
project-consul:
# PathPrefix 路徑前綴 匹配 project.com/consul/*,實際請求轉發的是 localhost:8500/consul/*
rule: "Host(`project.com`) && (PathPrefix(`/consul`)|| PathPrefix(`/v1`) )"
middlewares:
# 使用自定義的中間件
- auth
service: project-consul
# traefik 內置web儀表盤
dashboard:
rule: "Host(`project.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))"
# 內部api服務
service: api@internal
# 使用中間件,auth安全訪問
middlewares:
- auth
middlewares:
# 定義一個中間件,任意起名
# 用戶認證
auth:
# 使用官方的中間件
basicAuth:
usersFile: gateway/auth
# 斷路器
breaker:
circuitBreaker:
# 網路錯誤率達到10%時,打開斷路器
# 或者
# 25%的請求回傳5XX狀態(在回傳狀態代碼從0到5XX的請求中),打開斷路器
# 斷路器打開后,服務就會不可用,過段時間會自動恢復
expression: NetworkErrorRatio() > 0.10 || ResponseCodeRatio(500, 600, 0, 600) > 0.25
# 限流
limit:
rateLimit:
# 每秒請求數
average: 10
# 跳過前綴路徑
strip:
stripPrefix:
prefixes:
- /backend
# 錯誤頁面
errorpage:
errors:
# 指定狀態碼
status:
- 500-599
- 404
service: project-ui
# 服務提供錯誤頁面的網址
query: /{status}.html
services:
# 定義一個服務,任意起名
project-ui:
# 負載均衡,輪詢策略
loadBalancer:
# 配置多個服務器
servers:
- url: http://localhost:8081
healthCheck:
path: /health
interval: 10s
timeout: 3s
# 專案后端服務
project-backend:
# 負載均衡,輪詢策略
loadBalancer:
servers:
- url: http://localhost:8082
# 健康檢查,每隔10s,檢查一次,也就是去請求/health
healthCheck:
# 檢查路徑,自定義起名,自己服務器需支持此路徑
# 回傳狀態代碼2XX或3XX就認為是健康的,否則不健康會被移除
path: /health
# 檢查呼叫的頻率,每10秒呼叫一次
interval: 10s
# 超時時間設定
timeout: 3s
# 專案服務中心
project-consul:
# 負載均衡,輪詢策略
loadBalancer:
servers:
- url: http://localhost:8500
healthCheck:
path: /v1/agent/checks
interval: 10s
timeout: 3s
gateway/auth認證檔案,認證一次,所有子系統通用,退出瀏覽器失效
ps: 密碼必須使用MD5,SHA1或BCrypt進行哈希處理,可以使用htpasswd生成的密碼,
在線生成工具
test:$apr1$yjHwJ6kr$szcYpC.dwpayQYlZu4N5d1
啟動命令 --configFile 值為相對作業目錄的組態檔位置
traefik --configFile=gateway/traefik.yml
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260372.html
標籤:其他
上一篇:0元0基礎搭建個人網站簡易實操
