Bumblebee之负载、限流和故障处理实践

Bumblebee作为标准HTTP 1.1应用协议的网关,它能作为任何基于HTTP 1.1构建Webapi服务的前置网关。以下通过示例讲述如何用Bumblebee来制作一个asp.net core webapi的前置网关,并演示网关的一些基础功能如:负载,限流和故障迁移等相关基础功能。

网关定义

Bumblebee定义网关非常简便,只需要Nuget引用BeetleX.Bumblebee.然后定义Gateway类运行即可,大概代码如下:

            mGateway = new Bumblebee.Gateway();
            mGateway.HttpOptions(o => { o.LogToConsole = true; o.Port = 80; });
            //添中管理的服务,网关会对应用服务的可用状况进行监控
            mGateway.SetServer("http://192.168.2.18:8001");
            mGateway.SetServer("http://192.168.2.18:8002");
            mGateway.SetServer("http://192.168.2.18:8003");

            //default的是'*',匹配优先级最低
            mGateway.Routes.Default
               .AddServer("http://192.168.2.18:8001", 0, 0)
               .AddServer("http://192.168.2.18:8002", 0, 0)
               .AddServer("http://192.168.2.18:8003", 0, 0);
            //以上是手动代码的方式来构建网关,实际上可以通过配置'Gateway.json'来描述即可,具本查看https://github.com/IKende/Bumblebee
            mGateway.Open();

以上通过代码定义一个网关,网关主要有三台服务,并进行一个平均负载略。如果不想硬代码的情况可以通过编写'Gateway.json'配置文件来达到同样的效果:

{
  "Servers": [
    {
      "Uri": "http://192.168.2.18:8001/"
    },
    {
      "Uri": "http://192.168.2.18:8002/"
    },
    {
      "Uri": "http://192.168.2.18:8003/"
    }
  ],
  "Urls": [
    {
      "Url": "*",
      "HashPattern": null,
      "Servers": [
        {
          "Uri": "http://192.168.2.18:8001/"
        },
        {
          "Uri": "http://192.168.2.18:8002/"
        },
        {
          "Uri": "http://192.168.2.18:8003/"
        }
      ]
    }
  ]
}

以上是组件默认的配置文件,通过代码扩展的好处是可以根据自己的需要来制定存储方式。

Web服务

接下需要编写三个Asp.net core web api项目,分别部署配置到以上三个配置的地址中;为了方便测试服务,对应的方法是返回相应的服务地址IP端口

    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { $"{this.HttpContext.Connection.LocalIpAddress}${this.HttpContext.Connection.LocalPort}|{DateTime.Now}" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return $"{this.HttpContext.Connection.LocalIpAddress}${this.HttpContext.Connection.LocalPort}|{DateTime.Now}";
        }

        // POST api/values
        [HttpPost]
        public ActionResult<string> Post([FromBody] string value)
        {
            return $"{this.HttpContext.Connection.LocalIpAddress}${this.HttpContext.Connection.LocalPort}|{DateTime.Now}";
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public ActionResult<string> Put(int id, [FromBody] string value)
        {
            return $"{this.HttpContext.Connection.LocalIpAddress}${this.HttpContext.Connection.LocalPort}|{DateTime.Now}";
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public ActionResult<string> Delete(int id)
        {
            return $"{this.HttpContext.Connection.LocalIpAddress}${this.HttpContext.Connection.LocalPort}|{DateTime.Now}";
        }
    }

编写服务运行后会收到网关的状态请求信息,这个信息主要是网关用于检测服务的有效性,请求频率大概在1秒左右。

测试

为了方便测试示例网关集成了一个测试页面,通过页面请求网关可以看到响应情况;由于配置是三台服务平均负载请求,所以测试请求的情况会看到具体请求会被平均化到不同服务上。

调整权重

组件在添加服务的时候可以设置对应的重权值,以下把8001的权重设置10,其它两个设置成5看一下情况

  mGateway.Routes.Default
               .AddServer("http://192.168.2.18:8001", 10, 0)
               .AddServer("http://192.168.2.18:8002", 5, 0)
               .AddServer("http://192.168.2.18:8003", 5, 0);

零权重

组件支持零权重设置,当设置为零的情况下一般情况下是不参与负载的,为什么说一般情况下呢?其实即使是零的情况当其他服务不可用情况是会把负载落地它身上,以下简单的演示一下,把8003配置成零权重运行一段时间后把8001和8002关闭,后再运行8001看一下运行结果

   //default的是'*',匹配优先级最低
            mGateway.Routes.Default
               .AddServer("http://192.168.2.18:8001", 10, 0)
               .AddServer("http://192.168.2.18:8002", 10, 0)
               .AddServer("http://192.168.2.18:8003", 0, 0);

RPS限制

为了保障服务运行的稳定性,对请求限制是一种比较普遍的做法,组件支持针对不同URL不同服务进行一个RPS限制,用于确保服务可以可靠地运行。以下把三个服务的RPS都即制在100内

            mGateway.Routes.Default
               .AddServer("http://192.168.2.18:8001", 10, 100)
               .AddServer("http://192.168.2.18:8002", 10, 100)
               .AddServer("http://192.168.2.18:8003", 10, 100);

以上用工具压测了3秒,正常处理的请求是1000多个,其他几十万的请求都被网关拦载了。所以对于大量并发涌入的时候,rps的限制就能体现其作用。

故障迁移和恢复

这个功能暂由组件内部管理,组件会每秒探测服务的可访问情况;当有服务状态不可或恢复后组件都会重新构建负载策略确保服务的可用性。不过当服务不可用的情况存在短暂时间里访问到这服务的请求会返回5XX访问错误信息。

总结

到这里Bumblebee使用和相关基础功能就介绍完了,但Bumblebee所提供的功能并不远止这些,它提供不同的事件接口用于扩展服务处理可以制很完善的业务处理功能,后面会一步步深入讲解。

示例代码:https://github.com/IKende/Bumblebee/tree/master/Samples/AspCoreWebapi

原文地址:https://www.cnblogs.com/smark/p/10556849.html