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

2、Startup.cs文件代码调整

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Ocelot.DependencyInjection;
using Ocelot.Middleware; namespace ApiGateway
{
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
//通过nuget引入如下包
//Microsoft.Extensions.Options.ConfigurationExtensions
//Microsoft.Extensions.Configuration.Json var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("configuration.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json"); Configuration = builder.Build();
} public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddOcelot(Configuration);
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
} app.UseOcelot();
app.UseHttpsRedirection();
app.UseMvc();
}
}
}

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

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/order", /*下游路径模板*/
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 9001
}
],
"UpstreamPathTemplate": "/order", /*上游路径模板*/
"UpstreamHttpMethod": [ "Get" ]
}, {
"DownstreamPathTemplate": "/api/product",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 9002
}
],
"UpstreamPathTemplate": "/product",
"UpstreamHttpMethod": [ "Get" ]
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath" : "administration"
}
}

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. leetcode101

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  2. C# 封装SqlHelper

    在项目配置文件中添加数据库连接字符串 <connectionStrings> <add connectionString="Data Source=主机;Initial C ...

  3. PL2303 USB转串口 com

    PL2303 USB转串口 com PL-2303 XA/HXA chip http://www.prolific.com.tw/US/ShowProduct.aspx?p_id=225&pc ...

  4. 如何在js中使用递归

    很久没写博客了... 内容后补

  5. 2018-2019-2 20175213实验三《敏捷开发与XP实践》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:吕正宏 学号:20175213 指导教师:娄嘉鹏 实验日期:2019年4月29日 实验时间:13:45 - 21:00 实验序号:实验 ...

  6. html网页练习豆瓣网

    html </head> <body>     <!-- 头部 -->     <header class="header1">   ...

  7. tensorflow 升级后报错:ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory

    我的tensorflow之前的版本是1.2的所以支持cudnn5,但是tensorflow1.3及以上就是支持cudnn6. 查看: /usr/local/cuda/lib64$ ls  libcud ...

  8. Nginx搭建

    Nginx nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件. nginx因具有高并发(特别是静态资源),占用系统资源少等特性,且功能丰富而逐渐流行起来. nginx不但是一个优秀 ...

  9. 纯css loading动效

    .loading {margin: 100px;     width: 3px; height:3px;     border-radius: 100%;                      / ...

  10. 36 【kubernetes】coredns

    CoreDNS是k8s中,master节点和从节点及其pods之间通信的接口. 但是上个博客中遇到一个问题dns一直处于crush的状态. https://www.cnblogs.com/helww/ ...