Consul+Ocelot搭建微服务实践--初探路由

Ocelot介绍

公司打算将以前的系统利用core进行重构并且向微服务进军,趁着目前手上任务不是很多也就慢慢进军了微服务,本篇章是我对微服务实践的开篇,后续会逐步介绍到Ocelot、Consul、IdentityServer相关知识。
Ocelot是一个基于.net core的开源webapi 服务网关项目,目前已经集成了IdentityServer认证。Ocelot本质上是一堆中间件的集合,当HttpRequest请求到达后由一堆中间件进行处理,处理完毕,请求根据配置转发给下游服务。然后接受下游服务的返回信息在转发给客户端,这样就避免了在调用的时候记录复杂的服务ip与端口,只需要使用Ocelot的ip/端口即可。详细信息请参考 官方文档:https://ocelot.readthedocs.io

安装Ocelot

在管理nuget程序中搜索Ocelot进行安装。非常简单就不多说。

配置路由

{
  "ReRoutes": [
    // API:Service1
    {      
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "5011"
        }
      ],
      "UpstreamPathTemplate": "/Service1/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ]    
    },
    // API:Service2
    {     
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "5021"
        }
      ],
      "UpstreamPathTemplate": "/Service2/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ]     
    }
  ]
}

相关配置进行介绍:

  • DownstreamPathTemplate 下游路由模板
  • DownstreamScheme 下游路由请求方式
  • DownstreamHostAndPorts 下游Host/Port 注意是一个数组,可以配置多个。(后面介绍负载均衡的时候会用到)
  • UpstreamPathTemplate 上游路由模板
  • UpstreamHttpMethod 上游请求方法

使用Ocelot

1、 添加配置文件

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureAppConfiguration(bulid=>
                {
                    bulid.AddJsonFile("eg.configuration.json", false, true);
                });

2、 添加Ocelot中间件

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    #region 添加ocelot
    services.AddOcelot(Configuration);          
    #endregion
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }
     else
     {
         // 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.UseMvc();
     //使用Ocelot中间件
     app.UseOcelot().Wait();
 }

添加服务进行测试

这里只是对路由进行简单的测试快速的建立两个Webapi。如何建立就不用多废话了。

  1. 建立Service1(端口:5011)
    这里说明一下,建立的服务只是进行简单的测试将启动配置删除不必要项,并修改"launchBrowser": false
    例如:

    {
      "profiles": {
        "Study.Microservices.Service1": {
          "commandName": "Project",
          "launchBrowser": false,
          "launchUrl": "api/values",
          "applicationUrl": "https://localhost:5011",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    
  2. 建立Service2(端口:5021)
    修改启动方式同servce1一样。

进行模拟测试

通过请求https://localhost:1001/Service1/values地址成功访问。
请求Service1

全局路由

{
   "DownstreamPathTemplate": "/{url}",
   "DownstreamScheme": "https",
   "DownstreamHostAndPorts": [
     {
       "Host": "localhost",
       "Port": "5011"
     }
   ],
   "UpstreamPathTemplate": "/{url}",
   "UpstreamHttpMethod": [ "Get", "Post" ]
}

直接将模板配置成 /{url} 进行全局路由,但是这是访问级别最低的,一旦设置了其他路由模板就不会使用此模板。可以使用 Priority来设置优先级。
例如:

"DownstreamPathTemplate": "/api/{url}"
"Priority":0,
"DownstreamPathTemplate": "/api/values"
"Priority":1,

访问 /api/values 就会匹配 “DownstreamPathTemplate”: “/api/values” 路由模板。

字符串占位符

{
   "DownstreamPathTemplate": "/api/put/{id}?value={value}",
   "DownstreamScheme": "https",
   "DownstreamHostAndPorts": [
     {
       "Host": "localhost",
       "Port": "5011"
     }
   ],
   "UpstreamPathTemplate": "/api/put?id={id}/{value}",
   "UpstreamHttpMethod": [ "Get", "Post" ]
}

下游上游模板的字符串模板进行匹配,那么上游的 value参数和id就会传递到下游的valueid中去。

总结

这里主要总结下我个人的想法,以前读书的时候我不管数数学还是英语都不会准备笔记本去记录笔记,知道现在慢慢通过博客来记录自己所学的东西的时候才真正理解到了好记性不如烂笔头。自己进行写博客来记录自己所学的知识后也算是对所学的知识做一个总结,从头到尾梳理一遍,但写博客还真花时间,在这里我感谢园子中那些总结的很好的园友,使他们花了很多的时间才能让我们有了一个通道去学习,感谢你们
不知不觉的现在就喜欢慢慢记录自己所学的点点滴滴,感觉这样也算是给自己的一个交代,也希望得到园友们的支持

原文地址:https://www.cnblogs.com/cqxhl/p/12993298.html