(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.Commands;
|
||||||
|
using Core.StateEntities;
|
||||||
using MaksIT.Core.Dapr;
|
using MaksIT.Core.Dapr;
|
||||||
using MaksIT.Core.Extensions;
|
using MaksIT.Core.Extensions;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Publisher.Models;
|
using Publisher.Models;
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Publisher.Controllers;
|
namespace Publisher.Controllers;
|
||||||
[ApiController]
|
[ApiController]
|
||||||
@ -11,24 +12,45 @@ namespace Publisher.Controllers;
|
|||||||
public class DaprTestController : ControllerBase {
|
public class DaprTestController : ControllerBase {
|
||||||
|
|
||||||
private readonly ILogger<DaprTestController> _logger;
|
private readonly ILogger<DaprTestController> _logger;
|
||||||
private readonly IDaprService _daprService;
|
private readonly IDaprPublisherService _daprPublisherService;
|
||||||
|
private readonly IDaprStateStoreService _daprStateStoreService;
|
||||||
|
|
||||||
public DaprTestController(
|
public DaprTestController(
|
||||||
ILogger<DaprTestController> logger,
|
ILogger<DaprTestController> logger,
|
||||||
IDaprService daprService
|
IDaprPublisherService daprPublisherService,
|
||||||
|
IDaprStateStoreService daprStateStoreService
|
||||||
) {
|
) {
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_daprService = daprService;
|
_daprPublisherService = daprPublisherService;
|
||||||
|
_daprStateStoreService = daprStateStoreService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost(Name = "DaprTestPublisher")]
|
[HttpPost(Name = "DaprTestPublisher")]
|
||||||
public async Task<IActionResult> Post([FromBody] PostDaprTestRequest requestData) {
|
public async Task<IActionResult> Post([FromBody] PostDaprTestRequest requestData) {
|
||||||
var result = await _daprService.PublishEventAsync(
|
|
||||||
"pubsub",
|
var setStateResult = await _daprStateStoreService.SetStateAsync(DaprStateStoreComponents.SharedStore, DaprShardStateStoreKeys.Test, new TestStateEntity {
|
||||||
"test",
|
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 {
|
new TestCommand {
|
||||||
Message = requestData.Message
|
Message = requestData.Message
|
||||||
}.ToJson());
|
}.ToJson());
|
||||||
|
|
||||||
return result.ToActionResult();
|
return result.ToActionResult();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ builder.Services.AddEndpointsApiExplorer();
|
|||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
builder.Services.RegisterPublisher();
|
builder.Services.RegisterPublisher();
|
||||||
|
builder.Services.RegisterStateStore();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MaksIT.Core" Version="1.1.6" />
|
<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="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
|
using Core;
|
||||||
using Core.Commands;
|
using Core.Commands;
|
||||||
|
using Core.StateEntities;
|
||||||
using Dapr;
|
using Dapr;
|
||||||
|
using MaksIT.Core.Dapr;
|
||||||
using MaksIT.Core.Extensions;
|
using MaksIT.Core.Extensions;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
@ -9,22 +12,38 @@ namespace Subscriber.Controllers;
|
|||||||
public class DaprTestController : ControllerBase {
|
public class DaprTestController : ControllerBase {
|
||||||
|
|
||||||
private readonly ILogger<DaprTestController> _logger;
|
private readonly ILogger<DaprTestController> _logger;
|
||||||
|
private readonly IDaprStateStoreService _daprStateStoreService;
|
||||||
|
|
||||||
public DaprTestController(
|
public DaprTestController(
|
||||||
ILogger<DaprTestController> logger
|
ILogger<DaprTestController> logger,
|
||||||
|
IDaprStateStoreService daprStateStoreService
|
||||||
) {
|
) {
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_daprStateStoreService = daprStateStoreService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Topic("pubsub", "test")]
|
[Topic(DaprPubSubComponents.PubSub, DaprPubSubTopics.Test)]
|
||||||
[HttpPost(Name = "DaprTestSubscriber")]
|
[HttpPost(Name = "DaprTestSubscriber")]
|
||||||
public IActionResult Post([FromBody] string json) {
|
public async Task<IActionResult> Post([FromBody] string json) {
|
||||||
|
|
||||||
var requestData = json.ToObject<TestCommand>();
|
var requestData = json.ToObject<TestCommand>();
|
||||||
if (requestData == null)
|
if (requestData == null)
|
||||||
return BadRequest();
|
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();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,8 @@ builder.Services.AddControllers();
|
|||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
|
||||||
|
builder.Services.RegisterStateStore();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MaksIT.Core" Version="1.1.6" />
|
<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="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,9 @@ services:
|
|||||||
]
|
]
|
||||||
depends_on:
|
depends_on:
|
||||||
- placement
|
- placement
|
||||||
|
- rabbitmq
|
||||||
|
- redis
|
||||||
|
- zipkin
|
||||||
- publisher
|
- publisher
|
||||||
network_mode: "service:publisher" # Attach the publisher-dapr service to the publisher network namespace
|
network_mode: "service:publisher" # Attach the publisher-dapr service to the publisher network namespace
|
||||||
|
|
||||||
@ -53,6 +56,9 @@ services:
|
|||||||
]
|
]
|
||||||
depends_on:
|
depends_on:
|
||||||
- placement
|
- placement
|
||||||
|
- rabbitmq
|
||||||
|
- redis
|
||||||
|
- zipkin
|
||||||
- subscriber
|
- subscriber
|
||||||
network_mode: "service:subscriber" # Attach the subscriber-dapr service to the subscriber network namespace
|
network_mode: "service:subscriber" # Attach the subscriber-dapr service to the subscriber network namespace
|
||||||
|
|
||||||
@ -103,6 +109,6 @@ services:
|
|||||||
|
|
||||||
redis:
|
redis:
|
||||||
ports:
|
ports:
|
||||||
- "6380:6379"
|
- "6379:6379"
|
||||||
networks:
|
networks:
|
||||||
- hello-dapr
|
- hello-dapr
|
||||||
@ -3,6 +3,36 @@ kind: Component
|
|||||||
metadata:
|
metadata:
|
||||||
name: statestore
|
name: statestore
|
||||||
namespace: dapr-test
|
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:
|
spec:
|
||||||
type: state.redis
|
type: state.redis
|
||||||
version: v1
|
version: v1
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user