1、在网关项目中通过nuget引入Ocelot

2、Startup.cs文件代码调整

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.HttpsPolicy;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using Ocelot.DependencyInjection;
  14. using Ocelot.Middleware;
  15. namespace ApiGateway
  16. {
  17. public class Startup
  18. {
  19. public Startup(IConfiguration configuration, IHostingEnvironment env)
  20. {
  21. //通过nuget引入如下包
  22. //Microsoft.Extensions.Options.ConfigurationExtensions
  23. //Microsoft.Extensions.Configuration.Json
  24. var builder = new ConfigurationBuilder()
  25. .SetBasePath(env.ContentRootPath)
  26. .AddJsonFile("configuration.json", true, true)
  27. .AddJsonFile($"appsettings.{env.EnvironmentName}.json");
  28. Configuration = builder.Build();
  29. }
  30. public IConfiguration Configuration { get; }
  31. public void ConfigureServices(IServiceCollection services)
  32. {
  33. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  34. services.AddOcelot(Configuration);
  35. }
  36. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  37. {
  38. if (env.IsDevelopment())
  39. {
  40. app.UseDeveloperExceptionPage();
  41. }
  42. else
  43. {
  44. app.UseHsts();
  45. }
  46. app.UseOcelot();
  47. app.UseHttpsRedirection();
  48. app.UseMvc();
  49. }
  50. }
  51. }

3、添加网关支持配置configuration.json

  1. {
  2. "ReRoutes": [
  3. {
  4. "DownstreamPathTemplate": "/api/order", /*下游路径模板*/
  5. "DownstreamScheme": "http",
  6. "DownstreamHostAndPorts": [
  7. {
  8. "Host": "localhost",
  9. "Port": 9001
  10. }
  11. ],
  12. "UpstreamPathTemplate": "/order", /*上游路径模板*/
  13. "UpstreamHttpMethod": [ "Get" ]
  14. },
  15. {
  16. "DownstreamPathTemplate": "/api/product",
  17. "DownstreamScheme": "http",
  18. "DownstreamHostAndPorts": [
  19. {
  20. "Host": "localhost",
  21. "Port": 9002
  22. }
  23. ],
  24. "UpstreamPathTemplate": "/product",
  25. "UpstreamHttpMethod": [ "Get" ]
  26. }
  27. ],
  28. "GlobalConfiguration": {
  29. "RequestIdKey": "OcRequestId",
  30. "AdministrationPath" : "administration"
  31. }
  32. }

4、运行网关项目验证,访问http://localhost:9000/product进行验证,localhost:9000是你网关项目的host

参考链接:https://www.cnblogs.com/yotsuki/p/7928095.html

Ocelot使用的更多相关文章

  1. 编译器开发系列--Ocelot语言7.中间代码

    Ocelot的中间代码是仿照国外编译器相关图书Modern Compiler Implementation 中所使用的名为Tree 的中间代码设计的.顾名思义,Tree 是一种树形结构,其特征是简单, ...

  2. 编译器开发系列--Ocelot语言6.静态类型检查

    关于"静态类型检查",想必使用C 或Java 的各位应该非常熟悉了.在此过程中将检查表达式的类型,发现类型不正确的操作时就会报错.例如结构体之间无法用+ 进行加法运算,指针和数值之 ...

  3. 编译器开发系列--Ocelot语言1.抽象语法树

    从今天开始研究开发自己的编程语言Ocelot,从<自制编译器>出发,然后再自己不断完善功能并优化. 编译器前端简单,就不深入研究了,直接用现成的一款工具叫JavaCC,它可以生成抽象语法树 ...

  4. API网关Ocelot 使用Polly 处理部分失败问题

    在实现API Gateway过程中,另外一个需要考虑的问题就是部分失败.这个问题发生在分布式系统中当一个服务调用另外一个服务超时或者不可用的情况.API Gateway不应该被阻断并处于无限期等待下游 ...

  5. Ocelot API网关的实现剖析

    在微软Tech Summit 2017 大会上和大家分享了一门课程<.NET Core 在腾讯财付通的企业级应用开发实践>,其中重点是基于ASP.NET Core打造可扩展的高性能企业级A ...

  6. Asp.Net Core API网关Ocelot

    首先,让我们简单了解下什么是API网关? API网关是一个服务器,是系统的唯一入口.从面向对象设计的角度看,它与外观模式类似.API网关封装了系统内部架构,为每个客户端提供一个定制的API.它可能还具 ...

  7. Ocelot网关

    Ocelot是一个.net core框架下的网关的开源项目,下图是官方给出的基础实现图,即把后台的多个服务统一到网关处,前端应用:桌面端,web端,app端都只用访问网关即可. Ocelot的实现原理 ...

  8. Ocelot 集成Butterfly 实现分布式跟踪

    微服务,通常都是用复杂的.大规模分布式集群来实现的.微服务构建在不同的软件模块上,这些软件模块,有可能是由不同的团队开发.可能使用不同的编程语言来实现.有可能布在了几千台服务器,横跨多个不同的数据中心 ...

  9. 给Ocelot做一个Docker 镜像

    写在前面 在微服务架构中,ApiGateway起到了承前启后,不仅可以根据客户端进行分类,也可以根据功能业务进行分类,而且对于服务调用服务也起到了很好的接口作用.目前在各个云端中,基本上都提供了Api ...

  10. .NET Core开源API网关 – Ocelot中文文档

    Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...

随机推荐

  1. Java -- XStreamAlias 处理节点中的属性和值

    XStreamAlias 可以把objec和xml相互转换,但是有时候节点带有属性和值就需要特殊处理下: <?xml version="1.0" encoding=" ...

  2. scrollview嵌套recyclerview显示不全现象

    只需在recyclerview的外层加入一个父布局就好了 <RelativeLayout android:layout_width="match_parent" androi ...

  3. spring 之 类型转换 2

    spring内置的转换器 在spring xml 文件中,配置属性的时候, 不管实际是 list 还是map ,还是Date, 或者原生的java 类型, 我们只能配置xml 给它们. 那么 spri ...

  4. Idea中类上有叉的解决方法

    idea中类的头上出现X解决办法 ctrl+alt+s 在弹出的菜单上选择Compiler下的Excludes 右边会有 移除掉,点击ok, 重启idea就可以了

  5. autolayout后获取frame

    autolayout设置完layout立即用frame拿对应的值可能拿不准,因为autolayout设置完布局后布局引擎并不会马上去更改布局,而是将布局标记为待更新,此时可以用的方法有两种,一是延时0 ...

  6. threading.local()源码分析

    前段时间写了个多线程的程序,了解到Python中有个与众不同的thread.local()方法,可以创建一个全局对象,各个线程可以用这个全局对象保存各自的局部变量,而在使用时不受其他线程的影响.于是抽 ...

  7. Centos7搭建OpenVPN服务器

    Windows下同时连接多个VPN的话,需要以管理员身份运行 C:\Program Files\TAP-Windows\bin\addtap.bat 添加虚拟网络适配器 --------------- ...

  8. push() 方法将一个或多个元素添加到数组的末尾,并返回新数组的长度

    var numbers = [1, 2, 3]; numbers.push(4); console.log(numbers); // [1, 2, 3, 4] numbers.push(5, 6, 7 ...

  9. 解决no module named ipykernel_launcher

    解决no module named ipykernel_launcher 最近开hydrogen的时候,提示no module named ipykernel_launcher. 记得以前解决过这个问 ...

  10. 微商城分享 包括app分享 微信分享

    <template> <div class="spr"> <img src="../../assets/images/activity/sh ...