Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Add Dapr state store to Basket API #3

Merged
merged 4 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion deploy/compose/.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

ESHOP_EXTERNAL_DNS_NAME_OR_IP=host.docker.internal
ESHOP_STORAGE_CATALOG_URL=http://host.docker.internal:5202/c/api/v1/catalog/items/[0]/pic/
ESHOP_STORAGE_MARKETING_URL=http://host.docker.internal:5110/api/v1/campaigns/[0]/pic/
ESHOP_STORAGE_MARKETING_URL=http://host.docker.internal:5110/api/v1/campaigns/[0]/pic/
15 changes: 15 additions & 0 deletions deploy/compose/dapr/components/eshop-basket-statestore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: eshop-basket-statestore
spec:
type: state.redis
metadata:
- name: redisHost
value: redis:6379
- name: redisPassword
secretKeyRef:
name: RedisPassword
key: RedisPassword
auth:
secretStore: eshop-secretstore
1 change: 1 addition & 0 deletions deploy/compose/dapr/components/eshop-secretstore.local.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RedisPassword=
7 changes: 7 additions & 0 deletions deploy/compose/dapr/components/eshop-secretstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: eshop-secretstore
spec:
type: eshop-secretstore.local.env
metadata:
18 changes: 8 additions & 10 deletions deploy/compose/docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ services:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80
- ConnectionString=${ESHOP_AZURE_REDIS_BASKET_DB:-redis}
- identityUrl=http://identity-api
- EventBusConnection=${ESHOP_AZURE_SERVICE_BUS:-rabbitmq}
# - IdentityUrlExternal=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5105
- OrchestratorType=${ORCHESTRATOR_TYPE}
- PATH_BASE=/basket-api
- GRPC_PORT=81
Expand All @@ -60,7 +58,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

catalog-api:
environment:
Expand Down Expand Up @@ -97,7 +95,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

identity-api:
environment:
Expand Down Expand Up @@ -158,7 +156,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

ordering-backgroundtasks:
environment:
Expand Down Expand Up @@ -191,7 +189,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

ordering-signalrhub:
environment:
Expand Down Expand Up @@ -219,7 +217,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

payment-api:
environment:
Expand Down Expand Up @@ -248,7 +246,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

webshoppingagg:
environment:
Expand Down Expand Up @@ -281,7 +279,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

webshoppingapigw:
volumes:
Expand All @@ -302,7 +300,7 @@ services:
"-config", "/components/config.yaml"
]
volumes:
- "../../src/components/:/components"
- "./dapr/components/:/components"

webspa:
environment:
Expand Down
18 changes: 9 additions & 9 deletions deploy/compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
- rabbitmq

basket-api-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:basket-api"
depends_on:
- basket-api
Expand All @@ -39,7 +39,7 @@ services:
- sqldata

catalog-api-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:catalog-api"
depends_on:
- catalog-api
Expand All @@ -62,7 +62,7 @@ services:
- sqldata

ordering-api-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:ordering-api"
depends_on:
- ordering-api
Expand All @@ -77,7 +77,7 @@ services:
- sqldata

ordering-backgroundtasks-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:ordering-backgroundtasks"
depends_on:
- ordering-backgroundtasks
Expand All @@ -96,7 +96,7 @@ services:
- sqldata

ordering-signalrhub-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:ordering-signalrhub"
depends_on:
- ordering-signalrhub
Expand All @@ -110,7 +110,7 @@ services:
- rabbitmq

payment-api-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:payment-api"
depends_on:
- payment-api
Expand All @@ -129,7 +129,7 @@ services:
- sqldata

webshoppingagg-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:webshoppingagg"
depends_on:
- webshoppingagg
Expand All @@ -138,7 +138,7 @@ services:
image: envoyproxy/envoy:v1.14.2

webshoppingapigw-dapr:
image: "daprio/daprd:0.9.0"
image: "daprio/daprd:0.10.0"
network_mode: "service:webshoppingapigw"
depends_on:
- webshoppingapigw
Expand All @@ -158,4 +158,4 @@ services:
dockerfile: src/Web/WebStatus/Dockerfile

dapr-placement:
image: "daprio/dapr:0.9.0"
image: "daprio/dapr:0.10.0"
4 changes: 3 additions & 1 deletion src/Services/Basket/Basket.API/Basket.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="3.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.0.2" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Google.Protobuf" Version="3.11.3" />
<PackageReference Include="Dapr.AspNetCore" Version="0.10.0-preview01" />
<PackageReference Include="Dapr.Client" Version="0.10.0-preview01" />
<PackageReference Include="Google.Protobuf" Version="3.12.3" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.27.0" />
<PackageReference Include="Grpc.Tools" Version="2.27.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.12.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,43 @@
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Dapr.Client;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Repositories
{
public class RedisBasketRepository : IBasketRepository
public class DaprBasketRepository : IBasketRepository
{
private readonly ILogger<RedisBasketRepository> _logger;
private readonly ConnectionMultiplexer _redis;
private readonly IDatabase _database;
private const string StoreName = "eshop-basket-statestore";

public RedisBasketRepository(ILoggerFactory loggerFactory, ConnectionMultiplexer redis)
{
_logger = loggerFactory.CreateLogger<RedisBasketRepository>();
_redis = redis;
_database = redis.GetDatabase();
}
private readonly ILogger<DaprBasketRepository> _logger;
private readonly DaprClient _dapr;

public async Task<bool> DeleteBasketAsync(string id)
public DaprBasketRepository(ILoggerFactory loggerFactory, DaprClient dapr)
{
return await _database.KeyDeleteAsync(id);
_logger = loggerFactory.CreateLogger<DaprBasketRepository>();
_dapr = dapr;
}

public IEnumerable<string> GetUsers()
public async Task DeleteBasketAsync(string id)
{
var server = GetServer();
var data = server.Keys();

return data?.Select(k => k.ToString());
await _dapr.DeleteStateAsync(StoreName, id);
}

public async Task<CustomerBasket> GetBasketAsync(string customerId)
{
var data = await _database.StringGetAsync(customerId);

if (data.IsNullOrEmpty)
{
return null;
}

return JsonConvert.DeserializeObject<CustomerBasket>(data);
return await _dapr.GetStateAsync<CustomerBasket>(StoreName, customerId);
}

public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket)
{
var created = await _database.StringSetAsync(basket.BuyerId, JsonConvert.SerializeObject(basket));
var state = await _dapr.GetStateEntryAsync<CustomerBasket>(StoreName, basket.BuyerId);
state.Value = basket;

if (!created)
{
_logger.LogInformation("Problem occur persisting the item.");
return null;
}
await state.SaveAsync();

_logger.LogInformation("Basket item persisted succesfully.");

return await GetBasketAsync(basket.BuyerId);
}

private IServer GetServer()
{
var endpoint = _redis.GetEndPoints();
return _redis.GetServer(endpoint.First());
}
}
}
}

This file was deleted.

This file was deleted.

3 changes: 1 addition & 2 deletions src/Services/Basket/Basket.API/Model/IBasketRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
public interface IBasketRepository
{
Task<CustomerBasket> GetBasketAsync(string customerId);
IEnumerable<string> GetUsers();
Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket);
Task<bool> DeleteBasketAsync(string id);
Task DeleteBasketAsync(string id);
}
}