.Netcore 2.0 Ocelot Api网关教程(1)- 入门
Ocelot(Github)
Ocelot官方文档(英文)
本文不会介绍Api网关是什么以及Ocelot能干什么
需要对Api网关及Ocelot有一定的理论了解
开始使用Ocelot搭建一个入门级Api网关
1.新建3个WebApi项目,分别命名为OcelotGetway、WebApiA、WebApiB

- OcelotGetway项目用于做Api网关
- WebApiA、WebApiB作为两个api接口服务
2.在OcelotGetway项目的Nuget包管理器中搜索Ocelot并安装

或者在程序包管理器中输入Install-Package Ocelot安装
3.在OcelotGetway项目中新建json文件并命名为configuration.json在VS中右键修改为“始终复制”

4.编辑configuration.json文件并添加以下内容
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"UpstreamPathTemplate": "/webapia/values",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5002
}
],
"UpstreamPathTemplate": "/webapib/values",
"UpstreamHttpMethod": [ "Get" ]
}
]
}
5.修改OcelotGetway项目的Startup类如下
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOcelot(new ConfigurationBuilder()
.AddJsonFile("configuration.json")
.Build());
}
public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
await app.UseOcelot();
app.UseMvc();
}
6.修改WebApiA和WebApiB项目的ValuesController分别为
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1 from webapi A", "value2 from webapi A" };
}
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1 from webapi B", "value2 from webapi B" };
}
7.编辑Properties目录下的launchSettings.json文件中的applicationUrl(每个文件中有两个,只改下边的就可以)

分别设置3个项目的applicationUrl为:
- OcelotGetway: http://localhost:5000
- WebApiA: http://localhost:5001
- WebApiB: http://localhost:5002
8.分别运行WebApiA、WebApiB、OcelotGetway
浏览器分别访问
http://localhost:5000/webapia/values
http://localhost:5000/webapib/values

完,下一篇将介绍路由
作者:Weidaicheng
链接:https://www.jianshu.com/p/c967eda8b04d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
.Netcore 2.0 Ocelot Api网关教程(1)- 入门的更多相关文章
- .Netcore 2.0 Ocelot Api网关教程(2)- 路由
.Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...
- .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理
本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...
- .Netcore 2.0 Ocelot Api网关教程(7)- 限流
本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对 ...
- .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权
本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...
- .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation
本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...
- .Netcore 2.0 Ocelot Api网关教程(9)- QoS
本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly ...
- .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现
本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...
- .Netcore 2.0 Ocelot Api网关教程(8)- 缓存
Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...
- .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合
在实际的应用当中,经常会遇到同一个操作要请求多个api来执行.这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别.年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelo ...
随机推荐
- Webpack v4.8.3 快速入门指南
一.进入 https://webpack.docschina.org/ 官方文档,点击 "文档" 进入 文档页面,文档中包含 “概念,配置,API,指南,LOADERS,插件&q ...
- ACM-ICPC 2015 沈阳赛区现场赛 F. Frogs && HDU 5514(容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子.问所 ...
- [2019HDU多校第一场][HDU 6590][M. Code]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...
- Bootstrap Popover(弹出框)弹出自定义格式代码
HEAD 标签之间引入CSS:<link href="../../../public/css/bootstrap.min.css" rel="stylesheet& ...
- 解决document.getElementById("")在IE7中误读成name的bug
<!DOCTYPE html> <html lang="en"> <head> <meta char ...
- Vivado与Modelsim联合仿真
[转载]: 1:https://blog.csdn.net/weixin_37603007/article/details/82823965 2:https://blog.csdn.net/Piece ...
- org.apache.commons.io.FilenameUtils 常用的方法
/** * getExtension * 获取文件的后缀名 */ public static void testGetExtension() { String extension = Filename ...
- linux manual free memory
/proc/sys/vm/drop_caches (since Linux 2.6.16)Writing to this file causes the kernel to drop clean ca ...
- luogu P4843 清理雪道
嘟嘟嘟 这其实就是一个最小流的板子题.把每一条边的流量至少为1,然后建立附加源汇跑一遍最大流,连上\(t, s\),再跑一遍最大流就是答案. 刚开始我想错了:统计每一个点的出度和入度,去两者较大值\( ...
- sprintf简介
sprintf 基本用法 输入一段有特点的字符串 #include <cstdio> #include <cstring> using namespace std; int m ...