cloud-foundry - 无法使用 Steeltoe 中的配置服务器服务从 GIT 存储库中读取属性键值
问题描述
对于 dot net core 2.1 应用程序,我无法从 Steeltoe 中的 git 读取属性值。
从我的客户端应用程序中,我想根据应用程序的环境读取 Git 存储库中存在的不同环境属性文件的属性文件,例如 foo-development.properties/foo-Production.properties。
找到我下面的代码来读取数据
程序.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");
}
}
}
类文件
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 参考和using
声明program.cs
,将“Steeltoe”更改为“Pivotal”,您应该已准备就绪。
通过此更改,您还可以删除.AddCloudFoundry
on WebHostBuilder
,因为 Cloud Foundry Config Provider 的 Pivotal 版本会为您添加它。
推荐阅读
- java - 使用 Kafka Streams 进行 OpenTracing - 如何?
- java - Hibernate 有时需要很长时间才能获得 JDBC 连接
- sql-server - 在 SQL Server 中将垂直表转换为水平表
- debugging - 如何在错误终止之前调试 MPI 程序?
- java - SpringBoot-Hibernate-Mysql id 主键值序列在我的所有域中使用@GeneratedValue 共享
- azure - Azure:从另一个函数应用调用一个函数应用
- logging - 以 JSON 格式制作 Keycloak 日志
- elasticsearch - 按别名删除索引
- regex - 我想在 kibana 中删除/使用正则表达式
- powershell - 为什么要在 PowerShell 中使用有序哈希?