首页 > 编程语言 >asp.net core 微服务网关示例 ocelot gateway Demo

asp.net core 微服务网关示例 ocelot gateway Demo

时间:2022-12-14 17:13:09浏览次数:64  
标签:core 网关 asp 示例 ocelot net todoitems localhost

ocelot asp.net core 微服务 gateway介绍
https://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html

 

1. 新建asp.net core webapi空项目 AProject, nuget引用ocelot插件

2.  新建asp.net core webapi示例项目BProject,并实现todoitemscontroller get方法,http://localhost:22/api/todoitems

3. 在AProject项目,新建ocelot.json

{
  "Routes": [
    {
      //下游请求
      "DownstreamPathTemplate": "/api/todoitems/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 22
        }
      ],
      //上游请求
      "UpstreamPathTemplate": "/todoitems/",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ],
//Gateway 的地址 "GlobalConfiguration": { "BaseUrl": "http://localhost:5211" } }

在AProject项目,Program.cs 文件里 写以下代码

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

new WebHostBuilder()
           .UseKestrel()
           .UseContentRoot(Directory.GetCurrentDirectory())
           .ConfigureAppConfiguration((hostingContext, config) =>
           {
               config
                   .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                   .AddJsonFile("appsettings.json", true, true)
                   .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                   .AddJsonFile("ocelot.json")
                   .AddEnvironmentVariables();
           })
           .ConfigureServices(s => {
               s.AddOcelot();
           })
           .ConfigureLogging((hostingContext, logging) =>
           {
               //add your logging
           })
           .UseIISIntegration()
           .Configure(app =>
           {
               app.UseOcelot().Wait();
           })
           .Build()
           .Run();

启动AProject、BProject项目, 此时访问:

http://localhost:5211/todoitems/   会跳到http://localhost:22/api/todoitems/ 

标签:core,网关,asp,示例,ocelot,net,todoitems,localhost
From: https://www.cnblogs.com/haoliansheng/p/16982676.html

相关文章

  • 第八节:基于Core6.0中间件实现SignalR的安全校验 以及 部署IIS注意的问题
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblog......
  • 邮件安全喜报!Coremail两大方案入选“2022年软件行业典型应用案例”
    12月5日,由中国软件行业协会联合中软国际有限公司、江苏省软件行业协会主办的2022中国程序员节在江苏南京圆满落幕,本次活动聚焦当前我国对数字技术创新及应用发展的现实需求,......
  • 积极拥抱.NET Core开源社区
    潘正磊在上海的TechSummit2018大会上给我们的.NETCore以及开源情况带来了最新信息。.NetCore开源后取得了更加快速的发展,目前越活跃用户高达400万人,每月新增开发者45......
  • BL110网关远程监测充电桩断网故障解决方案
    在碳中和等因素的影响下,燃油车逐步退出市场,新能源汽车逐步替代燃油车市场,获得了爆发性增长的市场空间。近年来,中国新能源汽车市场保持高速增长,新能源汽车当前的保有量、增......
  • Ocelot API网关的实现剖析
    在微软TechSummit2017大会上和大家分享了一门课程《.NETCore在腾讯财付通的企业级应用开发实践》,其中重点是基于ASP.NETCore打造可扩展的高性能企业级API网关,以开源的......
  • 搭建网关微服务实现接口统一访问
    我们现在搭建一个Zuul网关,实现在第9章创建的商品和订单两个微服务的接口通过网关统一访问。同样,先创建一个SpringBoot项目,命名为zuul,如图所示。  然后,在“Dependenc......
  • 网关介绍
    网关指的是一个网络连接到另一个网络的“关口”。在Internet里,网关是一种连接内部网与Internet上其他网络的中间设备,通俗来说,也叫作“路由器”。网关地址是能够理解成内部......
  • luabind-0.9.1在windows、linux下的使用详解及示例
    一.下载  1. 本篇博客使用的版本为luabind-0.9.1二.编译  1.luabind-0.9.1在window 三.示例代码下载:  1.windows下示例代码下载地址(环境是win7,VS2008,已......
  • centos7 redis5.0以后版本 集群部署示例
    简言1.redis5.0版本以前的集群部署是使用ruby脚本完成的,略为复杂2.本篇博客讲解redis5.0版本以后的集群部署,由于集成到了create_cluster中(位置:redis根目录/utils/create-c......
  • ubuntu16 redis5.0以后版本集群部署示例
    简言1.redis5.0版本以前的集群部署是使用ruby脚本完成的,略为复杂,具体示例见笔者的这篇博客,​​ubuntu16redis5.0以前版本集群部署示例_YZF_Kevin的博客2.本篇博客讲解red......