SteeltoeOSS / Steeltoe

.NET Components for Externalized Configuration, Database Connectors, Service Discovery, Logging and Distributed Tracing, Application Management, Security, and more.
https://steeltoe.io
Apache License 2.0
1k stars 164 forks source link

[QUESTION] Configuring Eureka Discovery Client spring app name is not working #1340

Closed NathanStrutz closed 3 weeks ago

NathanStrutz commented 3 weeks ago

Question

Spring:Application:Name looks like it is being ignored in the Eureka discovery client. Unsure if I'm doing something wrong or if the configuration is really just not working.

Background

This is a yarp service that stands as the incoming face of traffic to my other services. It, in turn, is registered in a Eureka reverse proxy to our parent org. I'm replacing the existing "myapp-gateway" with this service, "myapp-reverseproxy" (name changed for professional reasons).

The only real difference I can tell is the Steeltoe version change, and the move from Ocelot to Yarp which is mostly unrelated.

What I've done

appsettings.json current state (snippet):

  "Spring": {
    "Application": {
      "Name": "myapp"
    }
  },
  "eureka": {
    "instance": {
      "securePortEnabled": true
    }
  }

I confirmed this works by Console.WriteLine(Configuration["spring:application:name"]); before and after discovery client initialization. Without any env variables running locally it reads this as "myapp" precisely as expected, however this name is not being picked up by the Eureka client when it's running in TAS.

I added every possible relevant package from the demos and the Steeltoe Initializr into my csproj file:

<PackageReference Include="Steeltoe.Common.Hosting" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Connector.CloudFoundry" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Connector.ConnectorCore" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Discovery.ClientCore" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Discovery.Eureka" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Management.EndpointBase" Version="$(SteeltoeVersion)" />

I tried a variety of ways to initialize the client to register with Eureka; here is the current state:

var builder = WebApplication.CreateBuilder(args)
  .UseCloudHosting()
  .AddCloudFoundryConfiguration()
  .UseCloudHosting();

builder.Services.ConfigureCloudFoundryOptions(builder.Configuration);
builder.Services.AddDiscoveryClient(builder.Configuration);
// etc, add yarp and run

This does successfully register with Eureka, but it uses the application service name, I'm guessing it's the one injected in application_env_json:VCAP_APPLICATION. Once running, it sends continual service registration events to the injected Eureka instance as expected, but the application name is always myapp-reverseproxy and never myapp. Here's an example log message of a successful up message with the wrong service name:

2024-08-16T23:43:43.293-04:00 [APP/PROC/WEB/0] [OUT] Sending HTTP request PUT https://service-registry-00000-random-00000.local-tanzu-company-address.com/eureka/apps/MYAPP-REVERSEPROXY/myapp-reverseproxy.local-tanzu-company-address.com

The Ask

So first off, what am I doing wrong, or is Steeltoe broken?

Second, is there a programmatic way to set this in my Program.cs while the app is starting up so that I can make very sure that it has the correct configuration?

Environment (please complete the following information):

Additional context or links

Add any other context about the problem here.

bart-vmware commented 3 weeks ago

I suspect this is by design. What's in VCAP_SERVICES and VCAP_APPLICATION (running on Cloud Foundry) overrules all other configurations. The reason is so that developers can safely use settings locally, without risking that they are accidentally used in production.

You can force things by adding the "vcap:application:application_name" configuration key.

bart-vmware commented 3 weeks ago

If you're just concerned with the name sent to Eureka, you can set the Eureka:Instance:AppName key, see https://docs.steeltoe.io/api/v3/discovery/netflix-eureka.html.

The key Spring:Application:Name is pretty low priority, used as a fallback if nothing else is available.

NathanStrutz commented 3 weeks ago

Success - Yes! OK the only thing I had to do was this:

Set an environment variable for Eureka:Instance:AppName with the value myapp. The discovery client recognizes this when the service starts up and registers the application with that different name.

Thank you!

NathanStrutz commented 3 weeks ago

BTW I wish this were better documented as a capability. I had a very hard time finding it.