我正在研究微服务架构,我想聚合来自两个微服务的数据。
例如,Frontend 调用 API Gateway,API Gateway 调用两个微服务 Customer 和 Order 微服务。客户微服务返回客户详细信息,订单微服务返回客户所有订购的产品。
这是使用 Ocelot 或 Azure API 管理从两个微服务聚合后 API 网关返回的格式。
格式 1
{
"Customers":[
{
"customerId":1001,
"customerName":"Tom"
},
{
"customerId":1002,
"customerName":"Jerry"
}
],
"Orders":[
{
"CustomerId":1001,
"Orders":[
{
"ProductId":"PRO1",
"ProductName":"Books"
},
{
"ProductId":"PRO2",
"ProductName":"Pens"
}
]
},
{
"CustomerId":1002,
"Orders":[
{
"ProductId":"PRO3",
"ProductName":"Pencils"
},
{
"ProductId":"PRO4",
"ProductName":"Toys"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想要的格式是格式 2。
格式 2
{
"OrderDetails":[
{
"customerId":1001,
"customerName":"Tom",
"Orders":[
{
"ProductId":"PRO1",
"ProductName":"Books"
},
{
"ProductId":"PRO2",
"ProductName":"Pens"
}
] …
Run Code Online (Sandbox Code Playgroud) azure-api-management microservices aws-api-gateway api-gateway ocelot
在将传入的 http 请求转发到下游路径时,我正面临此警告/错误。
Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware:警告:requestId:80000025-0004-fd00-b63f-84710c7967bb,previousRequestId:没有先前的请求ID,消息:DownstreamRouteFinderMiddleware 设置管道错误。IDownstreamRouteFinder 返回错误代码:UnableToFindDownstreamRouteError 消息:无法匹配上游路径的路由配置:/getDepartment,动词:GET。
程序.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((host, config) =>
{
config.AddJsonFile("ocelot.json");
})
.UseStartup<Startup>();
}
Run Code Online (Sandbox Code Playgroud)
启动文件
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public …
Run Code Online (Sandbox Code Playgroud) 我有一个基于 .net core 的微服务架构。我选择ocelot作为api网关。我的前端应用程序基于 vue js 并托管在 nginx 容器上。在今天的讨论中,我了解到nginx已经可以用作网关。有人建议“你应该使用nginx作为网关,因为你已经用于服务前端,nginx也可以部署为网关”我在互联网上搜索到比较这两个网关(我知道 nginx 的主要目的不是网关),但找不到任何关于它们的优缺点的信息,例如性能、可扩展性可用性等......
使用这两种技术的人可以与我分享关于我应该选择哪一种技术的信息吗?
我有一个基于 .NET core 的微服务架构,并且有几个微服务。我们选择 ocelot 作为 API 网关来实现路由、聚合和安全。在我们的架构讨论中,有人提到 Envoy Api Gateway 也是一个不错的选择,但找不到 Ocelot 和 Envoy 之间的任何差异,它的优缺点包括性能、可扩展性、社区支持等......
任何人都可以帮助分享一些关于哪一个是用于 .NET Core 应用程序的最佳选择的见解吗?
我正在尝试使用 Ocelot 在 ASP.Net API 网关中获得 JWT 不记名身份验证以与多个机构/发行者合作。一个颁发者是 Auth0,另一个是基于 IdentityServer4 的内部认证服务器;我们正在尝试从 Auth0 迁移,但仍有依赖它的外部客户端,因此我们希望支持两者,直到所有内容都经过全面测试以进行切换。
根据this MSDN blog post,应该可以通过设置TokenValidationParameters.ValidIssuers
而不是使用多个权限JwtBearerOptions.Authority
。但是,无论TokenValidationParameters.ValidIssuers
.
有谁知道如何让这个工作?这就是我设置身份验证的方式。它仅在注释行未注释时才有效(并且仅适用于由该单一机构颁发的令牌)。我期待 Ocelot 或 ASP.Net Core 从发行服务器获取密钥;两者都通过 .well-known/openid-configuration 提供 JWK,它与 ASP.Net Core 中间件一起使用。
public static void AddJwtBearerAuthentication(this IServiceCollection services, IConfiguration configuration)
{
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
//options.Authority = configuration["Jwt:Authority"];
options.Audience = configuration["Jwt:Audience"];
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateIssuerSigningKey = true,
ValidateAudience = …
Run Code Online (Sandbox Code Playgroud) 我是API网关的新手并且有一个理解的问题.我也尝试在端点后面放置一系列(微)服务.
为此,我已经设置了一个ASP.NET核心应用程序并添加了包ThreeMammals Ocelot.在文档的帮助下,我已经配置了上游和下游.到现在为止还挺好.
客户端向http:// mygateway:4242/s1 / {api} 发出请求,例如,按预期从Service1获取JSON或XML响应.
对于同样的行为的http:// mygateway:4242/S2 / {} API也与预期的结果!
我的理解问题是Service3.当我向http:// mygateway/s3 /发送请求时,我将index.html作为响应.
index.html本身需要通过link-tag的CSS-File'xyz.css'并强制客户端加载文件.
<head>
<link rel="stylesheet" type="text/css" href="xyz.css">
</head>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,客户端发送到"mygateway"的请求URL是http:// mygateway:4242/xyz.css而不是 http:// mygateway:4242/s3 /xyz.css,因此respone是404未找到,因为"mygateway"对"xyz.css"一无所知
如何修复此路由(?)问题?
是否有可能用ocelot中间件解决这个问题?或者我是否需要使用SinglePageApplication(SPA)的服务(Service3)的其他内容?
也许将SPA置于网关之后根本不可能或没有错?我希望你能给我一些提示,以便访问网关后面的SPA或MVC网站.
谢谢iBot
UPATE:附上 index.html的代码.我认为这是直截了当的.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
<base href="/" />
<link rel="stylesheet" type="text/css" href="dist/xyz.css">
</head>
<body>
<div id="appContainer"></div>
<script src="dist/xyz.js" asp-append-version="true"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我使用ocelot作为我的微服务的API网关,并使用IdentityServer4进行身份验证。在ocelot配置文件中,我添加了“ AuthenticationOptions”并设置了api密钥。在启动中,添加身份服务器。在身份服务器中,我使用标头中的值来动态构建连接字符串。当我发送获取令牌的请求时,可以在身份服务中访问标头。但是当我发送带有令牌的下一个请求时,原始标头不可用。在身份服务中只能看到“主机”标头。
在将请求路由到身份服务器时,是否可以保留原始标头?
Startup.cs(添加身份服务器)
services
.AddAuthentication()
.AddIdentityServerAuthentication("APIParts", options =>
{
options.Authority = "http://localhost:60168";
options.RequireHttpsMetadata = false;
options.ApiName = "Parts";
options.SupportedTokens = SupportedTokens.Both;
});
Run Code Online (Sandbox Code Playgroud)
ocelot.json
ReRoutes": [
{
"DownstreamPathTemplate": "/connect/token",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 60168
}
],
"UpstreamPathTemplate": "/token",
"UpstreamHttpMethod": [ "Post" ]
},
{
"DownstreamPathTemplate": "/api/Parts/Inventory",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 65241
}
],
"UpstreamPathTemplate": "/api/Parts/Inventory",
"AuthenticationOptions": {
"AuthenticationProviderKey": "APIParts",
"AllowedScopes": []
}
}]
Run Code Online (Sandbox Code Playgroud) 根据文档,我尝试合并我的配置文件,以便它们更具可读性。然而,生成的 ocelot.json 文件并不像预期的那样。我的文件夹结构如下:
下面是这个的文本表示:
.
??? Ocelot route configs
??? ocelot.pokemon.json
??? ocelot.tweet.json
??? ocelot.weather.json
Run Code Online (Sandbox Code Playgroud)
ocelot.pokemon.json 文件如下所示(其他类似):
{
"Routes": [
{
"DownstreamPathTemplate": "/api/v2/pokemon",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "pokeapi.co",
"Port": 443
}
],
"UpstreamPathTemplate": "/api/pokemon",
"UpstreamHttpMethod": [ "GET" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "MyTestKey",
"AllowedScopes": []
}
},
{
"DownstreamPathTemplate": "/api/v2/pokemon/ditto",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "pokeapi.co",
"Port": 443
}
],
"UpstreamPathTemplate": "/api/pokemon/ditto",
"UpstreamHttpMethod": [ "GET" ]
}
]
}
Run Code Online (Sandbox Code Playgroud)
生成的 ocelot.json 文件如下所示:
{
"Routes": [ …
Run Code Online (Sandbox Code Playgroud) 我在我的新项目中实现了 ocelot,我在一个点上与 ocelot 创建了我的服务集成,但是当我尝试在我的 api 网关中发布、放置、路径或删除资源时,浏览器会向我显示消息
无法加载http://localhost:8080/api/prospects:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问来源“ http://localhost:8888 ”。响应的 HTTP 状态代码为 404。
我尝试在网关安装和实现包 Microsoft.AspNetCore.Cors 中配置 cors
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddOcelot(Configuration)
.AddCacheManager(x =>
{
x.WithMicrosoftLogging(log =>
{
log.AddConsole(Microsoft.Extensions.Logging.LogLevel.Debug);
})
.WithDictionaryHandle();
}); ;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
app.UseCors(
options => options.WithOrigins("http://localhost:8888").AllowAnyMethod()
);
app.UseOcelot().Wait();
}
Run Code Online (Sandbox Code Playgroud) .Net 6 已删除启动类,我无法找到如何在新的 .Net 6 结构中配置 Ocelot。我找到了两种方法
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddOcelot()// 1.ocelot.json goes where?
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot(); // 2.what is the use of this
Run Code Online (Sandbox Code Playgroud)
请告诉我
ocelot ×10
.net-core ×4
api-gateway ×4
asp.net-core ×3
c# ×3
.net-6.0 ×1
asp.net ×1
bearer-token ×1
envoyproxy ×1
jwt ×1
nginx ×1