-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-compose.yml
More file actions
133 lines (124 loc) · 3.85 KB
/
docker-compose.yml
File metadata and controls
133 lines (124 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
version: '3.8'
services:
# 服务发现组件
consul:
image: consul:1.15
container_name: consul
restart: always
ports:
- "8500:8500" # Web UI
- "8600:8600/udp" # DNS服务
volumes:
- consul-data:/consul/data
command: agent -server -bootstrap-expect=1 -ui -client=0.0.0.0
networks:
- microservice-network
# API网关 (Traefik)
traefik:
image: traefik:v2.10
container_name: traefik
restart: always
ports:
- "80:80" # HTTP入口
- "8080:8080" # Traefik Dashboard
volumes:
- ./traefik/traefik.yml:/etc/traefik/traefik.yml
- ./traefik/dynamic-config:/etc/traefik/dynamic-config
- /var/run/docker.sock:/var/run/docker.sock:ro
depends_on:
- consul
networks:
- microservice-network
# 认证服务
auth-service:
build:
context: ./services/auth
container_name: auth-service
restart: always
environment:
- SERVICE_NAME=auth-service
- CONSUL_HOST=consul
- CONSUL_PORT=8500
- JWT_SECRET_KEY=your-secret-key-here
- JWT_ACCESS_TOKEN_EXPIRE_MINUTES=30
depends_on:
- consul
labels:
- "traefik.enable=true"
- "traefik.http.services.auth-service.loadbalancer.server.port=8000"
- "traefik.http.routers.auth-service.rule=PathPrefix(`/api/auth`)"
- "traefik.http.routers.auth-service.entrypoints=web"
networks:
- microservice-network
# 示例业务服务1
user-service:
build:
context: ./services/user
container_name: user-service
restart: always
environment:
- SERVICE_NAME=user-service
- CONSUL_HOST=consul
- CONSUL_PORT=8500
depends_on:
- consul
- auth-service
labels:
- "traefik.enable=true"
- "traefik.http.services.user-service.loadbalancer.server.port=8000"
- "traefik.http.routers.user-service.rule=PathPrefix(`/api/users`)"
- "traefik.http.routers.user-service.entrypoints=web"
- "traefik.http.routers.user-service.middlewares=auth-middleware@file"
- "traefik.http.routers.user-service.priority=30"
networks:
- microservice-network
# 示例业务服务2
product-service:
build:
context: ./services/product
container_name: product-service
restart: always
environment:
- SERVICE_NAME=product-service
- CONSUL_HOST=consul
- CONSUL_PORT=8500
depends_on:
- consul
- auth-service
labels:
- "traefik.enable=true"
- "traefik.http.services.product-service.loadbalancer.server.port=8000"
- "traefik.http.routers.product-service.rule=PathPrefix(`/api/products`)"
- "traefik.http.routers.product-service.entrypoints=web"
- "traefik.http.routers.product-service.middlewares=auth-middleware@file"
- "traefik.http.routers.product-service.priority=30"
networks:
- microservice-network
# 前端服务
frontend-service:
build:
context: ./services/frontend
container_name: frontend-service
restart: always
depends_on:
- auth-service
- user-service
- product-service
labels:
- "traefik.enable=true"
- "traefik.http.services.frontend-service.loadbalancer.server.port=80"
- "traefik.http.routers.frontend-service.rule=PathPrefix(`/`) && !PathPrefix(`/api`)"
- "traefik.http.routers.frontend-service.entrypoints=web"
- "traefik.http.routers.frontend-service.priority=10"
# 为前端API请求添加单独路由并应用认证中间件
- "traefik.http.routers.frontend-api.rule=PathPrefix(`/api`)"
- "traefik.http.routers.frontend-api.entrypoints=web"
- "traefik.http.routers.frontend-api.middlewares=auth-middleware@file"
- "traefik.http.routers.frontend-api.priority=20"
networks:
- microservice-network
networks:
microservice-network:
driver: bridge
volumes:
consul-data: