项目结合Ocelot与Consul之间通讯进行服务发现或手动配置路由转发请求至各个服务,还新增Skywalking支持服务性能监控与追踪,项目提供了多种配置方式。
- 服务发现
- 负载、熔断、限流、降级
- 基于IdentityServer的授权认证
- 服务监控追踪
- 可使用携程Apollo配置中心接管配置
以上特性均可选配置。
完整配置方式,请参照官方文档:https://ocelot.readthedocs.io/en/latest/features/configuration.html
// appsettings.json 配置示例
{
"ReRoutes": [
{
"ServiceName": "UserService",
"UpstreamPathTemplate": "/userService/{url}",
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"LoadBalancerOptions": {
"Type": "LeastConnection"
},
"UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT", "OPTIONS" ]
}
],
"GlobalConfiguration":{}
}
// appsettings.json 配置示例
{
"ReRoutes": [],
"GlobalConfiguration": {
"BaseUrl": null,
"ServiceDiscoveryProvider": {
"Host": "127.0.0.1",
"Port": 8500
}
}
}
携程Apollo的使用请参考项目:https://github.com/zengqinglei/Creekdream.Configuration.Apollo
// appsettings.json 配置示例
{
"apollo": {
"AppId": "PublicService",
"MetaServer": "http://127.0.0.1:8080",
"Namespaces": [ "application" ]
}
}
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 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