Ocelot(Github)
Ocelot官方文档(英文)
本文不会介绍Api网关是什么以及Ocelot能干什么
需要对Api网关及Ocelot有一定的理论了解

开始使用Ocelot搭建一个入门级Api网关

1.新建3个WebApi项目,分别命名为OcelotGetway、WebApiA、WebApiB

 
webapi项目.png
  • OcelotGetway项目用于做Api网关
  • WebApiA、WebApiB作为两个api接口服务

2.在OcelotGetway项目的Nuget包管理器中搜索Ocelot并安装

 
Nuget包管理器.png

或者在程序包管理器中输入
Install-Package Ocelot安装

3.在OcelotGetway项目中新建json文件并命名为configuration.json在VS中右键修改为“始终复制”

 
configuration.json.png

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(每个文件中有两个,只改下边的就可以)

 
launchSettings.json.png

分别设置3个项目的applicationUrl为:

8.分别运行WebApiA、WebApiB、OcelotGetway
浏览器分别访问
http://localhost:5000/webapia/values
http://localhost:5000/webapib/values

 
运行效果.png

源码下载

完,下一篇将介绍路由

作者:Weidaicheng
链接:https://www.jianshu.com/p/c967eda8b04d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

.Netcore 2.0 Ocelot Api网关教程(1)- 入门的更多相关文章

  1. .Netcore 2.0 Ocelot Api网关教程(2)- 路由

    .Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...

  2. .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理

    本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...

  3. .Netcore 2.0 Ocelot Api网关教程(7)- 限流

    本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对  ...

  4. .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权

    本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...

  5. .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation

    本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...

  6. .Netcore 2.0 Ocelot Api网关教程(9)- QoS

    本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly  ...

  7. .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现

    本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...

  8. .Netcore 2.0 Ocelot Api网关教程(8)- 缓存

    Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...

  9. .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合

    在实际的应用当中,经常会遇到同一个操作要请求多个api来执行.这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别.年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelo ...

随机推荐

  1. R中数据的输入和数据的标注

    数据的导入 默认情况下数据导入时,字符型变量将转化为因子.若不希望转化,可设置 stringsAsFactors=FALSE 从带分隔符的文本文件中导入数据 read.table() file --& ...

  2. Map遍历效率 : entrySet > keySet

     1    //entrySet()  2     for (Entry<String, String> entry : map.entrySet()) {  3         Stri ...

  3. mysql 指令

    // 授予用户某些权限GRANT ALL ON *.* TO 'USER'@'HOST';// 进入mysql访问特定数据库mysql -u user -p database_name// 查看数据表 ...

  4. P2051 [AHOI2009]中国象棋 大力DP

    状压个啥$qwq$ 思路:大力$DP$ 提交:2次(自信的开了$int$) 题解:(见注释) #include<cstdio> #include<iostream> using ...

  5. locate/find

    locate 从数据库 (/var/lib/mlocate/mlocate.db) 查找命令,使用updatedb更新库. 类似于数据库的索引建立,在首次简历索引的时候,很耗费资源,在建立完成后,查询 ...

  6. [Hihocoder] 字符串排序

    题目 http://hihocoder.com/problemset/problem/1712 题解 https://www.zybuluo.com/wsndy-xx/note/1135606

  7. 记录一:tensorflow下载安装

    1.下载Anaconda,默认选项安装 2.验证 conda --v 3.下载 tensorflow     3.1 创建环境 conda create -n tensorflow python=3. ...

  8. Python基础面试题整理

    基础 Python中lambda是什么意思 Python中的pass是什么意思 作为解释型语言,Python如何运行 什么是Python的单元测试 在Python中unittest是什么 如何将数字转 ...

  9. Python实用黑科技——解包元素(2)

    需求: 前面的文章讲的是使用变量的个数需要和迭代器数据变量的元素个数相同的方法,但更多的时候确实不想根据元素个数n来定义相应多的变量,而是希望用较少的变量( def drop_first_last(g ...

  10. python 二维数组 转 矩阵

    x = numpy.array([[,,],[,,],[,,]]) print x print x.shape 输出 [[ ] [ ] [ ]] (3L, 3L) [Finished .2s]