一、概要

Ocelot是.Net Core下一个开源API网关;Ocelot主要目标是在.NET在微服务或面向服务架构中提供统一的入口服务,

Ocelot拿到HttpRequest对象到管道后,先创建HttpRequestMessage对象,该对象用于向下游服务发出请求。再将HttpResponseMessage映射到HttpResponse对象上,并返回给客户端。。

主要功能:统一入口、认证、鉴权、限流熔断、内置了负载均衡等等

二、单网关QuickStart

2.1 API网关

新建.Net Core 2.0 webapi项目:Practice.ApiGetway

添加Ocelot的Nuget包引用到该项目:Install-Package Ocelot

修改Startup.cs:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOcelot(new ConfigurationBuilder().AddJsonFile("ocelot_config.json").Build());
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseOcelot().Wait();
app.UseMvc();
}
}

修改Program.cs:

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseUrls("http://*:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
}

添加一个json文件ocelot_config.json,设置属性:始终复制

{
"ReRoutes": [ {
"DownstreamPathTemplate": "/api/users",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port":
}
],
"UpstreamPathTemplate": "/users/values",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": ,
"DurationOfBreak": ,
"TimeoutValue":
}
},
{
"DownstreamPathTemplate": "/api/news",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port":
}
],
"UpstreamPathTemplate": "/news/values",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": ,
"DurationOfBreak": ,
"TimeoutValue":
}
}
], "GlobalConfiguration": {
//"BaseUrl": "https://api.mybusiness.com"
}
}

修改launchSettings.json设置个固定的端口

2.2 下游实际API

最后在新建两个API项目:

项目:Practice.NewsApi  、Controller:UsersController、端口:5002

项目:Practice.UsersApi  、Controller:NewsController、端口:5001

运行三个项目

不再截图,运行结果:

访问网关:http://localhost:5000/users/values   会得到 http://localhost:5001/api/users 结果

访问网关:http://localhost:5000/news/values   会得到 http://localhost:5002/api/news  结果

资源:

项目开源地址:https://github.com/ThreeMammals/Ocelot

官方文档:http://ocelot.readthedocs.io/en/latest/

资源:https://github.com/geffzhang/awesome-ocelot

博客:

http://www.cnblogs.com/jesse2013/p/net-core-apigateway-ocelot-docs.html

https://www.cnblogs.com/jackcao/

最新文章

  1. oracle 存储过程
  2. 浅谈JavaScript中forEach与each
  3. python 模块包裹
  4. 几种简单的负载均衡算法及其Java代码实现
  5. ES6之let(理解闭包)和const命令
  6. SQL Server里因丢失索引造成的死锁
  7. jQuery事件绑定和委托
  8. PHP 插入排序法
  9. Android——C语言、JNI与低层调用
  10. String类比较,String类运算比较,String运算
  11. LINUX 内核与 systemtap +GO 专家博客 一个[ 系统软件工程师] 的随手涂鸦
  12. UITableViewCell性能优化
  13. SharePoint代码段设计
  14. 使用MongoDB的开源项目(转)
  15. SQL 查询同一天日期内的数据
  16. HTML5中的DOM新特性
  17. Java版网络爬虫基础
  18. npm install报错
  19. python模块:xlsxwriter和xlrd相结合读取、写入excel文件
  20. 利用python将表格中的汉字转化为拼音

热门文章

  1. [Java并发编程(三)] Java volatile 关键字介绍
  2. featureCounts 软件说明
  3. MyBatis 配置多数据源
  4. 【Dubbo 源码解析】02_Dubbo SPI
  5. numpy学习之创建数组
  6. Oracle 行列转置
  7. Eclipse 左侧树形展示字体调节
  8. 使用ReentrantLock和Condition来代替内置锁和wait(),notify(),notifyAll()
  9. 基本的sqlplus命令
  10. 12:Css3的概念和优势