从入门到入土-微服务治理(Ocelot)-1

Ocelot 是干啥的啊?如果是面试的话,面试官肯定要问,请说下你对ocelot的理解,或者是问请简单的聊下ocelot;
ocelot 下载: https://gitee.com/mirrors/ocelot-gateway
1、Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,强大的很啊。。。哈哈反正如果是面试的时候就吹呗,但是不要浮夸,ocelot
包括了:路由功能、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly Tracing集成。这些功能只都只需要简单的配置即可 ;
2、简单的来说Ocelot是asp.net core middleware组成的一个管道。当它拿到请求之后会用一个request builder来构造一个HttpRequestMessage发到下游的服务器,等下游的服务返回response之后再由一个middleware将它返回的HttpResponseMessage映射到HttpResponse上,相当于反向代理一下;

如果你有很多api你完全可以使用这个网关作为唯一的入口,一个域名多个URL即可,当然后流量多一定要用集群,因为这是唯一的入口啊所有的请求都从这过,如果是大型项目的话针对不同的微服务做一个单独的集群,这也是和nginx的区别做多个集群做服务治理;
这个图是抄来的

他与Nginx的对比:
【Ocelot和Nginx的共同点】
反向代理,地址转发
负载均衡

【不同点】
Nginx由一个集群组成。主要优点高性能,高吞吐,安全
Ocelot 由多个集群组成。本质是为了做路由(多组地址转发),实现服务治理

下面开始实现一个小集群。
三个服务
1、先创建3个netcore web站点或者是3个api 里面的内容改下 ,我这就是app1/2/3
三个都改下
2、创建好之后在启动下。 分别到3个服务的跟目录cmd 然后用命令启动 :

dotnet run --urls="http://*:5051" /port=5051  
dotnet run --urls="http://*:5052" /port=5052  
dotnet run --urls="http://*:5053" /port=5053

3、开始创建网关 :
创建一个api的项目
网关的项目
4、然后在Startup里把管道啥的都干掉,Nuget添加Ocelot引用
添加引用

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Ocelot.Provider.Polly;

namespace KH.Fz.GetWay
{
    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(); //.AddConsul().AddPolly();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseOcelot().Wait();
        }
    }
}

5、然后添加配置文件

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, builder) => {
                    builder.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath).AddJsonFile("Ocelot.json");
                })
                .ConfigureWebHostDefaults(webBuilder =>
                
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

6、配置文件的配置: Routes 高版本和低版本的写法不一样 老版本加Re 要不不显示 这有坑:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5053
        }

      ],
      "UpstreamPathTemplate": "/v3/{url}",
      "UpstreamHttpMethod": [
        "Get",
        "Post"
      ],
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //轮询 , 还有一种是空闲分配 https://www.freesion.com/article/53741280560/
      }
    },
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5052
        }

      ],
      "UpstreamPathTemplate": "/v2/{url}",
      "UpstreamHttpMethod": [
        "Get",
        "Post"
      ],
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //轮询 , 还有一种是空闲分配 https://www.freesion.com/article/53741280560/
      }
    },
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5051
        }

      ],
      "UpstreamPathTemplate": "/v1/{url}",
      "UpstreamHttpMethod": [
        "Get",
        "Post"
      ],
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //轮询 , 还有一种是空闲分配 
      }
    }
  ], 
  // 全局配置,此节点的配置会覆盖Routes,可以在这里设置一些通用的配置
  "GlobalConfiguration": {
    "ReRouteIsCaseSensitive": false // 路由是否区分大小写
  }
}
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5053
        }{
          "Host": "localhost",
          "Port": 5052
        }{
          "Host": "localhost",
          "Port": 5051
        }],
      "UpstreamPathTemplate": "/v3/{url}",
      "UpstreamHttpMethod": [
        "Get",
        "Post"
      ],
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //轮询 , 还有一种是空闲分配 https://www.freesion.com/article/53741280560/
      }
    }
    }

7、配置文件一般一看就知道咋回事了;
UpstreamPathTemplate 上行,也就是请求路径,{url}是请求路由
UpstreamHttpMethod 支持的请求方式
DownstreamPathTemplate 请求Kestrel的地址,也就是说请求你的真实项目的地址
DownstreamScheme http还是https
DownstreamHostAndPorts 这个就是host和port了。
LoadBalancerOptions:有这三个机制 一般用第一个或者第二个;
RoundRobin:轮询机制,循环找到可以用的服务
LeastConnection:最少连接数,跟踪发现现在有最少请求或处理的可用服务
NoLoadBalancer:不使用负载均衡,直接访问config配置或者服务发现的第一个可用服务

大坑:如果consul注册发现中心和web节点在通一台机器上的话 他找的不是ip而是 hostname+port模式所以经常会报错: 非法的主机名 或者主机名不对。 这是个大坑请大家注意

上面是两种配置方法,大家注意啊。如果服务一样而且负载的话就用第2种就行了。
这个整完了 就可以跑了

下一篇请点击:
微服务治理(Ocelot)- 2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值