Fork me on GitHub

(四)surging 微服务框架使用系列之网关

一、什么是API网关

  API网关是一个服务器,是系统对外的唯一入口。API网关封装了系统内部架构,为每个客户端提供一个定制的API。API网关方式的核心要点是,所有的客户端和消费端都通过统一的网关接入微服务,在网关层处理所有的非业务功能。通常,网关也是提供REST/HTTP的访问API。服务端通过API-GW注册和管理服务。

二、网关的职责

  网关具有的职责,如身份验证、监控、负载均衡、缓存、请求分片与管理、静态响应处理。当然,最主要的职责还是与“外界联系”。

三、surging的网关架构设计

  https://www.cnblogs.com/fanliang11/p/7441281.html

四、surging的网关使用

  1.配置

  

  在surging网关的configs文件下有三个配置。appsettings:自定义的配置。cacheSettings:缓存的配置。gatewaySettings:网关配置。

  gatewaySettings 配置:

复制代码
 1 {
 2   "AccessTokenExpireTimeSpan": "30",//token过期时间,单位分钟
 3   "AuthorizationRoutePath": "api/userapp/authentication",//token服务验证方法
 4   "AuthorizationServiceKey": "UserApp",//服务对应的ModuleName
 5   "TokenEndpointPath": "api/oauth2/token",//token请求对外路径
 6   "CacheMode": "ddlCache.Redis", //MemoryCache or  gateway.Redis save token(缓存配置)
 7   //跨域配置
 8   "AccessPolicy": {
 9     "Origins": [ "http://127.0.0.1:1927", "http://localhost:927" ],
10     "AllowAnyHeader": true,
11     "AllowAnyMethod": true
12   },
13   //注册中心配置(zookeeper或者consul)
14   "Register": {
15     "Provider": "Consul",
16     "Address": "127.0.0.1:8500"
17   },
18   //服务聚合配置
19   "ServicePart": {
20     "MainPath": "part/service/aggregation",
21     "EnableAuthorization": false,
22     "Services": [
23       {
24         "ServiceAggregation": [
25           {
26             "RoutePath": "api/user/GetUserName",
27             "ServiceKey": "User",
28             "Key": "Users"
29           },
30           {
31             "RoutePath": "api/user/GetUserName",
32             "ServiceKey": "User",
33             "Key": "Roles"
34           }
35         ],
36         "UrlMapping": "part/user/getuserrole"
37       }
38     ]
39   }
40 }
复制代码

  2.将网关发布。

  3.将服务启动

  4.浏览网页(以下表示成功)

  5.请求token jwt模式(postman)

  Content-Type:application/json

  请求参数:input 是api/userapp/authentication方法对应的参数名称。里面的对象是你真正的要传的参数对象

 

  如上图就表示token请求成功

posted @   phil_guo  阅读(5639)  评论(3编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示