cloud-foundry - 无法使用 Steeltoe 中的配置服务器服务从 GIT 存储库读取属性键值

标签 cloud-foundry asp.net-core-2.1 pcf configserver steeltoe

我无法从 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/

相关文章:

Node.js 应用程序在本地运行,但不能在云中运行

c# - ASP.Net Core 2.1 中间件 WindowsPrincipal ClaimsPrincipal

.net - libuv 与 asp.net core 2.1 中的套接字

java - 已将 Weblogic 证书添加到本地 JDK cacerts。如何在 PCF 中添加证书,以便我可以通过 mulesoft 成功连接到 Weblogic Jms

node.js - Cloud Foundry 中的应用程序到应用程序通信

cloud-foundry - 通配符路由和证书

asp.net - 访问 linux 托管的 ASP.NET Core 2.x webapp(无 nginx)

java - 用Java将100万条记录从XLS文件写入CSV文件

java - 如何使用 PCF 将 QueueManager 作为部分存储库添加到集群?

java - Spring 执行器 : Error configuring CloudFoundryActuator with multiple custom RestTemplateBuilder beans