我无法从 Steeltoe 中的 git 读取 dot net core 2.1 应用程序的属性值。
从我的客户端应用程序中,我想读取 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" });
});
}
Controller 页面
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");
}
}
}
类文件
public class ConfigServerData
{
public string Bar { get; set; }
public string foo { get; set; }
// Optional data from vault
public string Vault { get; set; }
}
Git 存储库中的 myclient.properties 和 myclient-development.properties 文件
foo: Test
我已将这些文件提交到定义的 git URL 中。但我无法在客户端应用程序中获取消息属性。
请帮我解决这个问题。提前致谢。
最佳答案
PCF 上的 Spring Cloud Config Server 需要授权交互。在 Steeltoe 2.1 版本中,授权不在 Steeltoe.Extensions.ConfigServer* 中执行,而是在 Pivotal.Extensions.ConfigServer* 中执行
对于您的 ConfigServer NuGet 引用和 program.cs
中的 using
语句,将“Steeltoe”更改为“Pivotal”,您应该已准备就绪。
通过此更改,您还可以删除 WebHostBuilder
上的.AddCloudFoundry
,因为 Cloud Foundry Config Provider 的 Pivotal 版本将为您添加该内容。
关于cloud-foundry - 无法使用 Steeltoe 中的配置服务器服务从 GIT 存储库读取属性键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316223/