Skip to content

Commit

Permalink
Merge pull request #10 from Kritner/FEATURES/envConfig
Browse files Browse the repository at this point in the history
Environment config for Orleans
  • Loading branch information
Kritner authored Dec 20, 2018
2 parents 22b9275 + 23a66b0 commit 5d3ea5f
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 4 deletions.
7 changes: 7 additions & 0 deletions OrleansGettingStarted.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kritner.OrleansGettingStart
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kritner.OrleansGettingStarted.SiloHost", "src\Kritner.OrleansGettingStarted.SiloHost\Kritner.OrleansGettingStarted.SiloHost.csproj", "{60C38B38-65C6-4F8F-AF7B-37ACC1D8E4FB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kritner.OrleansGettingStarted.Common", "src\Kritner.OrleansGettingStarted.Common\Kritner.OrleansGettingStarted.Common.csproj", "{E7346FF3-2DB9-430A-8A6E-7949A8B470B2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,6 +37,10 @@ Global
{60C38B38-65C6-4F8F-AF7B-37ACC1D8E4FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60C38B38-65C6-4F8F-AF7B-37ACC1D8E4FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60C38B38-65C6-4F8F-AF7B-37ACC1D8E4FB}.Release|Any CPU.Build.0 = Release|Any CPU
{E7346FF3-2DB9-430A-8A6E-7949A8B470B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7346FF3-2DB9-430A-8A6E-7949A8B470B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7346FF3-2DB9-430A-8A6E-7949A8B470B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7346FF3-2DB9-430A-8A6E-7949A8B470B2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -44,6 +50,7 @@ Global
{A2997108-2330-4544-B3B0-4EF2B4537E14} = {E9107609-5D2A-4988-81F6-EDF031856CD0}
{93D88312-0369-4858-8F2E-1A0F11FCB224} = {E9107609-5D2A-4988-81F6-EDF031856CD0}
{60C38B38-65C6-4F8F-AF7B-37ACC1D8E4FB} = {E9107609-5D2A-4988-81F6-EDF031856CD0}
{E7346FF3-2DB9-430A-8A6E-7949A8B470B2} = {E9107609-5D2A-4988-81F6-EDF031856CD0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A3713286-1C26-4298-9843-247ACCE599CC}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Kritner.OrleansGettingStarted.Common.Config;
using Microsoft.Extensions.Options;
using Orleans;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Kritner.OrleansGettingStarted.Client.ExtensionMethods
{
public static class IClientBuilderExtensions
{
/// <summary>
/// Configures clustering for the Orleans Client based on
/// the Orleans environment.
/// </summary>
/// <param name="builder">The client builder.</param>
/// <param name="orleansConfigOptions">The Orleans configuration options.</param>
/// <param name="environmentName">The environment.</param>
public static IClientBuilder ConfigureClustering(
this IClientBuilder builder,
IOptions<OrleansConfig> orleansConfigOptions,
string environmentName
)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (orleansConfigOptions.Value == default(OrleansConfig))
{
throw new ArgumentException(nameof(orleansConfigOptions));
}

switch (environmentName.ToLower())
{
case "dev":
builder.UseLocalhostClustering();
break;
default:
var orleansConfig = orleansConfigOptions.Value;
List<IPEndPoint> nodes = new List<IPEndPoint>();
foreach (var node in orleansConfig.NodeIpAddresses)
{
nodes.Add(new IPEndPoint(IPAddress.Parse(node), orleansConfig.GatewayPort));
}
builder.UseStaticClustering(nodes.ToArray());
break;
}

return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.0" />
<PackageReference Include="Microsoft.Orleans.Client" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kritner.OrleansGettingStarted.Common\Kritner.OrleansGettingStarted.Common.csproj" />
<ProjectReference Include="..\Kritner.OrleansGettingStarted.GrainInterfaces\Kritner.OrleansGettingStarted.GrainInterfaces.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="..\_appsettings\appsettings.json" Link="appsettings.json" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
<Content Include="..\_appsettings\appsettings.dev.json" Link="appsettings.dev.json" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

</Project>
19 changes: 17 additions & 2 deletions src/Kritner.OrleansGettingStarted.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
using Kritner.OrleansGettingStarted.Client.OrleansFunctionExamples;
using Kritner.OrleansGettingStarted.Common.Config;
using Kritner.OrleansGettingStarted.Common.Helpers;
using Kritner.OrleansGettingStarted.GrainInterfaces;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Orleans;
using Orleans.Configuration;
using Orleans.Runtime;
using System;
using System.Threading.Tasks;
using Kritner.OrleansGettingStarted.Common;
using Kritner.OrleansGettingStarted.Client.ExtensionMethods;

namespace Kritner.OrleansGettingStarted.Client
{
public class Program
{
private static Startup Startup;
private static IServiceProvider ServiceProvider;
const int initializeAttemptsBeforeFailing = 5;
private static int attempt = 0;

static int Main(string[] args)
{
Startup = ConsoleAppConfigurator.BootstrapApp();
var serviceCollection = new ServiceCollection();
Startup.ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();

return RunMainAsync().Result;
}

Expand Down Expand Up @@ -44,7 +56,10 @@ private static async Task<IClusterClient> StartClientWithRetries()
attempt = 0;
IClusterClient client;
client = new ClientBuilder()
.UseLocalhostClustering()
.ConfigureClustering(
ServiceProvider.GetService<IOptions<OrleansConfig>>(),
Startup.HostingEnvironment.EnvironmentName
)
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
Expand Down
25 changes: 25 additions & 0 deletions src/Kritner.OrleansGettingStarted.Common/Config/OrleansConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Kritner.OrleansGettingStarted.Common.Config
{
/// <summary>
/// Contains properties utilized for configuration Orleans
/// Clients and Cluster Nodes.
/// </summary>
public class OrleansConfig
{
/// <summary>
/// The IP addresses that will be utilized in the cluster.
/// First IP address is the primary.
/// </summary>
public string[] NodeIpAddresses { get; set; }
/// <summary>
/// The port used for Client to Server communication.
/// </summary>
public int GatewayPort { get; set; }
/// <summary>
/// The port for Silo to Silo communication
/// </summary>
public int SiloPort { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using System;
using System.IO;

namespace Kritner.OrleansGettingStarted.Common.Helpers
{
public static class ConsoleAppConfigurator
{
public static Startup BootstrapApp()
{
var environment = GetEnvironment();
var hostingEnvironment = GetHostingEnvironment(environment);
var configurationBuilder = CreateConfigurationBuilder(environment);

return new Startup(hostingEnvironment, configurationBuilder);
}

private static string GetEnvironment()
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.IsNullOrEmpty(environmentName))
{
return "dev";
}

return environmentName;
}

private static IHostingEnvironment GetHostingEnvironment(string environmentName)
{
return new HostingEnvironment
{
EnvironmentName = environmentName,
ApplicationName = AppDomain.CurrentDomain.FriendlyName,
ContentRootPath = AppDomain.CurrentDomain.BaseDirectory,
ContentRootFileProvider = new PhysicalFileProvider(AppDomain.CurrentDomain.BaseDirectory)
};
}

private static IConfigurationBuilder CreateConfigurationBuilder(string environmentName)
{
var config = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();

return config;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions src/Kritner.OrleansGettingStarted.Common/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Kritner.OrleansGettingStarted.Common.Config;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Kritner.OrleansGettingStarted.Common
{
public class Startup
{
public IHostingEnvironment HostingEnvironment { get; }
public IConfiguration Configuration { get; }

public Startup(
IHostingEnvironment hostingEnvironment,
IConfigurationBuilder configurationBuilder
)
{
HostingEnvironment = hostingEnvironment;
Configuration = configurationBuilder.Build();
}

public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddOptions();
serviceCollection.Configure<OrleansConfig>(Configuration.GetSection(nameof(OrleansConfig)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Kritner.OrleansGettingStarted.Common.Config;
using Microsoft.Extensions.Options;
using Orleans.Hosting;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Kritner.OrleansGettingStarted.SiloHost.ExtensionMethods
{
public static class ISiloHostBuilderExtensions
{
/// <summary>
/// Configures clustering for the Orleans Silo Host based on
/// the Orleans environment.
/// </summary>
/// <param name="builder">The silo host builder.</param>
/// <param name="orleansConfigOptions">The Orleans configuration options.</param>
/// <param name="environmentName">The environment.</param>
public static ISiloHostBuilder ConfigureClustering(
this ISiloHostBuilder builder,
IOptions<OrleansConfig> orleansConfigOptions,
string environmentName
)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (orleansConfigOptions.Value == default(OrleansConfig))
{
throw new ArgumentException(nameof(orleansConfigOptions));
}

switch (environmentName.ToLower())
{
case "dev":
builder.UseLocalhostClustering();
break;
default:
var orleansConfig = orleansConfigOptions.Value;
builder.UseDevelopmentClustering(
new IPEndPoint(
IPAddress.Parse(orleansConfig.NodeIpAddresses[0]),
orleansConfig.SiloPort
)
);
break;
}

return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kritner.OrleansGettingStarted.Common\Kritner.OrleansGettingStarted.Common.csproj" />
<ProjectReference Include="..\Kritner.OrleansGettingStarted.GrainInterfaces\Kritner.OrleansGettingStarted.GrainInterfaces.csproj" />
<ProjectReference Include="..\Kritner.OrleansGettingStarted.Grains\Kritner.OrleansGettingStarted.Grains.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="..\_appsettings\appsettings.json" Link="appsettings.json" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
<Content Include="..\_appsettings\appsettings.dev.json" Link="appsettings.dev.json" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

</Project>
19 changes: 17 additions & 2 deletions src/Kritner.OrleansGettingStarted.SiloHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Kritner.OrleansGettingStarted.GrainInterfaces;
using Kritner.OrleansGettingStarted.Common;
using Kritner.OrleansGettingStarted.Common.Config;
using Kritner.OrleansGettingStarted.Common.Helpers;
using Kritner.OrleansGettingStarted.Grains;
using Kritner.OrleansGettingStarted.SiloHost.ExtensionMethods;
using Kritner.OrleansGettingStarted.SiloHost.Helpers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
Expand All @@ -14,8 +18,16 @@ namespace Kritner.OrleansGettingStarted.SiloHost
{
public class Program
{
private static Startup Startup;
private static IServiceProvider ServiceProvider;

public static int Main(string[] args)
{
Startup = ConsoleAppConfigurator.BootstrapApp();
var serviceCollection = new ServiceCollection();
Startup.ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();

return RunMainAsync().Result;
}

Expand All @@ -42,7 +54,10 @@ private static async Task<ISiloHost> StartSilo()
{
// define the cluster configuration
var builder = new SiloHostBuilder()
.UseLocalhostClustering()
.ConfigureClustering(
ServiceProvider.GetService<IOptions<OrleansConfig>>(),
Startup.HostingEnvironment.EnvironmentName
)
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
Expand Down
7 changes: 7 additions & 0 deletions src/_appsettings/appsettings.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"OrleansConfig" : {
"NodeIpAddresses" : ["0.0.0.0"],
"GatewayPort" : -1,
"SiloPort" : -1
}
}
Loading

0 comments on commit 5d3ea5f

Please sign in to comment.