2

对于 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 中提交了这些文件。但我无法在我的客户端应用程序中获取消息属性。

请帮助我。在此先感谢。

4

1 回答 1

2

PCF 上的 Spring Cloud Config Server 需要授权交互。在 Steeltoe 2.1 版中,该授权不在 Steeltoe.Extensions.ConfigServer* 中执行,而是在 Pivotal.Extensions.ConfigServer* 中执行

对于 ConfigServer NuGet 参考和using声明program.cs,将“Steeltoe”更改为“Pivotal”,您应该已准备就绪。

通过此更改,您还可以删除.AddCloudFoundryon WebHostBuilder,因为 Cloud Foundry Config Provider 的 Pivotal 版本会为您添加它。

于 2019-01-25T12:11:45.413 回答