(8)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Ocelot网关(Api GateWay)

说到如今现有微服务的几点不足:html

1) 对于在微服务体系中、和 Consul 通信的微服务来说,使用服务名便可访问。可是对于手 机、web 端等外部访问者仍然须要和 N 多服务器交互,须要记忆他们的服务器地址、端 口号等。一旦内部发生修改,很麻烦,并且有时候内部服务器是不但愿外界直接访问的。git

2) 各个业务系统的人没法自由的维护本身负责的服务器;github

3) 现有的微服务都是“我家大门常打开”,没有作权限校验。若是把权限校验代码写到每 个微服务上,那么开发工做量太大。web

4) 很难作限流、收费等。算法


ocelot 中文文档:https://blog.csdn.net/sD7O95O/article/details/79623654 json

资料:http://www.csharpkit.com/apigateway.html 后端

官网:https://github.com/ThreeMammals/Ocelot api

腾讯.Net 大队长“张善友”是项目主力开发人员之一。缓存

1、 Ocelot 基本配置

Ocelot 就是一个提供了请求路由、安全验证等功能的 API 网关微服务。 安全

建一个空的 asp.net core 项目。

Install-Package Ocelot

项目根目录下建立 configuration.json

ReRoutes 下就是多个路由规则

{
    "ReRoutes": [
        {
            "DownstreamPathTemplate": "/api/{url}",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 5001
                }
            ],
            "UpstreamPathTemplate": "/MsgService/{url}",
            "UpstreamHttpMethod": [
                "Get",
                "Post"
            ]
        },
        {
            "DownstreamPathTemplate": "/api/{url}",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 5003
                }
            ],
            "UpstreamPathTemplate": "/ProductService/{url}",
            "UpstreamHttpMethod": [
                "Get",
                "Post"
            ]
        }
    ]
}

 

Program.cs的CreateWebHostBuilder中

//在原基础上添加上这俩行
//只是监听这个端口 你本身配置也能够
.UseUrls("http://127.0.0.1:8888")
.ConfigureAppConfiguration((hostingContext, builder) => { builder.AddJsonFile("configuration.json", false, true); })

在ConfigureAppConfiguration 中AddJsonFile是解析json配置文件的方法。

为了确保直接在bin 下直接dotnet运行的时候能找到配置文件,因此要在vs中把配置文件的【复制到输出目录】设置为【如 果若是较新则复制】。

 

Startup.cs中 经过构造函数注入一个 private IConfiguration Configuration;

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseOcelot().Wait();//不要忘了写Wait
}

这样当访问http://127.0.0.1:8888/MsgService/Send?msg=aaa的时候就会访问 http://127.0.0.1:5002/api/email/Send?msg=aaa

configuration.json中UpstreamHttpMethod表示对什么样的请求类型作转发。

2、 Ocelot+Consul

上面的配置仍是把服务的ip地址写死了,Ocelot能够和Consul通信,经过服务名字来配置。

只要改配置文件便可

{
    "ReRoutes": [
        {
            "DownstreamPathTemplate": "/api/{url}",
            "DownstreamScheme": "http",
            "UpstreamPathTemplate": "/MsgService/{url}",
            "UpstreamHttpMethod": [
                "Get",
                "Post"
            ],
            "ServiceName": "MsgService", //服务名字
            "LoadBalancerOptions": {
                "Type": "RoundRobin" //指定一个负载均衡算法:  LeastConnection 最少的链接     RoundRobin 轮训
            },
            "UseServiceDiscovery": true //使用服务发现
        }
    ],
        "GlobalConfiguration": {  //全局配置
        "ServiceDiscoveryProvider": {  //链接这台Consul服务器
            "Host": "localhost",
                "Port": 8500
        }
    }
}

有多个服务就 ReRoutes 下面配置多组便可

 访问 http://localhost:8888/MsgService/SMS/Send_MI 便可,请求报文体

{phoneNum:"110",msg:"aaaaaaaaaaaaa"}。

 表示只要是/MsgService/开头的都会转给后端的服务名为" MsgService "的一台服务器,转发的路径是"/api/{url}"。

LoadBalancerOptions 中"LeastConnection"表示负载均衡算法是“选择当前最少链接数的服务器”,若是改成 RoundRobin 就是“轮询”。

ServiceDiscoveryProvider 是 Consul 服务器的配置。

Ocelot 由于是流量中枢,也是能够作集群的。

 

 (*) 也 支 持 Eureka 进 行 服 务 的 注 册 、 查 找

(http://ocelot.readthedocs.io/en/latest/features/servicediscovery.html),也支持访问 Service Fabric 中的服务(http://ocelot.readthedocs.io/en/latest/features/servicefabric.html)。

3、Ocelot 其余功能简单介绍

一、  限流:

文档:http://ocelot.readthedocs.io/en/latest/features/ratelimiting.html 须要和 Identity Server 一块儿使用,其余的限速是针对 clientId 限速,而不是针对 ip 限速。好比我调用微博的api开发了一个如鹏版新浪微博,个人 clientid 是 rpwb,而后限制了 1 秒钟只能调用 1000 次,那么全部用如鹏版微博这个 app 的全部用户加在一块儿,在一秒钟以内,不能累计超过 1000 次。目前开放式 api 的限流都是这个套路。

若是要作针对 ip 的限速等,要本身在 Ocelot 前面架设 Nginx 来实现。

二、  请求缓存

http://ocelot.readthedocs.io/en/latest/features/caching.html 只支持 get,只要 url 不变,就会缓存。

三、  QOS(熔断器)

http://ocelot.readthedocs.io/en/latest/features/qualityofservice.html

 

注:此文章是我看杨中科老师的.Net Core微服务第二版和.Net Core微服务第二版课件整理出来的

相关文章
相关标签/搜索