【问题标题】:Using Steeltoe Discovery Client on .NET Core console application (Spring Cloud Config)在 .NET Core 控制台应用程序(Spring Cloud Config)上使用 Steeltoe Discovery Client
【发布时间】:2019-12-04 04:36:14
【问题描述】:

在这个thread 中,我能够使用 ASP.NET CORE 的配置系统设置我的简单控制台应用程序。

代码很简单:

static void Main(string[] args)
{
    string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment}.json", optional: false)
        .AddEnvironmentVariables();

    config = configuration.Build();


    var serviceProvider = new ServiceCollection()
        .AddSingleton<IConfiguration>(config)
        .AddDiscoveryClient(config)
        .BuildServiceProvider();

    Console.WriteLine(config["Test"]);

    Console.Read();
}

但是,由于应用程序不使用 IApplicationBuilder,我无法调用 .UseDiscoveryClient() 方法。我最终在 .AddDiscoveryClient(config) 上收到错误:

“发现客户端类型 UNKNOWN,检查配置”

有解决办法吗?我们想在 Spring Cloud Config 服务器上尝试使用控制台应用程序。如果 Steeltoe 没有办法做到这一点,请随时通知其他有这样做的图书馆。

【问题讨论】:

  • 您是在尝试使用服务发现服务器(Eureka 或 Consul)查找/注册服务,还是从远程服务器(Spring Cloud Config)添加应用配置,还是两者兼而有之?
  • 我正在尝试从 Spring Cloud Config 中提取配置

标签: asp.net-core .net-core configuration steeltoe


【解决方案1】:

扩展方法AddDiscoveryClientUseDiscoveryClient 用于Steeltoe 服务发现。您看到的错误消息是由于 Steeltoe 不知道您的应用程序应该是哪种类型的服务注册表(即:“客户端类型未知”)。

您只希望访问 Spring Cloud Config 服务器,因此您不需要任何一种方法。您可以将 ConfigServerConfigurationProvider 添加到您的配置生成器 with .AddConfigServer

【讨论】:

  • 我没有看到 .AddConfigServer。我有 Steeltoe.Extensions.Configuration 代码,我在 .NET CORE 2.2.0
  • 扩展包包含在Steeltoe.Extensions.Configuration.ConfigServerBase 包中。您应该在 AddEnvironmentVariables() 之前或之后将其添加到链中,具体取决于您是否要允许使用环境变量覆盖值。
猜你喜欢
  • 2021-05-04
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 2023-04-10
  • 2018-04-11
  • 2016-12-07
相关资源
最近更新 更多