(feature): demo shared statestore
This commit is contained in:
parent
195342f0a3
commit
a6f3089cb5
5
src/Core/DaprPubSubComponents.cs
Normal file
5
src/Core/DaprPubSubComponents.cs
Normal file
@ -0,0 +1,5 @@
|
||||
namespace Core;
|
||||
|
||||
public static class DaprPubSubComponents {
|
||||
public const string PubSub = "pubsub";
|
||||
}
|
||||
5
src/Core/DaprPubSubTopics.cs
Normal file
5
src/Core/DaprPubSubTopics.cs
Normal file
@ -0,0 +1,5 @@
|
||||
namespace Core;
|
||||
|
||||
public static class DaprPubSubTopics {
|
||||
public const string Test = "test";
|
||||
}
|
||||
5
src/Core/DaprShardStateStoreKeys.cs
Normal file
5
src/Core/DaprShardStateStoreKeys.cs
Normal file
@ -0,0 +1,5 @@
|
||||
namespace Core;
|
||||
|
||||
public static class DaprShardStateStoreKeys {
|
||||
public const string Test = "test";
|
||||
}
|
||||
7
src/Core/DaprStateStoreComponents.cs
Normal file
7
src/Core/DaprStateStoreComponents.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Core;
|
||||
|
||||
public static class DaprStateStoreComponents {
|
||||
public const string SharedStore = "statestore";
|
||||
public const string PrivateStore = "privatestore";
|
||||
public const string ActorsStore = "actorsstore";
|
||||
}
|
||||
10
src/Core/StateEntities/TestStateEntity.cs
Normal file
10
src/Core/StateEntities/TestStateEntity.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core.StateEntities;
|
||||
public class TestStateEntity {
|
||||
public string Message { get; set; }
|
||||
}
|
||||
@ -1,9 +1,10 @@
|
||||
using Core;
|
||||
using Core.Commands;
|
||||
using Core.StateEntities;
|
||||
using MaksIT.Core.Dapr;
|
||||
using MaksIT.Core.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Publisher.Models;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Publisher.Controllers;
|
||||
[ApiController]
|
||||
@ -11,24 +12,45 @@ namespace Publisher.Controllers;
|
||||
public class DaprTestController : ControllerBase {
|
||||
|
||||
private readonly ILogger<DaprTestController> _logger;
|
||||
private readonly IDaprService _daprService;
|
||||
private readonly IDaprPublisherService _daprPublisherService;
|
||||
private readonly IDaprStateStoreService _daprStateStoreService;
|
||||
|
||||
public DaprTestController(
|
||||
ILogger<DaprTestController> logger,
|
||||
IDaprService daprService
|
||||
IDaprPublisherService daprPublisherService,
|
||||
IDaprStateStoreService daprStateStoreService
|
||||
) {
|
||||
_logger = logger;
|
||||
_daprService = daprService;
|
||||
_daprPublisherService = daprPublisherService;
|
||||
_daprStateStoreService = daprStateStoreService;
|
||||
}
|
||||
|
||||
[HttpPost(Name = "DaprTestPublisher")]
|
||||
public async Task<IActionResult> Post([FromBody] PostDaprTestRequest requestData) {
|
||||
var result = await _daprService.PublishEventAsync(
|
||||
"pubsub",
|
||||
"test",
|
||||
|
||||
var setStateResult = await _daprStateStoreService.SetStateAsync(DaprStateStoreComponents.SharedStore, DaprShardStateStoreKeys.Test, new TestStateEntity {
|
||||
Message = requestData.Message
|
||||
});
|
||||
|
||||
if (!setStateResult.IsSuccess)
|
||||
return setStateResult.ToActionResult();
|
||||
|
||||
var gerStateResult = await _daprStateStoreService.GetStateAsync<TestStateEntity>(DaprStateStoreComponents.SharedStore, DaprShardStateStoreKeys.Test);
|
||||
if (!gerStateResult.IsSuccess || gerStateResult.Value == null) {
|
||||
return gerStateResult.ToActionResult();
|
||||
}
|
||||
|
||||
var state = gerStateResult.Value;
|
||||
|
||||
_logger.LogInformation($"Shared state message: {state.Message}");
|
||||
|
||||
var result = await _daprPublisherService.PublishEventAsync(
|
||||
DaprPubSubComponents.PubSub,
|
||||
DaprPubSubTopics.Test,
|
||||
new TestCommand {
|
||||
Message = requestData.Message
|
||||
}.ToJson());
|
||||
|
||||
return result.ToActionResult();
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.RegisterPublisher();
|
||||
builder.Services.RegisterStateStore();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MaksIT.Core" Version="1.1.6" />
|
||||
<PackageReference Include="MaksIT.Core.Dapr" Version="1.0.0" />
|
||||
<PackageReference Include="MaksIT.Core.Dapr" Version="1.0.3" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
using Core;
|
||||
using Core.Commands;
|
||||
using Core.StateEntities;
|
||||
using Dapr;
|
||||
using MaksIT.Core.Dapr;
|
||||
using MaksIT.Core.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@ -9,22 +12,38 @@ namespace Subscriber.Controllers;
|
||||
public class DaprTestController : ControllerBase {
|
||||
|
||||
private readonly ILogger<DaprTestController> _logger;
|
||||
private readonly IDaprStateStoreService _daprStateStoreService;
|
||||
|
||||
public DaprTestController(
|
||||
ILogger<DaprTestController> logger
|
||||
ILogger<DaprTestController> logger,
|
||||
IDaprStateStoreService daprStateStoreService
|
||||
) {
|
||||
_logger = logger;
|
||||
_daprStateStoreService = daprStateStoreService;
|
||||
}
|
||||
|
||||
[Topic("pubsub", "test")]
|
||||
[Topic(DaprPubSubComponents.PubSub, DaprPubSubTopics.Test)]
|
||||
[HttpPost(Name = "DaprTestSubscriber")]
|
||||
public IActionResult Post([FromBody] string json) {
|
||||
public async Task<IActionResult> Post([FromBody] string json) {
|
||||
|
||||
var requestData = json.ToObject<TestCommand>();
|
||||
if (requestData == null)
|
||||
return BadRequest();
|
||||
|
||||
_logger.LogInformation("Received message: {0}", requestData.Message);
|
||||
_logger.LogInformation($"Pubsub command message: {requestData.Message}");
|
||||
|
||||
var getStateResult = await _daprStateStoreService.GetStateAsync<TestStateEntity>(DaprStateStoreComponents.SharedStore, DaprShardStateStoreKeys.Test);
|
||||
if (!getStateResult.IsSuccess || getStateResult.Value == null) {
|
||||
return getStateResult.ToActionResult();
|
||||
}
|
||||
|
||||
var state = getStateResult.Value;
|
||||
_logger.LogInformation($"Shared state message: {state.Message}");
|
||||
|
||||
var deleteStateResult = await _daprStateStoreService.DeleteStateAsync(DaprStateStoreComponents.SharedStore, DaprShardStateStoreKeys.Test);
|
||||
if (!deleteStateResult.IsSuccess)
|
||||
return deleteStateResult.ToActionResult();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,8 @@ builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
builder.Services.RegisterStateStore();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MaksIT.Core" Version="1.1.6" />
|
||||
<PackageReference Include="MaksIT.Core.Dapr" Version="1.0.0" />
|
||||
<PackageReference Include="MaksIT.Core.Dapr" Version="1.0.3" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -26,6 +26,9 @@ services:
|
||||
]
|
||||
depends_on:
|
||||
- placement
|
||||
- rabbitmq
|
||||
- redis
|
||||
- zipkin
|
||||
- publisher
|
||||
network_mode: "service:publisher" # Attach the publisher-dapr service to the publisher network namespace
|
||||
|
||||
@ -53,6 +56,9 @@ services:
|
||||
]
|
||||
depends_on:
|
||||
- placement
|
||||
- rabbitmq
|
||||
- redis
|
||||
- zipkin
|
||||
- subscriber
|
||||
network_mode: "service:subscriber" # Attach the subscriber-dapr service to the subscriber network namespace
|
||||
|
||||
@ -103,6 +109,6 @@ services:
|
||||
|
||||
redis:
|
||||
ports:
|
||||
- "6380:6379"
|
||||
- "6379:6379"
|
||||
networks:
|
||||
- hello-dapr
|
||||
@ -3,6 +3,36 @@ kind: Component
|
||||
metadata:
|
||||
name: statestore
|
||||
namespace: dapr-test
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: redis:6379
|
||||
- name: keyPrefix
|
||||
value: none # Disables prefixing
|
||||
|
||||
---
|
||||
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: privatestatestore
|
||||
namespace: dapr-test
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: redis:6379
|
||||
|
||||
---
|
||||
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: actorsstatestore
|
||||
namespace: dapr-test
|
||||
spec:
|
||||
type: state.redis
|
||||
version: v1
|
||||
|
||||
Loading…
Reference in New Issue
Block a user