Browse Source

Added consul configuration

pull/11/head
Başak ERDEM 5 years ago
parent
commit
f9b57aad90
  1. 55
      shared/EShopOnAbp.Shared.Hosting.Microservices/ConsulConfigurationExtensions.cs
  2. 1
      shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj
  3. 3
      shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbpSharedHostingMicroservicesModule.cs

55
shared/EShopOnAbp.Shared.Hosting.Microservices/ConsulConfigurationExtensions.cs

@ -0,0 +1,55 @@
using System;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace EShopOnAbp.Shared.Hosting.Microservices
{
public static class ConsulConfigurationExtensions
{
public static IServiceCollection AddConsulConfig(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IConsulClient>(p => new ConsulClient(consulConfig =>
{
var address = configuration.GetValue<string>("Consul:Host");
consulConfig.Address = new Uri(address);
}));
return services;
}
public static string UseConsul(this IApplicationBuilder app)
{
using (var scope = app.ApplicationServices.CreateScope())
{
var configuration = scope.ServiceProvider.GetService<IConfiguration>();
bool isEnabled = configuration.GetValue<bool>("Consul:Enabled");
string serviceName = configuration.GetValue<string>("Consul:Service");
string host = configuration.GetValue<string>("Consul:Host");
var appString = configuration.GetValue<string>("App:SelfUrl");
Uri appUrl = new Uri(appString, UriKind.Absolute);
if (!isEnabled)
return String.Empty;
Guid serviceId = Guid.NewGuid();
string consulServiceID = $"{serviceName}:{serviceId}";
var client = scope.ServiceProvider.GetService<IConsulClient>();
var consulServiceRistration = new AgentServiceRegistration
{
Name = serviceName,
ID = consulServiceID,
Address = appUrl.Host,
Port = appUrl.Port
};
client.Agent.ServiceRegister(consulServiceRistration);
return consulServiceID;
}
}
}
}

1
shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj

@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="6.0.0-rc.1.21452.15" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0-rc.1.21452.15" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.1" />
<PackageReference Include="Consul.AspNetCore" Version="1.6.10.3" />
</ItemGroup>
<ItemGroup>

3
shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbpSharedHostingMicroservicesModule.cs

@ -2,6 +2,7 @@
using EShopOnAbp.SaasService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.AspNetCore;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using Volo.Abp.AspNetCore.MultiTenancy;
@ -44,6 +45,8 @@ namespace EShopOnAbp.Shared.Hosting.Microservices
context.Services
.AddDataProtection()
.PersistKeysToStackExchangeRedis(redis, "EShopOnAbp-Protection-Keys");
context.Services.AddConsulConfig(configuration);
}
}
}
Loading…
Cancel
Save