/Creekdream.ApiGateway

API网关通用项目,基于Ocelot结合Consul的服务发现对各服务进行路由

Primary LanguageC#

API网关项目

项目结合Ocelot与Consul之间通讯进行服务发现或手动配置路由转发请求至各个服务,还新增Skywalking支持服务性能监控与追踪,项目提供了多种配置方式。

升级日志

支持如下特性

  1. 服务发现
  2. 负载、熔断、限流、降级
  3. 基于IdentityServer的授权认证
  4. 服务监控追踪
  5. 可使用携程Apollo配置中心接管配置

以上特性均可选配置。

使用配置文件配置Ocelot

完整配置方式,请参照官方文档:https://ocelot.readthedocs.io/en/latest/features/configuration.html

1. 手动路由配置示例

// appsettings.json 配置示例
{
  "ReRoutes": [
    {
      "ServiceName": "UserService",
      "UpstreamPathTemplate": "/userService/{url}",
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "http",
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      },
      "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT", "OPTIONS" ]
    }
  ],
  "GlobalConfiguration":{}
}

2.配置Consul服务发现

// appsettings.json 配置示例
{
  "ReRoutes": [],
  "GlobalConfiguration": {
    "BaseUrl": null,
    "ServiceDiscoveryProvider": {
      "Host": "127.0.0.1",
      "Port": 8500
    }
  }
}

结合携程阿波罗配置中心配置

携程Apollo的使用请参考项目:https://github.com/zengqinglei/Creekdream.Configuration.Apollo

可将appsetting中的ocelot配置转移至配置中心

// appsettings.json 配置示例
{
  "apollo": {
    "AppId": "PublicService",
    "MetaServer": "http://127.0.0.1:8080",
    "Namespaces": [ "application" ]
  }
}

image

构建Docker镜像与启动项目

一、构建Docker镜像

git clone https://github.com/zengqinglei/Creekdream.ApiGateway.git
cd Creekdream.ApiGateway/src/Creekdream.ApiGateway
docker build -t registry.cn-shenzhen.aliyuncs.com/creekdream/apigateway:0.1.1 .

二、 运行Docker镜像

docker run -d --name=creekdream-apigateway \
    --restart=always --network=host \
    -e 'ASPNETCORE_ENVIRONMENT=Production' \
    -e 'ASPNETCORE_URLS=http://0.0.0.0:53211' \
    -e 'apollo:AppId=PublicService' \
    -e 'apollo:MetaServer=http://192.168.0.103:8080' \
    registry.cn-shenzhen.aliyuncs.com/creekdream/apigateway:0.1.0