
新建Gateway网关项目
项目介绍
API网关:Yarp Gateway
- 反向代理:能够接收客户端的请求,并将这些请求转发到适当的后端服务。
- 负载均衡:支持负载均衡功能,可以在多个后端服务之间分配请求,以提高系统的可扩展性和可靠性。
- 动态配置:支持在线动态添加和修改集群、路由绑定以及域名绑定HTTPS证书等配置,且这些更改可以即时生效,无需重启服务
更多功能请参考Yarp官方文档
安装或升级模板
cs
dotnet new install ZhonTai.Template.Gateway升级模板命令和安装模板命令相同
安装指定版本
cs
dotnet new install ZhonTai.Template.Gateway::10.1.0查看帮助
cs
dotnet new MyGateway -h-p, --port <port> Port settings
类型: int
默认: 16010卸载模板
cs
dotnet new uninstall ZhonTai.Template.Gateway创建网关项目
cs
dotnet new MyGateway -n MyCompanyName.MyGateway自定义网关端口项目
cs
dotnet new MyGateway -n MyCompanyName.MyGateway -p 16010appsettings.json
应用程序配置文件,包含路由配置、集群配置、证书配置等
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Urls": "http://*:16010",
"ReverseProxy": {
"Routes": {
// 权限管理
"admin": {
"ClusterId": "admin",
"Match": {
"Path": "/api/admin/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
},
"admin-doc": {
"ClusterId": "admin",
"Match": {
"Path": "/doc/admin/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
},
"admin-file": {
"ClusterId": "admin",
"Match": {
"Path": "/upload/{**catch-all}",
"Hosts": []
}
},
// 开发管理
"dev": {
"ClusterId": "dev",
"Match": {
"Path": "/api/dev/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
},
"dev-doc": {
"ClusterId": "dev",
"Match": {
"Path": "/doc/dev/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
},
// 系统管理,包含权限管理
"sys": {
"ClusterId": "admin",
"Match": {
"Path": "/api/sys/{**catch-all}",
"Hosts": []
}
},
"sys-doc": {
"ClusterId": "admin",
"Match": {
"Path": "/doc/sys/{**catch-all}",
"Hosts": []
}
},
// 业务管理
"biz": {
"ClusterId": "biz",
"Match": {
"Path": "/api/biz/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
},
"biz-doc": {
"ClusterId": "biz",
"Match": {
"Path": "/doc/biz/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
},
// 会员管理
"mem": {
"ClusterId": "mem",
"Match": {
"Path": "/api/mem/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
},
"mem-doc": {
"ClusterId": "mem",
"Match": {
"Path": "/doc/mem/{**catch-all}",
//"Methods": [ "POST", "GET", "DELETE", "PUT" ],
"Hosts": []
}
}
},
"Clusters": {
"admin": {
"Destinations": {
"destination1": {
"Address": "http://localhost:18010"
}
},
"LoadBalancingPolicy": "RoundRobin"
},
"dev": {
"Destinations": {
"destination1": {
"Address": "http://localhost:18020"
}
},
"LoadBalancingPolicy": "RoundRobin"
},
"biz": {
"Destinations": {
"destination1": {
"Address": "http://localhost:19010"
}
},
"LoadBalancingPolicy": "RoundRobin"
},
"mem": {
"Destinations": {
"destination1": {
"Address": "http://localhost:20010"
}
},
"LoadBalancingPolicy": "RoundRobin"
}
}
},
//网关配置
"GatewayConfig": {
//模块列表
"ModuleList": [
{
//文档名称
"Name": "权限接口文档",
//文档地址
"Url": "/doc/admin/index.html"
},
{
"Name": "系统接口文档",
"Url": "/doc/sys/index.html"
},
{
"Name": "开发接口文档",
"Url": "/doc/dev/index.html"
},
{
"Name": "业务接口文档",
"Url": "/doc/biz/index.html"
},
{
"Name": "会员接口文档",
"Url": "/doc/mem/index.html"
}
],
//健康检查
"HealthChecks": {
//启用
"Enable": true,
//访问路径
"Path": "/health"
}
}
}launchSettings.json
开发启动配置文件,用于配置启动参数,如环境变量、启动端口等。
launchSettings.json
{
"profiles": {
"MyGateway.Host": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:16010"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:16010",
"sslPort": 0
}
}
}