gateway/README.md

74 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# @go/gateway (高性能动态网关)
基于 `@go/service` 实现的微服务 API 网关。专注于作为“配置下发搬运工”,将繁重的路由转发交由底层 `service` 核心无锁处理。
详细路由语法与基础服务配置请参考:[https://apigo.cc/go/service](https://apigo.cc/go/service)
---
## 1. 基础配置 (env.yml)
```yaml
gateway:
Redis: "127.0.0.1:6379" # 配置中心 Redis
Prefix: "gateway" # 键名前缀 (默认为 gateway)
service:
Listen: ":80,http" # 监听端口
Register: "127.0.0.1:6379" # 服务发现注册中心
```
---
## 2. 动态路由管理 (Redis 示例)
网关以 **Host (域名)** 为单元管理配置,通过向频道发送“域名字符串”触发该域名的局部热更新。
### A. 配置存储 (HSET)
```bash
# 1. 添加 Host 到索引集 (必须)
SADD gateway:hosts "api.example.com"
# 2. 设置代理/重写/静态规则 (JSON 格式)
HSET gateway:host:api.example.com \
proxies '[
{"Path": "/user/*", "ToApp": "user-service", "ToPath": "/v1/*"},
{"Path": "/legacy", "ToApp": "http://10.0.0.1:8080", "ToPath": "/old"}
]' \
rewrites '[
{"Path": "^/api/([0-9]+)$", "ToPath": "/v2/api?id=$1"}
]' \
statics '{
"/ui": "/var/www/html"
}'
```
### B. 发布更新 (PUBLISH)
```bash
# 运维仅需发布一条域名消息,网关秒级无损更新
PUBLISH gateway:channel "api.example.com"
```
---
## 3. 命令行操作 (CLI)
```bash
./gateway start # 后台启动
./gateway status # 查看各域名健康状态与 IPC 信息
./gateway reload # 重载本地 YAML 配置并全量刷入 Redis 配置
./gateway stop # 优雅停止
```
## 4. 路由匹配速查
| 匹配模式 | 示例路径 (Path) | 说明 |
| :--- | :--- | :--- |
| **精确匹配** | `/login` | 必须完全一致 |
| **高性能前缀** | `/api/*` | **必须带 `/*` 结尾**,自动剥离前缀转发 |
| **正则匹配** | `^/user/(.*)$` | 包含 `(` 即视为正则,支持 `$1` 替换 |
---
*更多高级特性如 SSL、负载均衡超时、自动探测应用名等请查阅 [service 文档](https://apigo.cc/go/service)。*