DaprServiceBus:Dapr的服务总线抽象

  • Y0_963927
    了解作者
  • 36.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-05 11:51
    上传日期
达普活动巴士 Dapr pub / sub上的事件总线抽象。 介绍 ,它代表d istributed鸭折襞ř不定时,使用,以提供一个过消息代理和排队系统,包括 , ,和几个 。 提供了一个API,可以从ASP.NET服务执行发布/,但是它要求应用程序直接了解Dapr。发布者需要使用DaprClient来发布事件,而订阅者需要使用Topic属性来装饰控制器动作。 该共和国总统办公室事件总线项目的目的是提供了共和国总统办公室的pub / sub薄抽象层,这样应用程序可以发布事件和订阅主题,而不共和国总统办公室的任何知识。这允许更好的可测试性和灵活性,尤其是对于本机不包括HTTP堆栈的工作程序服务。 用法 在发布者和订阅者中,都需要通过调用Startup.ConfigureServices services.AddDaprEventBus向DI注册Dapr事件总线。 public void
DaprServiceBus-master.zip
内容介绍
# Dapr Event Bus An event bus abstraction over Dapr pub/sub. ## Introduction [Dapr](https://dapr.io/), which stands for **D**istributed **Ap**plication **R**untime, uses a [sidecar pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar) to provide a [pub/sub abstraction](https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-overview/) over message brokers and queuing systems, including [AWS SNS+SQS](https://www.helenanderson.co.nz/sns-sqs/), [GCP Pub/Sub](https://cloud.google.com/pubsub), [Azure Events Hub](https://azure.microsoft.com/en-us/services/event-hubs/) and several [others](https://docs.dapr.io/operations/components/setup-pubsub/supported-pubsub/). The [Dapr .NET SDK](https://github.com/dapr/dotnet-sdk) provides an API to perform pub/sub from an ASP.NET service, but it requires the application to be directly aware of Dapr. Publishers need to use `DaprClient` to publish an event, and subscribers need to decorate controller actions with the `Topic` attribute. The purpose of the **Dapr Event Bus** project is to provide a thin abstraction layer over Dapr pub/sub so that applications may publish events and subscribe to topics _without any knowledge of Dapr_. This allows for better testability and flexibility, especially for worker services that do not natively include an HTTP stack. ## Usage 1. In both the _publisher_ and _subscriber_, you need to register the **Dapr Event Bus** with DI by calling `services.AddDaprEventBus` in `Startup.ConfigureServices`. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // Add Dapr Event Bus services.AddDaprEventBus(Constants.DaprPubSubName); } ``` 2. Define an event using a C# [record](https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/exploration/records) that extends `IntegrationEvent`. For example, the following `WeatherForecastEvent` record does so by adding a `WeatherForecasts` property. ```csharp public record WeatherForecastEvent(IEnumerable<WeatherForecast> WeatherForecasts) : IntegrationEvent { } ``` 3. In the publisher, inject `IEventBus` into the constructor of a controller (Web API projects) or worker class (Worker Service projects). Then call `EventBus.PublishEventAsync`, passing the event you defined in step 2. ```csharp public class Worker : BackgroundService { private readonly WeatherFactory _factory; private readonly IEventBus _eventBus; private readonly ILogger<Worker> _logger; public Worker(WeatherFactory factory, IEventBus eventBus, ILogger<Worker> logger) { _factory = factory; _eventBus = eventBus; _logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Publishing event at: {time}", DateTimeOffset.Now); // Create weather forecasts var weathers = _factory.CreateWeather(); // Publish event await _eventBus.PublishEventAsync(new WeatherForecastEvent(weathers)); // Pause await Task.Delay(5000, stoppingToken); } } } ``` 4. In the subscriber create the same event as in the publisher. Then create an **event handler** that extends `IntegrationEventHandler<TIntegrationEvent>` where `TIntegrationEvent` is the event type you defined earlier. - Override `HandleAsync` to perform a task when an event is received. - For example, `WeatherForecastEventHandler` sets `WeatherForecasts` on `WeatherForecastRepository` to the `WeatherForecasts` property of `WeatherForecastEvent`. ```csharp public class WeatherForecastEventHandler : IntegrationEventHandler<WeatherForecastEvent> { private readonly WeatherForecastRepository _weatherRepo; private readonly ILogger<WeatherForecastEventHandler> _logger; public WeatherForecastEventHandler(WeatherForecastRepository weatherRepo, ILogger<WeatherForecastEventHandler> logger) { _weatherRepo = weatherRepo; _logger = logger; } public override Task HandleAsync(WeatherForecastEvent @event) { _logger.LogInformation($"Weather posted."); _weatherRepo.WeatherForecasts = @event.WeatherForecasts; return Task.CompletedTask; } } ``` 5. Lastly, in `Startup.Configure` call `app.UseDaprEventBus`, passing an action that configures the Event Bus by adding handlers. - Make sure to add parametera to `Startup.Configure` for each handler you wish to add. - For example, to add the weather forecast handler, you much add a `WeatherForecastEventHandler` parameter to the `Configure` method. ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env, WeatherForecastEventHandler forecastEventHandler) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // Use Dapr Event Bus app.UseDaprEventBus(EventBus => { // Add handler EventBus.AddHandler(forecastEventHandler); }); } ``` ## EventBus.Abstractions The **EventBus.Abstractions** package includes an `IEventBus` interface implemented by a `EventBusBase` abstract class. ```csharp public interface IEventBus { Dictionary<string, List<IIntegrationEventHandler>> Topics { get; } void AddHandler(IIntegrationEventHandler handler); Task PublishEventAsync<TIntegrationEvent>(TIntegrationEvent @event, string topic = null) where TIntegrationEvent : IIntegrationEvent; } ``` When a subscriber calls `AddHandler`, it passes a class that implements `IIntegrationEventHandler` by extending `IntegrationEventHandler`. The event handler is added to a topic which can have one more handlers. ```csharp public interface IIntegrationEventHandler { string Topic { get; set; } Task HandleAsync(IIntegrationEvent @event); } public interface IIntegrationEventHandler<in TIntegrationEvent> : IIntegrationEventHandler where TIntegrationEvent : IIntegrationEvent { Task HandleAsync(TIntegrationEvent @event); } ``` The generic version of `IntegrationEventHandler` includes a `TIntegrationEvent` type argument that must implement `IIntegrationEvent`. The `IntegrationEvent` abstract record provides defaults for both properies. ```csharp public interface IIntegrationEvent { Guid Id { get; } DateTime CreationDate { get; } } ``` ```csharp public abstract record IntegrationEvent : IIntegrationEvent { public Guid Id { get; init; } = Guid.NewGuid(); public DateTime CreationDate { get; init; } = DateTime.UtcNow; } ``` ## EventBus.Dapr The **EventBus.Dapr** package has a `DaprEventBus` that extends `EventBusBase` by injecting `DaprClient` and `DaprEventBusOptions` for the pubsub component name needed by `DaprClient.PublishEventAsync`. The event topic defaults to the type name of the the event, but it can also be supplied explicitly. ```csharp public class DaprEventBus : EventBusBase { private readonly IOptions<DaprEventBusOptions> _options; private readonly DaprClient _dapr; public DaprEventBus(IOptions<DaprEventBusOptions> options, DaprClient dapr) { _options = options ?? throw new ArgumentNullException(nameof(options)); _dapr = dapr ?? throw new ArgumentNullException(nameof(dapr)); } public override async Task PublishEventAsync<TIntegrationEvent>(TIntegrationEvent @event, string topic = null) { if (@event is null) throw new ArgumentNullException(nameof(@event)); topic = topic ?? @event.GetType().Name; await _dapr.PublishEventAsync(_options.Value.PubSubName, topic, (dynam
评论
    相关推荐
    • Weather Report .NET
      基于Google Weather API的程序。支持中文,英文,德文。.NET3.5做的,但是,由于Google问题,不知道是否在内地可以使用?无论怎么样,可以测试下。...国外下载地址:http://www.box.net/shared/d6ct9jlpyf
    • .net远程
      使用http管道的.net远程
    • .net Excel NPOI
      很多人可能对NPOI还很陌生,别担心,通过本教程你将对NPOI有进一步的认识和理解 http://tonyqus.sinaapp.com/npoi-1-2-1%e6%95%99%e7%a8%8b
    • aspx后门.net
      密码:upx8 原文网址:http://upx8.com/article.asp?id=275
    • system.net.http.dll
      system.net.http.dll文件 2016 版本2.2.22.0 186K DDL文件
    • .net学习资料
      .net插件框架。ado.nethttp协议,wcf,webapi,依赖注入,并发,死锁等等
    • .NET HTTP 请求服务接口
      .NET 常用的 HTTP 请求服务的方法 类
    • System.Net.Http.Formatting
      System.Net.Http.Formatting
    • Rss.Net类库
      开源项目Rss.Net 类库http://www.rssdotnet.com
    • System.Net.Http.dll下载
      System.Net.Http.dll下载