{"id":25627383,"url":"https://github.com/xincao9/infra-gateway","last_synced_at":"2026-06-20T13:31:21.267Z","repository":{"id":277838282,"uuid":"933679876","full_name":"xincao9/infra-gateway","owner":"xincao9","description":"一个开箱即用的业务网关服务","archived":false,"fork":false,"pushed_at":"2025-02-23T03:17:55.000Z","size":108,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-13T13:04:36.699Z","etag":null,"topics":["gateway-api","spring-cloud-gateway"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xincao9.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-16T12:58:14.000Z","updated_at":"2025-02-23T03:17:58.000Z","dependencies_parsed_at":"2025-02-16T14:37:49.280Z","dependency_job_id":"f79a77c6-df81-4b18-80fb-0482d4f4fee1","html_url":"https://github.com/xincao9/infra-gateway","commit_stats":null,"previous_names":["xincao9/infra-gateway"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xincao9/infra-gateway","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xincao9%2Finfra-gateway","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xincao9%2Finfra-gateway/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xincao9%2Finfra-gateway/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xincao9%2Finfra-gateway/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xincao9","download_url":"https://codeload.github.com/xincao9/infra-gateway/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xincao9%2Finfra-gateway/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34572423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gateway-api","spring-cloud-gateway"],"created_at":"2025-02-22T17:48:30.822Z","updated_at":"2026-06-20T13:31:21.262Z","avatar_url":"https://github.com/xincao9.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Infra-Gateway\n\n一个开箱即用的业务网关服务，基于 **Spring Cloud Gateway** 和 **Nacos** 实现动态路由、流量控制和监控支持。\n\n---\n\n## 功能特性\n\n- **动态路由**：通过 Nacos 配置中心实现路由规则的动态管理。\n- **流量控制**：集成 Sentinel，支持基于路由和 API 的限流。\n- **服务发现**：通过 Nacos Discovery 实现服务注册与路由。\n- **监控支持**：集成 Prometheus，提供网关性能指标。\n\n---\n\n## 快速开始\n\n### 1. 服务发现与配置\n\n#### `application.yml`\n\n基础配置，用于服务发现和静态路由定义。\n\n```yaml\nspring:\n  cloud:\n    nacos:\n      discovery:\n        server-addr: 127.0.0.1:8848\n        username: nacos\n        password: nacos\n        namespace: api\n    gateway:\n      discovery:\n        locator:\n          enabled: true                # 启用服务发现路由\n          lower-case-service-id: true  # 服务 ID 小写化\n      routes:\n        - id: sample-route\n          uri: lb://sample           # 通过服务名路由\n          predicates:\n            - Path=/sample/**\n          filters:\n            - StripPrefix=1\n```\n\n#### 动态路由配置（Nacos）\n\n在 Nacos 中配置动态路由规则，支持实时更新。\n\n- **Namespace**: `api`\n- **Data ID**: `gateway-routes.yaml`\n- **Group**: `infra-gateway`\n- **格式**: JSON OR YAML\n\n```yaml\n- id: sample-route\n  uri: lb://sample\n  predicates:\n  - Path=/sample/**\n  filters:\n  - StripPrefix=1\n```\n\n---\n\n### 2. 流量控制（Sentinel）\n\n通过 Sentinel 实现网关的限流配置，规则存储在 Nacos 中。\n\n#### 配置示例\n\n- **Namespace**: `sentinel`\n- **Data ID**: `infra-gateway-flow-rules`\n- **Group**: `infra-gateway`\n- **格式**: JSON\n\n```json\n[\n  {\n    \"resource\": \"sample-route\",\n    \"resourceMode\": 0,\n    \"grade\": 1,\n    \"count\": 1,\n    \"intervalSec\": 1,\n    \"controlBehavior\": 0,\n    \"burst\": 0\n  },\n  {\n    \"resource\": \"sample-api\",\n    \"resourceMode\": 1,\n    \"grade\": 1,\n    \"count\": 1,\n    \"intervalSec\": 1,\n    \"controlBehavior\": 0,\n    \"burst\": 0\n  }\n]\n```\n\n#### 参数说明\n\n| 参数                | 说明                 | 可选值                                   |\n|-------------------|--------------------|---------------------------------------|\n| `resource`        | 限流资源（如路由 ID 或 API） | 自定义字符串                                |\n| `resourceMode`    | 资源类型               | 0（路由）/ 1（API）                         |\n| `grade`           | 限流类型               | 0 (线程数) / 1（QPS）                      |\n| `count`           | 限流阈值               | 数值（如 5、10）                            |\n| `intervalSec`     | 统计时间窗口（秒）          | 整数（如 1）                               |\n| `controlBehavior` | 限流行为               | 0 (直接拒绝) / 1(预热) / 2(限流) / 3(预热 + 限流) |\n| `burst`           | 突发流量容量             | 0（禁用）或正整数                             |\n\n---\n\n### 3. 监控（Prometheus）\n\n集成 Prometheus 收集网关指标，便于性能监控。\n\n#### `application.yml`\n\n```yaml\nspring:\n  cloud:\n    gateway:\n      metrics:\n        enabled: true            # 启用 Gateway Metrics\nmanagement:\n  endpoints:\n    web:\n      exposure:\n        include: \"*\"           # 暴露所有端点\n      base-path: /management   # 自定义端点路径\n  metrics:\n    export:\n      prometheus:\n        enabled: true          # 启用 Prometheus 指标导出\n```\n\n#### 指标拉取地址\n\n- **URL**: `http://localhost:10101/management/prometheus`\n- **说明**: Prometheus 可通过此端点拉取指标，如请求延迟、吞吐量等。\n\n---\n\n## 使用步骤\n\n1. **启动 Nacos**：确保 Nacos 服务运行在 `127.0.0.1:8848`，并创建 `api` 和 `sentinel` 命名空间。\n2. **配置路由与限流**：在 Nacos 中添加上述配置。\n3. **运行网关**：启动 Spring Boot 应用。\n4. **测试路由**：访问 `http://localhost:10101/sample/xxx`，验证路由和限流是否生效。\n5. **监控指标**：配置 Prometheus 拉取指标，并结合 Grafana 可视化。\n\n---\n\n## 依赖与参考\n\n- **Spring Cloud Gateway\n  **: [官方文档](https://docs.spring.io/spring-cloud-gateway/docs/3.1.9/reference/html/#gateway-starter)\n- **Nacos**: [阿里云快速开始](https://start.aliyun.com/)\n- **Sentinel**: [GitHub](https://github.com/alibaba/Sentinel)\n- **Prometheus**: [官方文档](https://prometheus.io/docs/introduction/overview/)\n\n---\n\n## 注意事项\n\n- **版本兼容性**：确保 Spring Boot、Spring Cloud 和 Spring Cloud Alibaba 版本匹配。\n- **安全性**：生产环境中为 Nacos 和 Prometheus 端点添加认证。\n- **动态刷新**：路由和限流规则修改后会自动生效，无需重启服务。","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxincao9%2Finfra-gateway","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxincao9%2Finfra-gateway","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxincao9%2Finfra-gateway/lists"}