-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Kritner/FEATURES/envConfig
Environment config for Orleans
- Loading branch information
Showing
13 changed files
with
300 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Kritner.OrleansGettingStarted.Client/ExtensionMethods/IClientBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Kritner.OrleansGettingStarted.Common/Config/OrleansConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Kritner.OrleansGettingStarted.Common/Helpers/ConsoleAppConfigurator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Kritner.OrleansGettingStarted.Common/Kritner.OrleansGettingStarted.Common.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/ISiloHostBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"OrleansConfig" : { | ||
"NodeIpAddresses" : ["0.0.0.0"], | ||
"GatewayPort" : -1, | ||
"SiloPort" : -1 | ||
} | ||
} |
Oops, something went wrong.