微服务架构eShopOnContainers怎么使用

发布时间:2021-12-20 09:21:51 作者:iii
来源:亿速云 阅读:288

这篇文章主要介绍“微服务架构eShopOnContainers怎么使用”,在日常操作中,相信很多人在微服务架构eShopOnContainers怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”微服务架构eShopOnContainers怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

解析源码

我们知道使用EventBus是为了解除Publisher和Subscriber之间的依赖性,这样我们的Publisher就不需要知道有多少Subscribers,只需要通过EventBus进行注册管理就好了,在eShop项目中,有一个这样的接口IEventBus(eShopOnContainers\src\BuildingBlocks\EventBus\EventBus\Abstractions)

public interface IEventBus
{   
  void Subscribe<T, TH>(Func<TH> handler)        where T : IntegrationEvent        where TH : IIntegrationEventHandler<T>;    

   void Unsubscribe<T, TH>()        where TH : IIntegrationEventHandler<T>        where T : IntegrationEvent;  

  void Publish(IntegrationEvent @event); }

我们可以看到这个接口定义了EventBus所需的一些操作, 对比大神的EventBus,相关功能都是一致的,我们看下它的实现类:EventBusRabbitMQ,从名字上可以看出,这是一个通过RabbitMQ来进行管理的EventBus,我们可以看到它使用了IEventBusSubscriptionsManager进行订阅存储,也就是大神文中的:

private readonly ConcurrentDictionary<Type, List<Type>> _eventAndHandlerMapping;

微软在Demo中把其提取出了接口,把一些常用方法给提炼了出来,但是核心还是Dictionary<string, List<Delegate>>, 使用Dictionary进行Map映射。通过Subscribe和UnSubscribe进行订阅和取消,使用Publish方法进行发布操作。

public void Subscribe<T, TH>(Func<TH> handler)    where T : IntegrationEvent    where TH : IIntegrationEventHandler<T>{   
 var eventName = typeof(T).Name;  
 var containsKey = _subsManager.HasSubscriptionsForEvent<T>();  
 if (!containsKey)    {      
       if (!_persistentConnection.IsConnected)        {            _persistentConnection.TryConnect();        }      
      using (var channel = _persistentConnection.CreateModel())        {            channel.QueueBind(queue: _queueName,                                exchange: BROKER_NAME,                                routingKey: eventName);        }    }    _subsManager.AddSubscription<T, TH>(handler); }

我们看到在订阅的时候,EventBus会检查下在Map中是否有相应的注册,如果没有的话首先回去RabbitMQ中创建一个新的channel进行绑定,随后在Map中进行注册映射。

UnSubscribe则直接从Map中取消映射,通过OnEventRemoved事件判断Map下此映射的subscriber是否为空,为空则从RabbitMQ中关闭channel。

在RabbitMQ的构造方法中,我们看到这样一个创建:CreateConsumerChannel(),这里创建了一个EventingBasicConsumer,当Queue中有新的消息时会通过ProcessEvent执行Map中注册的handler(subscribers)

在ProcessEvent方法中,回去Map中找寻subscribers,然后通过动态反射进行执行:

private async Task ProcessEvent(string eventName, string message)
{    if (_subsManager.HasSubscriptionsForEvent(eventName))
    { 
        var eventType = _subsManager.GetEventTypeByName(eventName);        var integrationEvent = JsonConvert.DeserializeObject(message, eventType);        var handlers = _subsManager.GetHandlersForEvent(eventName);     
   foreach (var handlerfactory in handlers)        {          
  var handler = handlerfactory.DynamicInvoke();      
   var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);  
     await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });        }    } }

微软通过简单的代码解耦了Publisher和Subscribers之间的依赖关系,我们引用大神的总结:

微服务架构eShopOnContainers怎么使用

应用

在catalog.api中,微软出现了EventBus,我在上一篇中也提到了,这是我的一个疑惑,因为在catalog中并没有订阅操作,直接执行了Publish操作,原先以为是一个空操作,后来看了Basket.Api我才知道为何微软要用RabbitMQ。

使用RabbitMQ,我们不仅是从类之间的解耦,更可以跨项目,跨语言,跨平台的解耦,publisher仅仅需要把消息体(IntegrationEvent)传送到RabbitMQ,Consumer从Queue中获取消息体,然后推送到Subscribers执行相应的操作。我们看下Basket.Api.Startup.cs:

protected virtual void ConfigureEventBus(IApplicationBuilder app)
{  
  var catalogPriceHandler = app.ApplicationServices        .GetService<IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>>();    var orderStartedHandler = app.ApplicationServices        .GetService<IIntegrationEventHandler<OrderStartedIntegrationEvent>>();    var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();    eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>        (() => app.ApplicationServices.GetRequiredService<ProductPriceChangedIntegrationEventHandler>());    eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>        (() => app.ApplicationServices.GetRequiredService<OrderStartedIntegrationEventHandler>()); }

到此,关于“微服务架构eShopOnContainers怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 微服务是什么?微服务架构又是什么?
  2. 架构演进之「微服务架构」

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

eshoponcontainers

上一篇:Phoenix常见问题有哪些

下一篇:Lr中脚本调试之参数化、检查点的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》