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

时间:2023-03-08 22:02:28
(四)surging 微服务框架使用系列之网关

一、什么是API网关

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

二、网关的职责

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

三、surging的网关架构设计

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

四、surging的网关使用

  1.配置

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

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

  gatewaySettings 配置:

 {
"AccessTokenExpireTimeSpan": "",//token过期时间,单位分钟
"AuthorizationRoutePath": "api/userapp/authentication",//token服务验证方法
"AuthorizationServiceKey": "UserApp",//服务对应的ModuleName
"TokenEndpointPath": "api/oauth2/token",//token请求对外路径
"CacheMode": "ddlCache.Redis", //MemoryCache or gateway.Redis save token(缓存配置)
//跨域配置
"AccessPolicy": {
"Origins": [ "http://127.0.0.1:1927", "http://localhost:927" ],
"AllowAnyHeader": true,
"AllowAnyMethod": true
},
//注册中心配置(zookeeper或者consul)
"Register": {
"Provider": "Consul",
"Address": "127.0.0.1:8500"
},
  //服务聚合配置
"ServicePart": {
"MainPath": "part/service/aggregation",
"EnableAuthorization": false,
"Services": [
{
"ServiceAggregation": [
{
"RoutePath": "api/user/GetUserName",
"ServiceKey": "User",
"Key": "Users"
},
{
"RoutePath": "api/user/GetUserName",
"ServiceKey": "User",
"Key": "Roles"
}
],
"UrlMapping": "part/user/getuserrole"
}
]
}
}

  2.将网关发布。

  3.将服务启动

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

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

  5.请求token jwt模式(postman)

  Content-Type:application/json

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

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

  如上图就表示token请求成功