.netcore的微服务学习(二)--网关(gateway)之Ocelot学习
一,引用ocelot,本文测试16版本有BUG,只好使用15.0.7
二,startup的配置,很简单,就是注册和添加管道
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Ocelot.DependencyInjection; using Ocelot.Middleware; namespace OcelotDemo { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOcelot(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ///使用coelot app.UseOcelot(); } } }
三,写配置文件,5001是我们控制台启动的端口,这个注意不要映射错
{ "ReRoutes": [ { "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量 "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 //服务端口 } //可以多个,自行负载均衡 ], "UpstreamPathTemplate": "/OcelotDemo/{url}", //网关地址--url变量 //冲突的还可以加权重Priority "UpstreamHttpMethod": [ "Get", "Post" ] } ] }
四,启动项目
dotnet OcelotDemo.dll urls="http://*:5003" --ip="127.0.0.1" --port=5003
五,访问http://localhost:5003/OcelotDemo/User/Get,这个可以拿到http://localhost:5001/api/User/Get地址的值,由于网关地址映射
六,多实例的配置
//*****************************多地址多实例,提供多种路由,可是不是集群******************************** { "ReRoutes": [ { "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量 "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 //服务端口 } //可以多个,自行负载均衡 ], "UpstreamPathTemplate": "/OcelotDemo5001/{url}", //网关地址--url变量 //冲突的还可以加权重Priority "UpstreamHttpMethod": [ "Get", "Post" ] }, { "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量 "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 //服务端口 } ], "UpstreamPathTemplate": "/OcelotDemo5002/{url}", //网关地址--url变量 "UpstreamHttpMethod": [ "Get", "Post" ] } ] }
访问的方法是http://localhost:52830/OcelotDemo5001/User/get和http://localhost:52830/OcelotDemo5002/User/get,所以需要访问两个地址,这个不是集群
七,单服务的负载均衡配置,如下
//*****************************单地址多实例负载均衡******************************** { "ReRoutes": [ { "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量 "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 //服务端口 } //可以多个,自行负载均衡 , { "Host": "localhost", "Port": 5002 //服务端口 } ], "UpstreamPathTemplate": "/OcelotDemo/{url}", //网关地址--url变量 //冲突的还可以加权重Priority "UpstreamHttpMethod": [ "Get", "Post" ], "LoadBalancerOptions": { "Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡 三种模式 } } ] }
启动网关dotnet OcelotDemo.dll urls="http://*:5003" --ip="127.0.0.1" --port=5003,我们可以使用同一个地址访问,http://localhost:5003/OcelotDemo/User/Get
八,UserController的写法请求的get接口
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; namespace ConsulTestDemo.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class UserController : ControllerBase { public IConfiguration _configuration; public UserController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public string Get() { return int.Parse(_configuration["port"]).ToString() + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //命令行参数必须传入 } } }