Skip to content

新建网关项目 9.0.0

项目介绍

API网关:Yarp Gateway

  • 反向代理:能够接收客户端的请求,并将这些请求转发到适当的后端服务。
  • 负载均衡:支持负载均衡功能,可以在多个后端服务之间分配请求,以提高系统的可扩展性和可靠性。
  • 动态配置:支持在线动态添加和修改集群、路由绑定以及域名绑定HTTPS证书等配置,且这些更改可以即时生效,无需重启服务

更多功能请参考Yarp官方文档

安装或升级模板

cs
dotnet new install ZhonTai.Template.Gateway

升级模板命令和安装模板命令相同

安装指定版本

cs
dotnet new install ZhonTai.Template.Gateway::9.0.0

查看帮助

cs
dotnet new MyGateway -h
-p, --port <port>   Port settings
                    类型: int
                    默认: 16010

创建网关项目

cs
dotnet new MyGateway -n MyCompanyName.MyGateway

自定义网关端口项目

cs
dotnet new MyGateway -n MyCompanyName.MyGateway -p 16010

appsettings.json 应用程序配置

appsettings.json
json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Urls": "http://*:16010",
  "ReverseProxy": {
    "Routes": {
      "admin": {
        "ClusterId": "admin",
        "Match": {
          "Path": "/api/admin/{*any}"
        }
      },
      "admin-doc": {
        "ClusterId": "admin",
        "Match": {
          "Path": "/doc/admin/{*any}"
        }
      }
    },
    "Clusters": {
      "admin": {
        "Destinations": {
          "destination1": {
            "Address": "http://localhost:18010"
          }
        },
        "LoadBalancingPolicy": "RoundRobin"
      }
    }
  },
  //网关配置
  "GatewayConfig": {
    //模块列表
    "ModuleList": [
      {
        //接口文档名称
        "Name": "权限接口文档",
        //接口文档地址
        "Url": "/doc/admin/index.html"
      }
    ],
    //健康检查
    "HealthChecks": {
      //启用
      "Enable": true,
      //访问路径
      "Path": "/health"
    }
  }
}

launchSettings.json 开发启动配置

launchSettings.json
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
    }
  }
}