我們要實現的是:在blazorweb服務中釋出一個事件,並傳遞事件引數,然後在serviceapi1服務中訂閱該事件,接收到blazorweb服務中釋出的事件和引數。
1 在blazorweb服務中釋出一個事件
在DaprTest1.Server專案的WeatherForecastController.cs檔案中增加事件釋出API:
[HttpPost(nameof(PublishTestEvent))]
public async Task PublishTestEvent(TestEventModel eventModel)
{
await _daprClient.PublishEventAsync<TestEventModel>("pubsub", "TestEventName", eventModel);
}
"TestEventModel"是自定義的事件訊息類,"TestEventName"是事件的名稱,"pubsub" 是事件釋出訂閱的名稱,定義在“pubsub.yaml” 元件中:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
這個元件中定義的釋出訂閱採用了Redis 的 Stream 特性,要注意舊版本的Redis是否支援Stream。
2 在serviceapi1服務中訂閱該事件
在DaprTest1.ServiceApi1專案中新增Dapr.AspNetCore包,該包實現了ASP.NET Core與Dapr的整合,例如依賴注入DaprClient物件,將事件訂閱釋出功能直接整合到 ASP.NET Core 模型繫結功能中等。
在DaprTest1.ServiceApi1專案的Startup.cs 檔案增加事件訂閱相關程式碼(注意綠色部分):
public void ConfigureServices(IServiceCollection services)
{ services.AddControllers().AddDapr();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "DaprTest1.ServiceApi1", Version = "v1" });
});
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "DaprTest1.ServiceApi1 v1"));
} app.UseRouting(); app.UseAuthorization(); app.UseCloudEvents(); app.UseEndpoints(endpoints =>
{
endpoints.MapSubscribeHandler();
endpoints.MapControllers();
});
}
在DaprTest1.ServiceApi1專案的WeatherForecastController檔案增加事件訂閱API
[Topic("pubsub", "TestEventName")]
[HttpPost(nameof(SubscribleTestEvent))]
public async Task SubscribleTestEvent(TestEventModel eventModel)
{
await Task.CompletedTask;
}
[Topic("pubsub", "TestEventName")] 訂閱了"pubsub"訂閱名稱的TestEventName事件。
3 在Blazor專案中增加Blazor前端事件釋出選單和頁面
@page "/pubsub"
@using DaprTest1.Shared
@using System.Text.Json
@inject HttpClient Http <h1>釋出訂閱</h1> <p>This component demonstrates publish and subscrible event.</p> <p>編碼:<input type="text" @bind="eventModel.Code" />, 數量:<input type="text" @bind="eventModel.Amount" /></p> <button class="btn btn-primary" @onclick="PublishEvent">釋出事件</button> @code {
private TestEventModel eventModel = new TestEventModel();
private async Task PublishEvent() => await Http.PostAsJsonAsync<TestEventModel>("WeatherForecast/PublishTestEvent", eventModel); }
4 事件釋出訂閱測試
和上一節一樣,我們先開啟每個微服務的SideCar,注意,因為的SideCar 指定了狀態儲存的Redis,所以我們先要開啟Redis,不然SideCar會啟動失敗。確保每個微服務的SideCar都是執行狀態。
然後啟動兩個微服務,並訪問http://localhost:5000
在ServiceApi1服務的事件接收處設定好斷點,然後點選“釋出事件”按鈕
可以看到,我們成功接收到了TestEventName事件和事件釋出的引數。
5 將Dapr的釋出訂閱元件修改為RabbitMQ
通常情況下,我們會用RabbitMQ來支援事件的釋出和訂閱,我們將Dapr 釋出訂閱元件“pubsub.yaml”,修改為如下內容即可:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
spec:
type: pubsub.rabbitmq
version: v1
metadata:
- name: host
value: "amqp://admin:******@localhost:5672"
- name: durable
value: true
相關程式碼:iamxiaozhuang/dapr-test (github.com)