首页 > 其他分享 >使用ocelot 配置网关

使用ocelot 配置网关

时间:2024-02-06 10:02:37浏览次数:33  
标签:test1 网关 test2 https app 配置 api ocelot localhost

3.1 场景描述

建3个站点,2个微服务站点,1个网关

微服务1:https://localhost:7227/

微服务2:https://localhost:7019/

网关:https://localhost:7055/

在浏览器里访问 https://localhost:7227/api/Product/test1 会输出test1

在浏览器里访问https://localhost:7019/api/order/test2 会输出 test2

我要达到的效果是:

访问https://localhost:7055/Product/test1 调用https://localhost:7227/api/Product/test1 会输出test1

访问https://localhost:7055/order/test2 调用 https://localhost:7019/api/order/test2 会输出 test2

3.2 新建号这3个站点,在gateway1 这个站点通过nuget 添加ocelot

 3.3 配置

3.3.1 在 appsettings.json 配置网关和微服务之间的映射

{

"Logging": {

"LogLevel": {

"Default": "Information",

"Microsoft.AspNetCore": "Warning"

}

},

"AllowedHosts": "*",

"Routes": [

{

"DownstreamPathTemplate": "/api/Product/{everything}",

"DownstreamScheme": "https",

"DownstreamHostAndPorts": [

{

"Host": "localhost",

"Port": 7227

}

],

"UpstreamPathTemplate": "/product/{everything}",

"UpstreamHttpMethod": [ "GET" ]

},

{

"DownstreamPathTemplate": "/api/Order/{everything}",

"DownstreamScheme": "https",

"DownstreamHostAndPorts": [

{

"Host": "localhost",

"Port": 7019

}

],

"UpstreamPathTemplate": "/order/{everything}",

"UpstreamHttpMethod": [ "GET" ]

}

],

"GlobalConfiguration": {

"BaseUrl": "http://localhost:7055"

}

}

3.3.2 在 program 里配置服务

加两行代码

builder.Services.AddOcelot();

app.UseOcelot().Wait();

下面是完整的program代码

 

using Ocelot.DependencyInjection;

using Ocelot.Middleware;

 

var builder = WebApplication.CreateBuilder(args);

 

// Add services to the container.

builder.Services.AddRazorPages();

builder.Services.AddOcelot();

 

var app = builder.Build();

 

// Configure the HTTP request pipeline.

if (!app.Environment.IsDevelopment())

{

app.UseExceptionHandler("/Error");

// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.

app.UseHsts();

}

 

app.UseHttpsRedirection();

app.UseStaticFiles();

 

app.UseRouting();

 

app.UseAuthorization();

 

app.MapRazorPages();

app.UseOcelot().Wait();

app.Run();

 

3.4 测试效果

访问https://localhost:7055/Product/test1 调用https://localhost:7227/api/Product/test1 会输出test1

 

访问https://localhost:7055/order/test2 调用 https://localhost:7019/api/order/test2 会输出 test2

 

标签:test1,网关,test2,https,app,配置,api,ocelot,localhost
From: https://www.cnblogs.com/wangtiantian/p/18009195

相关文章

  • 什么是 Ocelot?
    Ocelot是一个用于构建微服务架构中API网关的开源框架,它充当了前端应用程序和后端微服务之间的入口点,处理请求路由、认证授权、流量管理、负载均衡、日志记录、安全性等任务。通过API网关,可以将多个微服务的API统一暴露给客户端,提供更简化和统一的接入方式。 Ocelot提供......
  • 什么是微服务的网关?
    假如有10个微服务,地址分部是:http://service1.abc.comhttp://service2.abc.comhttp://service3.abc.com................................................http://service10.abc.com网关:http://gatway.abc.com (在这个站点的appsetting.json里配置和微服务之间的映射)最后......
  • K8S前置配置
    安装基础软件0.不能联网处理重启网络服务servicenetworkrestart查看报错信息,如果正常就pingwww.baidu.com看是否通,如果通了不用管journalctl-xe0.将networkmanager服务停了systemctlstopNetworkManagersystemctldisableNetworkManager再次重启服务servic......
  • Nginx配置TCP/UDP流量转发
    #usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}stream{log_formatmain'$remote_addr[$tim......
  • Ubuntu 配置samba
     参考链接:[shared]comment=SharedFolderpath=/home/user/sharebrowseable=yesreadonly=noguestok=yescreatemask=0755directorymask=0755 关键点:1.配置samba用户和密码2.配置共享路径3.samb.conf中的内容按上面的填写就行。4.可以使用......
  • Profinet转CANopen主站网关与堡盟编码器通讯案例
    Profinet转CANopen主站网关与堡盟编码器通讯案例Profinet转CANopen主站网关(XD-COPNm20)为CPU与堡盟编码器的通讯提供了CANopen协议向Profinet协议转换互通的桥梁。CANopen是一种基于CAN总线的通讯协议,它被广泛应用于工业自动化领域,而Profinet是一种以太网协议,其优点是高速传输和......
  • PowerShell是一种跨平台的任务自动化解决方案,包括命令行shell、脚本语言和配置管理框
    PowerShell是一种跨平台的任务自动化解决方案,包括命令行shell、脚本语言和配置管理框架。PowerShell提供了丰富的命令(称为cmdlet),可以用来执行许多Windows管理任务,包括但不限于用户和组管理、文件系统操作、进程和服务管理等。下面列举一些常见的net命令以及它们在PowerShell中的对......
  • iis 配置后启动报不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种
     错误  配置后启动报不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny" 处理如图 C:\windows\system32\inetsrv\appcmdunlockconfig-section:system.webServer/handlersC:\windows\system3......
  • Springboot整合redis配置详解
    Springboot整合redis配置详解1.导入依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>2.编写properties或者yml配置#Redis本地服务器地址,注意要开启redis服务,即那个redis-s......
  • redis安装及常用配置
    目录一、引言1.1数据库压力过大1.2数据不同步1.3传统锁失效二、Redis介绍2.1NoSQL2.2NoSQL的类别2.2.1键值(Key-Value)存储数据库2.2.2列存储数据库2.2.3文档型数据库2.2.4图形(Graph)数据库2.3Redis是什么2.4Redis的应用场景2.5Redis优缺点2.5.1Redis优势2.5.2Redi......