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. Introduction to Go Modules

    转:https://roberto.selbach.ca/intro-to-go-modules/ git init git add * git commit -am "First comm ...

  2. BZOJ 1022 / P4279 Luogu [SHOI2008]小约翰的游戏 (反Nim游戏) (Anti-SG)

    题意 反Nim游戏,两人轮流选一堆石子拿,拿到最后一个的输.问先手是否必胜. 分析 怎么说,分类讨论? 情形1:首先考虑最简单的情况,所有石子数都为1.那么奇数堆石子为必败,偶数为必胜 情形2:然后考 ...

  3. JDK、JRE、JVM的区别与关系

    JDK JDK是Java开发工具包,是Sun Microsystems针对Java开发员的产品. JDK中包含JRE,在JDK的安装目录下有一个名为jre的目录,里面有两个文件夹bin和lib,在这里 ...

  4. vue-quill-editor的用法

    1. main.js引入vue-quill-editor import VueQuillEditor from 'vue-quill-editor' import 'quill/dist/quill. ...

  5. Django从Models 10分钟定制一个Admin后台

    目录 Django从Models 10分钟建立一套RestfulApi Django从Models 10分钟定制一个Admin后台 简介 Django自带一个Admin后台, 支持用户创建,权限配置和 ...

  6. 用Python爬虫爬取“女神吧”上的照片。

    爬取的网页链接为https://tieba.baidu.com/p/5177270774 是一个美女警花哦! 所用Python环境为:python 3.3.2   用到的库为:urllib.reque ...

  7. P2089 烤鸡

    题目背景 猪猪hanke得到了一只鸡 题目描述 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末.孜然等),每种配料可以放1—3克, ...

  8. InnoDB缓存---InnoDB Buffer Pool

    InnoDB Buffer Pool 定义 对于InnoDB存储引擎,不管用户数据还是系统数据都是以页的形式存储在表空间进行管理的,其实都是存储在磁盘上的. 当InnoDB处理客户端请求,需要读取某页 ...

  9. 【零基础】简单说说一键果体APP的AI

    参考: https://www.jianshu.com/p/8c7a7cb7198c https://blog.csdn.net/gdymind/article/details/82696481 零. ...

  10. if-for-while

    if help if可以看看if的用法 if ls -l / ;then echo "ok";else echo "no" ;fi for for ((i=0; ...