.net 这几年国内确实不好过。 很多都选择转行。不过.net Core跨平台 开源之后 。社区的生态在慢慢建立。往好的趋势发展。 对于坚守在.NET战线的开发者来说 是个挺不错的消息。 

特别是微软收购75亿美金GitHub。.net 生态 社区圈子。肯定会有所上升。 发展趋势越来越好。(当然 这只是我个人祈愿) 

最近也比较懒。也有段时间没有写过文章了。  但对于追寻新技术渴望学习 是不会断的 

最近微服务比较火热。 将以个大型项目根据业务可以拆分成一个个的较小的独立项目。便于管理 且互相协作

什么是Ocelot

Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly Tracing集成。这些功能只都只需要简单的配置即可完成

但是现在网上关于Ocelot的文章还是有限。

首先感谢几位大佬 Ocelot的文章  

https://www.cnblogs.com/shanyou/p/7787183.html(张友善 大神)

https://www.cnblogs.com/Leo_wl/p/7852311.htmlHackerVirus 大神)

https://www.cnblogs.com/jesse2013/p/net-core-apigateway-ocelot-docs.html腾飞(Jesse) 大神)

首先用VS2017 新建一个 .NET Core Api

1.NuGet控制台 安装Ocelot

PM> Install-Package Ocelot

2.在项目 Startup.cs 修改

这里需要用到两个比较重要的命名空间

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

 public Startup(IHostingEnvironment environment)
{
var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
builder.SetBasePath(environment.ContentRootPath)
.AddJsonFile("appsettings.json", false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: false, reloadOnChange: true)
.AddJsonFile("configuration.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables(); Configuration = builder.Build();
}
 /// <summary>
///配置
/// </summary>
public IConfigurationRoot Configuration { get; } /// <summary>
/// 配置服务
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
Action<ConfigurationBuilderCachePart> settings = (x) =>
{
x.WithMicrosoftLogging(log =>
{
log.AddConsole(LogLevel.Debug); }).WithDictionaryHandle();
};
services.AddOcelot(Configuration, settings);
//services.AddMvc();
}
 /// <summary>
/// 配置Ocelot
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
await app.UseOcelot();
//app.UseMvc();
}

然后是入口程序Main

       public static void Main(string[] args)
{
IWebHostBuilder builder = new WebHostBuilder();
builder.ConfigureServices(s =>
{
s.AddSingleton(builder);
});
builder.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights();
var host = builder.Build();
host.Run();
}

3. 新建一个命名为configuration的Json 文件

增加配置 如下

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHost": "localhost",
"DownstreamPort": ,
"UpstreamPathTemplate": "/api/values",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": ,
"DurationOfBreak": ,
"TimeoutValue":
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false
},
"AuthenticationOptions": { }
},
{
"DownstreamPathTemplate": "/api/product",
"DownstreamScheme": "http",
"DownstreamPort": ,
"DownstreamHost": "localhost",
"UpstreamPathTemplate": "/api/product",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": ,
"DurationOfBreak": ,
"TimeoutValue":
},
"AuthenticationOptions": { }
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/admin"
}
}

然后新建两个APi项目

并设置他们端口号为1001  1002

另一个类似

上面Json配置体现了上游请求和下游服务间的映射关系   上游是客户端直接调用的URL ,下游,则是对应我们开发的服务。

然后 你可以设置多项目启动 也可以单独开启新实例

接下 F5启动项目

最后附上Ocelot在GitHub上源码地址

https://github.com/TomPallister/Ocelot

.NET Core 玩一玩 Ocelot API网关的更多相关文章

  1. .NET Core微服务二:Ocelot API网关

    .NET Core微服务一:Consul服务中心 .NET Core微服务二:Ocelot API网关 .NET Core微服务三:polly熔断与降级 本文的项目代码,在文章结尾处可以下载. 本文使 ...

  2. ASP.NET Core on K8S学习之旅(13)Ocelot API网关接入

    本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 上一篇介绍了Ingress的基本概念和Nginx Ingress的基本配置和使 ...

  3. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(四)

    在上一讲中,我们已经完成了一个完整的案例,在这个案例中,我们可以通过Angular单页面应用(SPA)进行登录,然后通过后端的Ocelot API网关整合IdentityServer4完成身份认证.在 ...

  4. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(一)

    好吧,这个题目我也想了很久,不知道如何用最简单的几个字来概括这篇文章,原本打算取名<Angular单页面应用基于Ocelot API网关与IdentityServer4+ASP.NET Iden ...

  5. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(二)

    上文已经介绍了Identity Service的实现过程.今天我们继续,实现一个简单的Weather API和一个基于Ocelot的API网关. 回顾 <Angular SPA基于Ocelot ...

  6. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(三)

    在前面两篇文章中,我介绍了基于IdentityServer4的一个Identity Service的实现,并且实现了一个Weather API和基于Ocelot的API网关,然后实现了通过Ocelot ...

  7. 微服务(入门三):netcore ocelot api网关结合consul服务发现

    简介 api网关是提供给外部调用的统一入口,类似于dns,所有的请求统一先到api网关,由api网关进行指定内网链接. ocelot是基于netcore开发的开源API网关项目,功能强大,使用方便,它 ...

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

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

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

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

随机推荐

  1. Intellij IDEA 热部署处理

     1. 首先参考IDEA热部署同行经验分享:  Intellij IDEA 4种配置热部署的方法 2. IDEA 热部署实战: springboot项目: 不要引入热部署工具包spring-boot- ...

  2. node.js压缩版 Windows安装

    1.下载 下载地址:https://nodejs.org/zh-cn/download/ 选择相应的版本下载 2.解压缩 将文件解压到要安装的位置,并新建两个目录 node-global :npm全局 ...

  3. silverlight的Datagrid控件列绑定属性笔记

    <data:DataGridTemplateColumn Header="给作者留言"> <data:DataGridTemplateColumn.CellTem ...

  4. MVC仓储使用join

    代码: var result = from mpc in this.Context.Set<Domain.S_MENU_PURVIEWCODE>() join menu in this.C ...

  5. Python发送邮件不需要发件人密码认证

    #!/usr/bin/python # coding: UTF-8 import smtplib from email.mime.text import MIMEText receivers_list ...

  6. OSGi 系列(十八)之 基于注解的 Blueprint

    OSGi 系列(十八)之 基于注解的 Blueprint 1. 注解实现 blueprint 第一步:bundle 添加 Bundle-Blueprint-Annotation <plugin& ...

  7. lnmp vhost 虚拟目录配置

    以前常用Windows 很熟悉,lnmp 配置虚拟目录也很简单. 安装完lnmp环境之后,在nginx的配置文件夹下,我采用的方法是复制default.conf 然后重命名为vhost_a.conf ...

  8. C++11的一般概念——The C++ standard library, 2nd Edition 笔记(二)

    ( 原书第四章,General Concepts) 只介绍新内容,关于头文件格式和后缀等C++03已经规范化的内容,不再赘述. namespace std:新的std子空间包括: std::tr1, ...

  9. Android 在一个程序中启动另一个程序(包名,或者类名)

    http://hi.baidu.com/xiaofanqing/item/6fd724f7c5bb6dce531c26b7 Android 开发有时需要在一个应用中启动另一个应用,比如Launcher ...

  10. KbmMW 认证管理器说明(转载)

    这是kbmmw 作者关于认证管理器的说明,我懒得翻译了,自己看吧. There are 5 parts of setting up an authorization manager: A) Defin ...