(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)

?
1.首先新建三个.NET Core 3.1下的API项目
ApiGetWay:将作为网关使用,可以将不需要用到的Controller文件夹和实体类删掉
APIServiceOne:服务一,将做api使用
APIServiceTwo:服务二,做api使用
(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)
文章图片

2.配置跨域
在三个个项目的Starup.cs中添加跨域配置,每一个都要加
public void ConfigureServices(IServiceCollection services)

{ services.AddControllers(); //配置跨域处理,允许所有来源: services.AddCors(options => { options.AddPolicy("all", builder => { builder.AllowAnyOrigin() //允许任何来源的主机访问 .AllowAnyMethod() .AllowAnyHeader(); }); }); }// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #region 跨域 app.UseCors("all"); #endregion app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }

3.配置网关
1.在作为网关使用的项目中,Nuget中下载ocelot15.06版本的包,版本太新可能不稳定
(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)
文章图片

2.然后给这个项目加一些配置,在根目录新建一个configuration.json文件
(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)
文章图片

删掉两个大括号,复制粘贴以下代码
{
"ReRoutes": [
//{ //"UpstreamPathTemplate": "/api/{controller}/{action}", //请求路径模板 //"UpstreamHttpMethod": [ "Get", "POST" ], //请求方法数组 //"DownstreamPathTemplate": "/api/{controller}/{action}", //下游请求地址模板 //"DownstreamScheme": "https", //请求协议,目前应该是支持http和https //"DownstreamHostAndPorts": [ //下游地址和端口 //{ //"host": "localhost", //"port": 5003 //}, //{ //"host": "localhost", //"port": 5001 //} //], //"LoadBalancerOptions": { //负载均衡 RoundRobin(轮询)/LeastConnection(最少连接数)/CookieStickySessions(相同的Sessions或Cookie发往同一个地址)/NoLoadBalancer(不使用负载) //"Type": "RoundRobin" //} //}, { //APIServiceOne服务 "DownstreamPathTemplate": "/api/{Controller}/{action}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5196 } ], "UpstreamPathTemplate": "/One/{Controller}/{action}", "UpstreamHttpMethod": [ "GET", "POST" ] }, { //APIServiceTwo服务 "DownstreamPathTemplate": "/api/{controller}/{action}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5197 } ], "UpstreamPathTemplate": "/Two/{controller}/{action}", "UpstreamHttpMethod": [ "GET", "POST" ] }

]
}
注意配置中,服务一和服务二的端口分别是5196和5197,所以需要对另外两个项目进行修改,修改ApiServiceOne和ApiServiceTwo项目中的启动端口,在launchSettings.json中
(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)
文章图片

在三个项目的配置中删除了其他不必要的配置,只留下启动的配置就行了,依次配置好服务端口,然后可以配置网关项目的端口,可以任意配置,这里我就给了5195.
然后需要根据configuration.json文件中的下游路径的模板去修改两个服务的controller中的路由
3.在网关项目的Starup.cs中注册和使用Ocelot,添加以下代码即可,在添加的时候根据提示导入相应的命名空间即可
public void ConfigureServices(IServiceCollection services)
{services.AddControllers(); services.AddOcelot(new ConfigurationBuilder().AddJsonFile("configuration.json", true, true).Build()); //配置跨域处理,允许所有来源: services.AddCors(options => { options.AddPolicy("all", builder => { builder.AllowAnyOrigin() //允许任何来源的主机访问 .AllowAnyMethod() .AllowAnyHeader(); }); }); }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #region 跨域 app.UseCors("all"); #endregion app.UseRouting(); app.UseOcelot(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }

4.postman测试
先把三个项目全部启动
【(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)】(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)
文章图片

打开postman测试,注意看请求的是网关的地址端口,5195,而我之前的步骤是把网关的controller给删了的,所以测试成功,网关成功的将请求转发到了服务一,当然把地址的One换成Two也没问题,那就会转发到服务二,可自行写一个测试接口。
(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)
文章图片

我分别在两个服务中加了一个测试action,再测试
(一)、手把手教你配置.NET|(一)、手把手教你配置.NET Core 微服务,前后端分离项目——跨域和网关(Ocelot)
文章图片

好了,测试证明没有问题,文章中有涉及到不懂的知识,就需要各位自己百度了,这里就不介绍了。
?原文链接:原文链接

    推荐阅读