无法使用 Steeltoe 中的配置服务器服务从 GIT 存储库读取 属性 键值
Unable to read property key value from GIT repository using Config Server Services in Steeltoe
我无法在点网核心 2.1 应用程序的 steeltoe 中从 git 中读取 属性 值。
从我的客户端应用程序中,我想读取 Git 存储库中的属性文件,以获取不同环境属性文件,如 foo-development.properties/foo-Production.properties 基于应用程序的环境。
找到我下面读取数据的代码
Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseCloudFoundryHosting(5000)
.ConfigureAppConfiguration(b => b.AddConfigServer(new LoggerFactory().AddConsole(LogLevel.Trace)))
.AddCloudFoundry()
.UseUnityServiceProvider()
.UseStartup<Startup>();
启动页
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.ConfigureConfigServerClientOptions(Configuration);
services.AddConfiguration(Configuration);
services.ConfigureCloudFoundryOptions(Configuration);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors();
services.AddDiscoveryClient(Configuration);
//The following setting is tp read config values from json to class
services.Configure<ConfigSettings>(Configuration.GetSection("ConfigSettings"));
// Adds the configuration data POCO configured with data returned from the Spring Cloud Config Server
services.Configure<ConfigServerData>(Configuration);
services.AddScoped<ApiExceptionFilter>();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Alcatraz AddUrlLink Service", Version = "v1" });
});
}
控制器页面
public class HomeController
{
private IOptionsSnapshot<ConfigServerData> IConfigServerData { get; set; }
private CloudFoundryServicesOptions CloudFoundryServices { get; set; }
private CloudFoundryApplicationOptions CloudFoundryApplication { get; set; }
private IConfigurationRoot Config { get; set; }
public HomeController(IOptionsSnapshot<ConfigServerData> configServerData, IConfigurationRoot config,
IOptions<CloudFoundryApplicationOptions> appOptions,
IOptions<CloudFoundryServicesOptions> servOptions)
{
if (configServerData != null)
IConfigServerData = configServerData;
// The ASP.NET DI mechanism injects these as well, see
// public void ConfigureServices(IServiceCollection services) in Startup class
if (servOptions != null)
CloudFoundryServices = servOptions.Value;
if (appOptions != null)
CloudFoundryApplication = appOptions.Value;
_service = service;
Config = config;
CreateConfigServerDataViewData();
}
private void CreateConfigServerDataViewData()
{
Console.WriteLine("Started...");
// IConfigServerData property is set to a IOptionsSnapshot<ConfigServerData> that has been
// initialized with the configuration data returned from the Spring Cloud Config Server
if (IConfigServerData != null && IConfigServerData.Value != null)
{
try
{
if (IConfigServerData != null)
{
Console.WriteLine("Not Null" + IConfigServerData.ToString());
}
}
catch(System.Exception ex) { }
var data = IConfigServerData.Value;
if (IConfigServerData.Value != null)
{
Console.WriteLine("Propertis " + data);
Console.WriteLine("Propertis " + data.foo);
}
Console.WriteLine("foo1 " + Config["Foo"]);
Console.WriteLine("foo2 " + Config["foo"]);
}
else
{
Console.WriteLine("There is no Properties Files available");
}
}
}
Class 个文件
public class ConfigServerData
{
public string Bar { get; set; }
public string foo { get; set; }
// Optional data from vault
public string Vault { get; set; }
}
myclient.properties & myclient-development.properties 文件在 Git 存储库
foo: Test
我已经在定义的 git URL 中提交了这些文件。但我无法在我的客户端应用程序中收到消息 属性。
请提前帮我this.Thanks。
Spring PCF 上的云配置服务器需要授权交互。在 Steeltoe 2.1 版本中,该授权不在 Steeltoe.Extensions.ConfigServer* 中执行,而是在 Pivotal.Extensions.ConfigServer*
中执行
对于您的 ConfigServer NuGet 参考和 program.cs
中的 using
语句,将 "Steeltoe" 更改为 "Pivotal",您应该已经准备就绪。
通过此更改,您还可以删除 WebHostBuilder
上的.AddCloudFoundry
,因为 Cloud Foundry Config Provider 的 Pivotal 版本会为您添加它。
我无法在点网核心 2.1 应用程序的 steeltoe 中从 git 中读取 属性 值。
从我的客户端应用程序中,我想读取 Git 存储库中的属性文件,以获取不同环境属性文件,如 foo-development.properties/foo-Production.properties 基于应用程序的环境。
找到我下面读取数据的代码
Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseCloudFoundryHosting(5000)
.ConfigureAppConfiguration(b => b.AddConfigServer(new LoggerFactory().AddConsole(LogLevel.Trace)))
.AddCloudFoundry()
.UseUnityServiceProvider()
.UseStartup<Startup>();
启动页
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.ConfigureConfigServerClientOptions(Configuration);
services.AddConfiguration(Configuration);
services.ConfigureCloudFoundryOptions(Configuration);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors();
services.AddDiscoveryClient(Configuration);
//The following setting is tp read config values from json to class
services.Configure<ConfigSettings>(Configuration.GetSection("ConfigSettings"));
// Adds the configuration data POCO configured with data returned from the Spring Cloud Config Server
services.Configure<ConfigServerData>(Configuration);
services.AddScoped<ApiExceptionFilter>();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Alcatraz AddUrlLink Service", Version = "v1" });
});
}
控制器页面
public class HomeController
{
private IOptionsSnapshot<ConfigServerData> IConfigServerData { get; set; }
private CloudFoundryServicesOptions CloudFoundryServices { get; set; }
private CloudFoundryApplicationOptions CloudFoundryApplication { get; set; }
private IConfigurationRoot Config { get; set; }
public HomeController(IOptionsSnapshot<ConfigServerData> configServerData, IConfigurationRoot config,
IOptions<CloudFoundryApplicationOptions> appOptions,
IOptions<CloudFoundryServicesOptions> servOptions)
{
if (configServerData != null)
IConfigServerData = configServerData;
// The ASP.NET DI mechanism injects these as well, see
// public void ConfigureServices(IServiceCollection services) in Startup class
if (servOptions != null)
CloudFoundryServices = servOptions.Value;
if (appOptions != null)
CloudFoundryApplication = appOptions.Value;
_service = service;
Config = config;
CreateConfigServerDataViewData();
}
private void CreateConfigServerDataViewData()
{
Console.WriteLine("Started...");
// IConfigServerData property is set to a IOptionsSnapshot<ConfigServerData> that has been
// initialized with the configuration data returned from the Spring Cloud Config Server
if (IConfigServerData != null && IConfigServerData.Value != null)
{
try
{
if (IConfigServerData != null)
{
Console.WriteLine("Not Null" + IConfigServerData.ToString());
}
}
catch(System.Exception ex) { }
var data = IConfigServerData.Value;
if (IConfigServerData.Value != null)
{
Console.WriteLine("Propertis " + data);
Console.WriteLine("Propertis " + data.foo);
}
Console.WriteLine("foo1 " + Config["Foo"]);
Console.WriteLine("foo2 " + Config["foo"]);
}
else
{
Console.WriteLine("There is no Properties Files available");
}
}
}
Class 个文件
public class ConfigServerData
{
public string Bar { get; set; }
public string foo { get; set; }
// Optional data from vault
public string Vault { get; set; }
}
myclient.properties & myclient-development.properties 文件在 Git 存储库
foo: Test
我已经在定义的 git URL 中提交了这些文件。但我无法在我的客户端应用程序中收到消息 属性。
请提前帮我this.Thanks。
Spring PCF 上的云配置服务器需要授权交互。在 Steeltoe 2.1 版本中,该授权不在 Steeltoe.Extensions.ConfigServer* 中执行,而是在 Pivotal.Extensions.ConfigServer*
中执行对于您的 ConfigServer NuGet 参考和 program.cs
中的 using
语句,将 "Steeltoe" 更改为 "Pivotal",您应该已经准备就绪。
通过此更改,您还可以删除 WebHostBuilder
上的.AddCloudFoundry
,因为 Cloud Foundry Config Provider 的 Pivotal 版本会为您添加它。