Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Lesson5 branch #125

Merged
merged 10 commits into from
Aug 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Akka.CQRS
This code sample is part of Petabridge's [Akka.NET, Akka.Cluster, Kubernetes, and Docker Workshop](https://petabridge.com/cluster/) - if you'd like to follow along with the exercises, please go there!

Akka.CQRS is a reference architecture for [Akka.NET](https://getakka.net/), intended to illustrate the following Akka.NET techniques and principles:

1. [Command-Query Responsibility Segregation](https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs) - the Akka.NET actors who consume write events use distinctly different interfaces from those who consume read events
Expand Down
16 changes: 12 additions & 4 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Target "Clean" (fun _ ->
CleanDir outputPerfTests
CleanDir outputNuGet
CleanDir "docs/_site"
CleanDirs !! "./**/bin"
CleanDirs !! "./**/obj"
)

Target "AssemblyInfo" (fun _ ->
Expand Down Expand Up @@ -164,7 +166,7 @@ Target "Protobuf" <| fun _ ->

let result = ExecProcess(fun info ->
info.FileName <- protocPath
info.WorkingDirectory <- (Path.GetDirectoryName (FullName protocPath))
info.WorkingDirectory <- (Path.GetDirectoryName (FullName protocPath):string)
info.Arguments <- args) (System.TimeSpan.FromMinutes 45.0) (* Reasonably long-running task. *)
if result <> 0 then failwithf "protoc failed. %s %s" protocPath args

Expand Down Expand Up @@ -274,6 +276,7 @@ Target "PublishNuget" (fun _ ->
// Docker images
//--------------------------------------------------------------------------------
Target "PublishCode" (fun _ ->
ActivateFinalTarget "KillCreatedProcesses"
let projects = !! "src/**/*.Service.csproj" // publish services and web only
++ "src/**/*.Web.csproj"

Expand All @@ -284,7 +287,6 @@ Target "PublishCode" (fun _ ->
Project = project
Configuration = configuration
VersionSuffix = overrideVersionSuffix project
AdditionalArgs = ["--no-restore --output bin/Release/netcoreapp2.1/publish"] // would be ideal to change publish dir via MSBuild
})

projects |> Seq.iter (runSingleProject)
Expand All @@ -305,6 +307,9 @@ Target "BuildDockerImages" (fun _ ->

let remoteRegistryUrl = getBuildParamOrDefault "remoteRegistry" ""

let composedGetFileNameWithoutExtension (p:string) =
System.IO.Path.GetFileNameWithoutExtension p

let buildDockerImage imageName projectPath =

let args =
Expand All @@ -331,13 +336,16 @@ Target "BuildDockerImages" (fun _ ->
|> append "."
|> toText

let composedGetDirName (p:string) =
System.IO.Path.GetDirectoryName p

ExecProcess(fun info ->
info.FileName <- "docker"
info.WorkingDirectory <- Path.GetDirectoryName projectPath
info.WorkingDirectory <- composedGetDirName projectPath
info.Arguments <- args) (System.TimeSpan.FromMinutes 5.0) (* Reasonably long-running task. *)

let runSingleProject project =
let projectName = Path.GetFileNameWithoutExtension project
let projectName = composedGetFileNameWithoutExtension project
let imageName = mapDockerImageName projectName
let result = match imageName with
| None -> 0
Expand Down
11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ if [ ! -f "$FAKE_EXE" ]; then
exit 1
fi

###########################################################################
# INSTALL Protobuf
###########################################################################
if [ ! -f "$PROTOBUF_EXE" ]; then
mono "$NUGET_EXE" install Google.Protobuf.Tools -ExcludeVersion -Version $PROTOBUF_VERSION -OutputDirectory "$TOOLS_DIR"
if [ $? -ne 0 ]; then
echo "An error occured while installing Google.Protobuf.Tools."
exit 1
fi
fi

###########################################################################
# INSTALL DOCFX
###########################################################################
Expand Down
10 changes: 10 additions & 0 deletions deployK8sServices.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# deploys all Kubernetes services

find ./k8s -name "*.yaml" | while read fname; do
echo "Deploying $fname"
kubectl apply -f "$fname"

echo "Waiting 10s before start of next deployment."
sleep 10
done
4 changes: 4 additions & 0 deletions docker-images.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
akka.cqrs.tradeprocessor
akka.cqrs.traders
akka.cqrs.pricing
akka.cqrs.pricing.web
5 changes: 4 additions & 1 deletion k8s/mongodb-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ spec:
selector:
app: mongodb
---
apiVersion: apps/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: akka-cqrs
name: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
Expand Down
2 changes: 1 addition & 1 deletion k8s/pricing-web-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ spec:
httpGet:
path: "/"
port: 80
initialDelaySeconds: 5
initialDelaySeconds: 30
ports:
- containerPort: 16666
protocol: TCP
Expand Down
6 changes: 3 additions & 3 deletions src/Akka.CQRS.Infrastructure/Akka.CQRS.Infrastructure.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>$(NetStandardVersion)</TargetFramework>
<Description>Shared, non-domain-specific infrastructure used by various Akka.CQRS services.</Description>
<Configurations>Debug;Release;Phobos</Configurations>
</PropertyGroup>
Expand All @@ -15,13 +15,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Akka.Cluster.Sharding" Version="$(AkkaVersion)-beta*" />
<PackageReference Include="Akka.Cluster.Sharding" Version="$(AkkaVersion)" />
<PackageReference Include="Akka.Persistence.Extras" Version="$(AkkaPersistenceExtrasVersion)" />
<PackageReference Include="Akka.Bootstrap.Docker" Version="$(AkkaBootstrapVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)' == 'Phobos'">
<!-- Uncomment these to install Phobos binaries -->
<!-- Uncomment these to install Phobos binaries -->
<!--<PackageReference Include="Phobos.Actor.Cluster" Version="$(PhobosVersion)" />
<PackageReference Include="Phobos.Tracing.Jaeger" Version="$(PhobosTracingVersion)" />
<PackageReference Include="Phobos.Monitoring.StatsD" Version="$(PhobosMonitoringVersion)" />-->
Expand Down
14 changes: 1 addition & 13 deletions src/Akka.CQRS.Infrastructure/StockShardMsgRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,7 @@ public override string EntityId(object message)
{
return envelope.Message.StockId;
}

switch (message)
{
case ConfirmableMessage<Ask> a:
return a.Message.StockId;
case ConfirmableMessage<Bid> b:
return b.Message.StockId;
case ConfirmableMessage<Fill> f:
return f.Message.StockId;
case ConfirmableMessage<Match> m:
return m.Message.StockId;
}


return null;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Akka.CQRS.Pricing.Actors/MatchAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ private void Recovers()
/// Recovery has completed successfully.
/// </summary>
protected override void OnReplaySuccess()
{
_publishPricesTask = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(10), Self, PublishEvents.Instance, ActorRefs.NoSender);
{

// setup subscription to TradeEventPublisher
Self.Tell(DoSubscribe.Instance);
Expand Down Expand Up @@ -263,6 +261,9 @@ private void Commands()

protected override void PreStart()
{
_publishPricesTask = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(10), Self, PublishEvents.Instance, ActorRefs.NoSender);

_log.Info("Starting...");
base.PreStart();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Akka.CQRS.Pricing.Cli/Akka.CQRS.Pricing.Cli.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>$(NetStandardVersion)</TargetFramework>
<Description>Petabridge.Cmd palettes for accessing the pricing system.</Description>
<Configurations>Debug;Release;Phobos</Configurations>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Petabridge.Cmd.Host" Version="0.6.1" />
<PackageReference Include="Petabridge.Cmd.Host" Version="$(PetabridgeCmdVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Phobos|AnyCPU'">
<OutputPath>bin\Release\$(NetCoreVersion)\</OutputPath>
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>

<ItemGroup>
Expand All @@ -17,10 +17,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Akka.HealthCheck.Persistence" Version="0.2.1" />
<PackageReference Include="Akka.Persistence.MongoDb" Version="1.3.12" />
<PackageReference Include="Akka.HealthCheck.Persistence" Version="0.3.0" />
<PackageReference Include="Akka.Persistence.MongoDb" Version="1.4.5" />
<PackageReference Include="Petabridge.Cmd.Cluster" Version="$(PetabridgeCmdVersion)" />
<PackageReference Include="Petabridge.Cmd.Cluster.Sharding" Version="$(PetabridgeCmdVersion)-beta" />
<PackageReference Include="Petabridge.Cmd.Cluster.Sharding" Version="$(PetabridgeCmdVersion)" />
<PackageReference Include="Petabridge.Cmd.Remote" Version="$(PetabridgeCmdVersion)" />
</ItemGroup>

Expand Down
14 changes: 10 additions & 4 deletions src/Akka.CQRS.Pricing.Service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ ENV CLUSTER_IP ""
ENV CLUSTER_PORT "6055"
ENV MONGO_CONNECTION_STR "" #MongoDb connection string for Akka.Persistence

COPY ./bin/Release/netcoreapp2.1/publish/ /app

# 9110 - Petabridge.Cmd
# 6055 - Akka.Cluster
EXPOSE 9110 6055

# Install Petabridge.Cmd client
RUN dotnet tool install --global pbm

COPY ./bin/Release/netcoreapp2.1/publish/ /app

FROM microsoft/dotnet:2.1-runtime AS app
WORKDIR /app

COPY --from=base /app /app

# copy .NET Core global tool
COPY --from=base /root/.dotnet /root/.dotnet/

# Needed because https://stackoverflow.com/questions/51977474/install-dotnet-core-tool-dockerfile
ENV PATH="${PATH}:/root/.dotnet/tools"

# RUN pbm help

CMD ["dotnet", "Akka.CQRS.Pricing.Service.dll"]
4 changes: 4 additions & 0 deletions src/Akka.CQRS.Pricing.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ static int Main(string[] args)

var sharding = ClusterSharding.Get(actorSystem);


var shardRegion = sharding.Start("priceAggregator",
s => Props.Create(() => new MatchAggregator(s)),
ClusterShardingSettings.Create(actorSystem),
new StockShardMsgRouter());

var priceInitiatorActor = actorSystem.ActorOf(ClusterSingletonManager.Props(Props.Create(() => new PriceInitiatorActor(shardRegion)),
ClusterSingletonManagerSettings.Create(actorSystem).WithRole("pricing-engine").WithSingletonName("priceInitiator")), "priceInitiator");

var clientHandler =
actorSystem.ActorOf(Props.Create(() => new ClientHandlerActor(shardRegion)), "subscriptions");

Expand Down
4 changes: 2 additions & 2 deletions src/Akka.CQRS.Pricing.Web/Akka.CQRS.Pricing.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Configurations>Debug;Release;Phobos</Configurations>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Phobos|AnyCPU'">
<OutputPath>bin\Release\$(NetCoreVersion)\</OutputPath>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Phobos|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Akka.CQRS.Pricing.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class HomeController : Controller
{
public IActionResult Index()
{
ViewData["AppVersion"] = typeof(HomeController).Assembly.ImageRuntimeVersion;
return View();
}

Expand Down
12 changes: 10 additions & 2 deletions src/Akka.CQRS.Pricing.Web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ EXPOSE 16666
# Install Petabridge.Cmd client
RUN dotnet tool install --global pbm

COPY ./bin/Release/netcoreapp2.1/publish/ /app

FROM mcr.microsoft.com/dotnet/core/aspnet:2.1 AS app
WORKDIR /app

COPY --from=base /app /app

# copy .NET Core global tool
COPY --from=base /root/.dotnet /root/.dotnet/

# Needed because https://stackoverflow.com/questions/51977474/install-dotnet-core-tool-dockerfile
ENV PATH="${PATH}:/root/.dotnet/tools"

# RUN pbm help

COPY ./bin/Release/netcoreapp2.1/publish/ /app

CMD ["dotnet", "Akka.CQRS.Pricing.Web.dll"]
2 changes: 1 addition & 1 deletion src/Akka.CQRS.Pricing.Web/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@RenderBody()
<hr />
<footer>
<p>&copy; 2019 - Akka.CQRS.Pricing.Web</p>
<p>&copy; 2019 - @DateTime.UtcNow.Year Akka.CQRS.Pricing.Web v @ViewData["AppVersion"]</p>
</footer>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Configurations>Debug;Release;Phobos</Configurations>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Phobos|AnyCPU'">
<OutputPath>bin\Release\$(NetCoreVersion)\</OutputPath>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Phobos|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,7 +18,7 @@

<ItemGroup>
<PackageReference Include="Petabridge.Cmd.Cluster" Version="$(PetabridgeCmdVersion)" />
<PackageReference Include="Petabridge.Cmd.Cluster.Sharding" Version="$(PetabridgeCmdVersion)-beta" />
<PackageReference Include="Petabridge.Cmd.Cluster.Sharding" Version="$(PetabridgeCmdVersion)" />
<PackageReference Include="Petabridge.Cmd.Remote" Version="$(PetabridgeCmdVersion)" />
</ItemGroup>

Expand Down
12 changes: 10 additions & 2 deletions src/Akka.CQRS.TradePlacers.Service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ ENV CLUSTER_SEEDS "[]"
ENV CLUSTER_IP ""
ENV CLUSTER_PORT "5054"

COPY ./bin/Release/netcoreapp2.1/publish/ /app

# 9110 - Petabridge.Cmd
# 5055 - Akka.Cluster
EXPOSE 9110 5054

# Install Petabridge.Cmd client
RUN dotnet tool install --global pbm

COPY ./bin/Release/netcoreapp2.1/publish/ /app

FROM microsoft/dotnet:2.1-runtime AS app
WORKDIR /app

COPY --from=base /app /app

# copy .NET Core global tool
COPY --from=base /root/.dotnet /root/.dotnet/

# Needed because https://stackoverflow.com/questions/51977474/install-dotnet-core-tool-dockerfile
ENV PATH="${PATH}:/root/.dotnet/tools"

Expand Down
Loading