【重要】traefik 配置

This commit is contained in:
编码猿
2025-09-30 10:01:58 +08:00
parent a3ba6c2944
commit 3c66faf9a7
3 changed files with 120 additions and 75 deletions

View File

@@ -2,7 +2,7 @@ version: '3.8'
services:
traefik:
image: traefik:v3.0
image: traefik:latest
container_name: traefik
networks:
- traefik-net
@@ -10,30 +10,18 @@ services:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik.yml:/etc/traefik/traefik.yml
- ./dynamic.yml:/etc/traefik/dynamic.yml # 动态配置文件
- ./dynamic.yml:/etc/traefik/dynamic.yml
- ./log-file.log:/etc/traefik/log-file.log
- ./access.log:/etc/traefik/access.log
restart: always
whoami:
image: traefik/whoami
image: traefik/whoami:latest
labels:
- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
# 2. Nacos 服务监听脚本
# nacos-cluster-watcher:
# user: "root"
# privileged: true
# image: node:24.8.0-alpine3.21
# container_name: nacos-cluster-watcher
# # network_mode: "host"
# networks:
# - traefik-net
# working_dir: /app
# volumes:
# - ./:/app # 挂载当前目录到容器
# command: sh -c "npm install && node src/index.js"
# restart: always
networks:
traefik-net:
driver: bridge

View File

@@ -1,68 +1,107 @@
http:
routers:
app-product-router:
rule: PathPrefix(`/app-product`)
service: app-product-service
entryPoints:
- web
middlewares:
- compress
- ipWhiteList
- rateLimit
- retries
main-app-router:
rule: PathPrefix(`/main-app`)
service: main-app-service
entryPoints:
- web
middlewares:
- compress
- ipWhiteList
- rateLimit
- retries
app-shared-router:
rule: PathPrefix(`/app-shared`)
rule: Host(`nacos.ddd.com`) && PathPrefix(`/app-shared`)
service: app-shared-service
observability:
metrics: true
accessLogs: true
tracing: true
traceVerbosity: detailed
entryPoints:
- web
middlewares:
- compress
- ipWhiteList
- rateLimit
- retries
app-product-router:
rule: Host(`nacos.ddd.com`) && PathPrefix(`/app-product`)
service: app-product-service
observability:
metrics: true
accessLogs: true
tracing: true
traceVerbosity: detailed
entryPoints:
- web
# 使用域名访问前端main-app
main-ddd-router:
rule: Host(`main.ddd.com`)
service: main-ddd-service
observability:
metrics: true
accessLogs: true
tracing: true
traceVerbosity: detailed
entryPoints:
- web
# Nacos 集群 管理后台的统一访问域名
nacosui-ddd-router:
rule: Host(`nacosui.ddd.com`)
service: nacosui-ddd-service
observability:
metrics: true
accessLogs: true
tracing: true
traceVerbosity: detailed
entryPoints:
- web
# trafik 网关 代理 内网 nacos 集群 的地址
nacos-ddd-router:
rule: Host(`nacos.ddd.com`)
service: nacos-ddd-service
observability:
metrics: true
accessLogs: true
tracing: true
traceVerbosity: detailed
entryPoints:
- web
# trafik 网关的管理后台的域名
trafikui-ddd-router:
rule: Host(`trafikui.ddd.com`)
service: trafikui-ddd-service
observability:
metrics: true
accessLogs: true
tracing: true
traceVerbosity: detailed
entryPoints:
- web
services:
app-product-service:
loadBalancer:
servers:
- url: http://192.168.31.101:1301
main-app-service:
loadBalancer:
servers:
- url: http://192.168.31.101:1300
app-shared-service:
loadBalancer:
servers:
- url: http://192.168.31.101:1333
middlewares:
rateLimit:
rateLimit:
average: 100
burst: 200
period: 1s
sourceCriterion:
ipStrategy:
depth: 2
compress:
compress:
excludedContentTypes:
- text/event-stream
minResponseBodyBytes: 1024
ipWhiteList:
ipWhiteList:
sourceRange:
- 127.0.0.1/32
- 192.168.107.0/24
retries:
retry:
attempts: 3
initialInterval: 200ms
app-product-service:
loadBalancer:
servers:
- url: http://192.168.31.101:1301
main-ddd-service:
loadBalancer:
servers:
- url: http://192.168.31.101:1300
nacosui-ddd-service:
loadBalancer:
servers:
- url: http://192.168.139.84:8080
- url: http://192.168.139.15:8080
- url: http://192.168.139.178:8080
nacos-ddd-service:
loadBalancer:
servers:
- url: http://192.168.139.84:8848
- url: http://192.168.139.15:8848
- url: http://192.168.139.178:8848
trafikui-ddd-service:
loadBalancer:
servers:
- url: http://192.168.139.80:8080

View File

@@ -10,7 +10,25 @@ entryPoints:
# 服务发现与配置源(关键:子项缩进必须一致)
providers:
docker: { }
# 动态路由规则文件
file:
filename: "/etc/traefik/dynamic.yml"
watch: true # 自动监听文件变化
log:
filePath: "/etc/traefik/log-file.log"
format: common
level: INFO
accessLog:
filePath: "/etc/traefik/access.log"
format: "json"
addInternals: true
filters:
retryAttempts: true
minDuration: "2ms"
fields:
defaultMode: "keep"
headers:
defaultMode: "keep"