杜燕明

杜燕明

Anno框架交流:478399354 欢迎大家加群相互交流!

Anno 框架 增加缓存、限流策略、事件总线、支持 thrift grpc 作为底层传输

github 地址:https://github.com/duyanming/dymDemo

dym 分布式开发框架 Demo 熔断 限流 事件总线(包括基于内存的、rabbitmq的) CQRS DDD 实例 随后更新

Java 实现 https://github.com/duyanming/anno.thrift-parent

Demo 在线演示地址 :http://140.143.207.244

账号:anno
密码:123456
 

Anno EventBus

Eventbus Support  InMemory and Rabbitmq

  

1、Server配置

//指定EventHandler的 所在程序集
    var funcs = Anno.Const.Assemblys.Dic.Values.ToList();
                #region RabbitMQEventBus
                //消费失败通知

                RabbitMQEventBus.Instance.ErrorNotice += (string exchange, string routingKey, Exception exception, string body) =>
                        {
                            Log.Fatal(new { exchange, routingKey, exception, body }, typeof(RabbitMQEventBus));
                        };
                EventBusSetting.Default.RabbitConfiguration = new RabbitConfiguration()
                {
                    HostName = "192.168.100.173",
                    VirtualHost = "dev",
                    UserName = "dev",
                    Password = "dev",
                    Port = 5672
                };
                RabbitMQEventBus.Instance.SubscribeAll(funcs);

                #endregion
                #region InMemory EventBus
                EventBus.Instance.ErrorNotice += (string exchange, string routingKey, Exception exception, string body) =>
                {
                    Log.Fatal(new { exchange, routingKey, exception, body }, typeof(EventBus));
                };
                EventBus.Instance.SubscribeAll(funcs);

 

2、EventData配置

using Anno.EventBus;
    
    namespace Events
    {
        public class FirstMessageEvent:EventData
        {
            public string Message { get; set; }
        }
    }

 

3、EventHandler配置

namespace Anno.Plugs.SamsundotService.EventHandler
    {
        using Anno.EventBus;
        using Events;
    
        class FirstMessageEventHandler : IEventHandler<FirstMessageEvent>
        {
            public void Handler(FirstMessageEvent entity)
            {
                Log.Log.Info(new { Plugs= "Samsundot",Entity=entity },typeof(FirstMessageEventHandler));
            }
        }
    }
   
   namespace Anno.Plugs.YYTestService.EventHandler
   {
       using Anno.EventBus;
       using Events;
   
       class FirstMessageEventHandler : IEventHandler<FirstMessageEvent>
       {
           public void Handler(FirstMessageEvent entity)
           {
               Log.Log.Info(new { Plugs = "YYTest", Entity = entity }, typeof(FirstMessageEventHandler));
           }
       }
       /// <summary>
       /// 异常消费演示,测试 消费失败通知
       /// </summary>
       class FirstMessageExceptionEventHandler : IEventHandler<FirstMessageEvent>
       {
           public void Handler(FirstMessageEvent entity)
           {
               Log.Log.Info(new { Plugs = "YYTest",Handle= "FirstMessageExceptionEventHandler", Entity = entity }, typeof(FirstMessageEventHandler));
               throw new Exception("异常消费演示,测试 消费失败通知 From FirstMessageExceptionEventHandler!");
           }
       }
   }

 

4、中间件

4.1 缓存中间件

nuget install

Install-Package Anno.EngineData.Cache

 

using System;
using System.Collections.Generic;
using System.Text;
using Anno.EngineData;
using Anno.EngineData.Cache;


namespace Anno.Plugs.CacheRateLimitService
{
   public class CacheModule : BaseModule
   {
       /*
       参数1:缓存长度
       参数2:缓存存活时间
       参数3:缓存存活时间是否滑动
       */
       [CacheLRU(5,6,true)]
       public ActionResult Cache(string msg)
       {
           Console.WriteLine(msg);
           return new ActionResult(true, null,null,msg);
       }
   }
}

 

4.2 缓存中间件

nuget install

Install-Package Anno.EngineData.RateLimit
   
using System;
using System.Collections.Generic;
using System.Text;
using Anno.EngineData;
using Anno.RateLimit;

namespace Anno.Plugs.CacheRateLimitService
{
   public class LimitModule : BaseModule
   {
       /*
       参数1:限流算法是令牌桶还是漏桶
       参数2:限流时间片段单位秒
       参数3:单位时间可以通过的请求个数
       参数4:桶容量
       */
       [EngineData.Limit.RateLimit(LimitingType.TokenBucket,1,5,5)]
       public ActionResult Limit(string msg)
       {
           Console.WriteLine(msg);
           return new ActionResult(true, null, null, msg);
       }
   }
}

 

posted @ 2019-12-18 18:18  杜燕明  阅读(869)  评论(0编辑  收藏  举报