首页
/ 【亲测免费】 Apache ShenYu API网关使用教程

【亲测免费】 Apache ShenYu API网关使用教程

2026-01-30 04:30:43作者:余洋婵Anita

1. 项目介绍

Apache ShenYu是一个高性能、响应式的API网关解决方案,适用于所有微服务架构。它提供了代理、安全、API治理、可观察性等功能,并且支持多种语言客户端进行API注册,包括.NET、Python、Go和Java。

ShenYu API网关的主要特性包括:

  • 代理支持:Apache Dubbo、Spring Cloud、gRPC、Motan、SOFA、TARS、WebSocket、MQTT等。
  • 安全:签名、OAuth 2.0、JSON Web Tokens、WAF插件。
  • API治理:请求、响应、参数映射、Hystrix、RateLimiter插件。
  • 可观察性:追踪、指标、日志插件。
  • 仪表盘:动态流量控制,用户菜单权限的可视化后端。
  • 扩展性:插件热插拔,动态加载。
  • 集群:支持NGINX、Docker、Kubernetes。

2. 项目快速启动

创建网络

首先,创建一个Docker网络用于ShenYu:

docker network create shenyu

启动Apache ShenYu Admin

拉取ShenYu Admin镜像并启动容器:

docker pull apache/shenyu-admin
docker run -d --name shenyu-admin-quickstart -p 9095:9095 --net shenyu apache/shenyu-admin

启动Apache ShenYu Bootstrap

拉取ShenYu Bootstrap镜像并启动容器:

docker pull apache/shenyu-bootstrap
docker run -d --name shenyu-quickstart -p 9195:9195 -e "shenyu.local.enabled=true" -e SHENYU_SYNC_WEBSOCKET_URLS=ws://shenyu-admin-quickstart:9095/websocket --net shenyu apache/shenyu-bootstrap

设置路由规则

实际请求:

http://127.0.0.1:8080/helloworld

响应内容:

{
  "name": "Shenyu",
  "data": "hello world"
}

添加路由规则(独立模式):

localKey: 123456添加到Headers中。如果需要自定义localKey,可以使用sha512工具根据明文生成密钥,并更新shenyu.local.sha512Key属性。

curl --location --request POST 'http://localhost:9195/shenyu/plugin/selectorAndRules' \
--header 'Content-Type: application/json' \
--header 'localKey: 123456' \
--data-raw '{
  "pluginName": "divide",
  "selectorHandler": "[{\"upstreamUrl\":\"127.0.0.1:8080\"}]",
  "conditionDataList": [{
    "paramType": "uri",
    "operator": "match",
    "paramValue": "/**"
  }],
  "ruleDataList": [{
    "ruleHandler": "{\"loadBalance\":\"random\"}",
    "conditionDataList": [{
      "paramType": "uri",
      "operator": "match",
      "paramValue": "/**"
    }]
  }]
}'

代理请求:

http://localhost:9195/helloworld

响应内容:

{
  "name": "Shenyu",
  "data": "hello world"
}

3. 应用案例和最佳实践

(此部分将介绍Apache ShenYu在实际应用中的案例和最佳实践,具体内容根据实际项目情况编写。)

4. 典型生态项目

(此部分将介绍与Apache ShenYu协同使用的典型生态项目,具体内容根据实际项目情况编写。)

登录后查看全文
热门项目推荐
相关项目推荐