Rebus message bus

I'm here mainly to talk about ABP based on a framework Rebus write module


 
Directory Structure

For Rebus little information online, in fact, I am not very understanding of the service bus. . Personal understanding is like ABP in EventBus like that, but integrates a number of message queues as MSMQ, RabbitMQ and so on.

Ado, mainly under the following about several key documents

 
RebusRabbitMqModule

The ABP module it is written, detailed look can go to the official website of ABP, where the main code is here

            var moduleConfig = IocManager.Resolve<IRebusRabbitMqModuleConfig>();

            if (moduleConfig.Enabled)
            {
                var rebusConfig = Configure.With(new CastleWindsorContainerAdapter(IocManager.IocContainer));

                if (moduleConfig.LoggingConfigurer != null) { //配置Rebus用哪种工具来记录日志,我这里用的Log4net rebusConfig.Logging(moduleConfig.LoggingConfigurer); } rebusConfig.Serialization(moduleConfig.SerializerConfigurer); if (moduleConfig.OptionsConfigurer != null) { //自定义配置 rebusConfig.Options(moduleConfig.OptionsConfigurer); } rebusConfig.Options(c => { c.SetMaxParallelism(moduleConfig.MaxParallelism); c.SetNumberOfWorkers(moduleConfig.NumberOfWorkers); }); if (moduleConfig.MessageAuditingEnabled) { //消息审计队列名称 rebusConfig.Options(o => o.EnableMessageAuditing(moduleConfig.MessageAuditingQueueName)); } var mqMessageTypes = new List<Type>(); //通过反射取到所有继承IHandleMessages的类进行消息订阅 foreach (var assembly in moduleConfig.AssemblysIncludeRebusMqMessageHandlers) { IocManager.IocContainer.AutoRegisterHandlersFromAssembly(assembly); mqMessageTypes.AddRange(assembly.GetTypes() .Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandleMessages<>))) .SelectMany(t => t.GetInterfaces()) .Distinct() .SelectMany(t => t.GetGenericArguments()) .Distinct()); } //这个就是配置使用RabbitMq进行消息通信的方法,具体的去看Rebus上的文档 _bus = rebusConfig.Transport(c => c.UseRabbitMq(moduleConfig.ConnectString, moduleConfig.QueueName)).Start(); //Subscribe messages mqMessageTypes = mqMessageTypes.Distinct().ToList(); foreach (var mqMessageType in mqMessageTypes) { _bus.Subscribe(mqMessageType); } } 

Use Module

Find the place you need to reference module, as shown below


 
Add dependent

Then the corresponding configuration


 
image.png

Inside RabbitMqUrl is to visit your local RabbitMq address, like I was amqp: // guest: [email protected]: 5672 /

The next step is how to transmit and process messages

Like other EventBus, the need to establish EventData

 public class Test
    {
        public string Name { get; set; } } 

Then EventHander

public class TestHandler : EventDataConsumerHandlerBase<Test>
    {
        public override Task Handle(Test message) { //这里就是写你需要对message进行怎样的处理 return base.Handle(message); } } 

Finally is released, here is my message which is sent AppService

 #region 构造函数
        private readonly IRepository<BaseItem> _baseItemRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IMqMessagePublisher _iMqMessagePublisher;//依赖注入 public BaseItemAppService(IRepository<BaseItem> baseItemRepository, ICacheManager cacheManager, IMqMessagePublisher iMqMessagePublisher) : base(baseItemRepository) { this._cacheManager = cacheManager; this._baseItemRepository = baseItemRepository; _iMqMessagePublisher = iMqMessagePublisher; } #endregion #region 增删改查 protected override IQueryable<BaseItem> CreateFilteredQuery(BaseItemSearchDto input) { //消息的发布 _iMqMessagePublisher.Publish(new Test { Name = "123" }); return base.CreateFilteredQuery(input) .WhereIf(input.DisplayName.IsNotNullOrEmpty(), m => m.DisplayName.Contains(input.DisplayName)) .WhereIf(input.TypeId != null, m => m.TypeId == input.TypeId); } #endregion 

that's it

I am talking about is not very detailed (my ability to express not), you can go directly to see my code, I'll do a simple package is not very troublesome. You might ask me what business integration scenarios after is this? I'll say I used it at:

  • Order processing: the same time there may be many orders, then we can put him posted to a queue for processing a push, and unlabeled completed orders, even if you program error, he will automatically push back as if default is 5 secondary
  • Scan code storage time: scan code storage warehouse manager speed is very fast, but the background program needs to operate than a time-consuming, this time can also scan the code slowly added to the queue processing.

Rebus my little research, some hidden goodies I could not join temporarily, hoping to point to mention any more points. . There is bus service what?

Finally, attach a few addresses:



Author: Shao Jianan
link: https: //www.jianshu.com/p/5961bc5e556d
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/Leo_wl/p/11069707.html