1首先创建一个json的配置文件,文件名随便取,我取Ocelot.json

这个配置文件有两种配置方式,第一种,手动填写 服务所在的ip和端口;第二种,用Consul进行服务发现

第一种如下:

  1. {
  2. "ReRoutes": [
  3. {
  4. //转发处理格式
  5. "DownstreamPathTemplate": "/api/{url}",
  6. "DownstreamScheme": "http",
  7. //手动指明ip和端口号
  8. "DownstreamHostAndPorts": [
  9. {
  10. "Host": "localhost",
  11. "Port":
  12. }
  13. ],
  14. //请求格式
  15. "UpstreamPathTemplate": "/Ocelot_Consul_Service/{url}",
  16. "UpstreamHttpMethod": [ "Get", "Post" ]
  17. }
  18. ]
  19. //例如,我的Ocelot ip是127.0.0.1 端口是8888的情况下,
  20. //我请求的是localhost:8888/Ocelot_Consul_Service/values
  21. //会被转到localhost 的6001端口 6001端口对应的是 Ocelot_Consul_Service 对应的webapi
  22. //请求转后的路径是:localhost:6001/api/Ocelot_Consul_Service/values
  23. }

第二种如下:

  1. {
  2. "ReRoutes": [
  3. {
  4. "DownstreamPathTemplate": "/api/{url}",
  5. "DownstreamScheme": "http",
  6.  
  7. "UpstreamPathTemplate": "/Ocelot_Consul_Service/{url}",
  8. "UpstreamHttpMethod": [ "Get", "Post" ],
  9. //指明服务名
  10. "ServiceName": "Ocelot_Consul_Service",
  11. //指明负载平衡方式
  12. "LoadBalancerOptions": {
  13. "Type": "RoundRobin" //轮询
  14. },
  15. //使用服务发现
  16. "UseServiceDiscovery": true
  17. }
  18.  
  19. ],
  20. //全局配置
  21. "GlobalConfiguration": {
  22. //服务发现的提供者
  23. "ServiceDiscoveryProvider": {
  24. //ip
  25. "Host": "localhost",
  26. //端口
  27. "Port": ,
  28. //由Consul提供服务发现
  29. "Type": "Consul"
  30. }
  31. }
  32. }

2.接下来我们要安装Ocelot  install-package Ocelot

3.安装完毕 要在Program.cs文件中使用第一步中创建的json文件,把它读到配置里面去。

  1. public static IWebHostBuilder CreateWebHostBuilder(string[] args)
  2. {
  3. //解析出从控制台传入的ip和端口号
  4. var config = new ConfigurationBuilder()
  5. .AddCommandLine(args)
  6. .Build();
  7. string ip = config["ip"];
  8. string port = config["port"];
  9.  
  10. return WebHost.CreateDefaultBuilder(args)
  11. .UseUrls($"http://{ip}:{port}")
  12. //注册应用配置
  13. .ConfigureAppConfiguration((hostingContext,builder)=> {
  14. //false 此文件是否是可选的,不是!true 如果此文件被修改了是否重新加载 是!
  15. builder.AddJsonFile("Ocelot.json", false, true);
  16. })
  17. .UseStartup<Startup>();
  18. }

4.在启动类(startup.cs)文件中添加Ocelot服务

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. //这个AddOcelot方法是Ocelot包给IServiceCollection扩展的方法
  4. //如果不使用Consul进行服务发现,只需要services.AddOcelot(configuration)即可
  5. //但是如果使用Consul进行服务发现 后面还要AddConsul()
  6. //要使用AddConsul()必须安装包 Ocelot.Provider.Consul
  7. services.AddOcelot(configuration).AddConsul();
  8. }

一定要注意第4步,使用Consul做服务发现要安装 Ocelot.Provider.Consul 包 并AddConsul()。在实际中 我们要尽量要用Consul进行服务发现。

附上Ocelot文档截图一张如下:

使用Ocelot做网关的更多相关文章

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

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

  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网关教程(6)- 配置管理

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

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

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

随机推荐

  1. 【b091&&z11】潜伏者

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动. 历尽艰险后,潜伏于S国的R国间谍小 ...

  2. GCD下载后清除缓存

    //GCD下载后清除缓存1 —(void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; //清除缓存 [self.cache re ...

  3. [NodeJS] Use Now alias for custom sub-domains

    Now is a great way to deploy your node application, but the generated URLs aren't memorable or easil ...

  4. [TypeScript] The Basics of Generics in TypeScript

    It can be painful to write the same function repeatedly with different types. Typescript generics al ...

  5. kafka集群操作指南

    目录 kafka集群操作指南 (一)单机版安装 (二)集群安装 (三)集群启停操作 (四)topic相关的操作 (五)某个broker挂掉,本机器可重启 (六)某个broker挂掉且无法重启,需要其它 ...

  6. angular表单的使用实例

    原文 https://www.jianshu.com/p/da1fd5396798 大纲 1.模板驱动表单的创建 2.响应式表单的创建 3.模板驱动型表单的自定义指令 4.响应式表单的自定义指令 5. ...

  7. 神奇校车 = topsage

    https://post.smzdm.com/p/6356/ 适合6岁至99岁的小盆友看的<The Magic School Bus> (神奇校车) http://club.topsage ...

  8. 【29.27%】【hdu 5908】Abelian Period

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) 问题描述 设SS是一个数字串,定义 ...

  9. 1.2.4 Java Annotation 提要

    (本文是介绍依赖注入容器Spring和分析JUnit源码的准备知识) Java Annotation(标注) java.lang.annotation.Annotation是全部Java标注的父接口. ...

  10. 【42.07%】【codeforces 558A】Lala Land and Apple Trees

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...