ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.36k stars 1.64k forks source link

Eureka ServiceDiscovery with a peered environment #826

Closed vuchl closed 9 months ago

vuchl commented 5 years ago

Expected Behavior / New Feature

Ocelot being able to connect to a peered environment of multiple Eureka instances

Actual Behavior / Motivation for New Feature

Ocelot not able to connect and fail over between instances of Eureka

Steps to Reproduce the Problem

  1. run multiple instances of Eureka in a peered mode, e.g. peer 1: docker run --publish 8761:8761 -d steeltoeoss/eureka-server eureka.instance.name=peer1 --eureka.instance.preferIpAddress=true --eureka.client.serviceUrl.defaultZone=http://10.0.0.1:8761/eureka/,http://10.0.0.2:8761/eureka/ peer 2: docker run --publish 8761:8761 -d steeltoeoss/eureka-server eureka.instance.name=peer2 --eureka.instance.preferIpAddress=true --eureka.client.serviceUrl.defaultZone=http://10.0.0.1:8761/eureka/,http://10.0.0.2:8761/eureka/
  2. confirm that the service discovery runs as a replication by looking at the Eureka dashboar at http://10.0.0.1:8761 and http://10.0.0.2:8761
  3. setup Ocelot in the AspNetCore application as per the docs

    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
    
        public static IWebHost BuildWebHost(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://localhost:8099")
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config
                        .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("appsettings.json", true, true)
                        .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true,
                            true)
                        .AddJsonFile("ocelot.json", false, false)
                        .AddEnvironmentVariables();
                })
                .ConfigureServices(s =>
                {
                    s.AddCors();
                    s
                        .AddOcelot()
                        .AddEureka()
                        // .AddCacheManager(x => x.WithDictionaryHandle())
                        ;
                    s.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                })
                .Configure(a =>
                {
                    a.UseOcelot().Wait();
    
                    a.UseCors(b => b
                      .AllowAnyOrigin()
                      .AllowAnyMethod()
                      .AllowAnyHeader()
                      .AllowCredentials()
                    );
                })
                .Build();
        }
    }

ocelot.json

{
  "GlobalConfiguration": {
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration",
    "UseServiceDiscovery" : true,
    "ServiceDiscoveryProvider": { "Type": "Eureka"}
  },
  "ReRoutes": [ ... ]
}

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "spring": {
    "application": {
      "name": "API-Gateway"
    }
  },
  "eureka": {
    "client": {
      "serviceUrl": "http://10.0.0.1:8761/eureka/,http:10.0.0.2:8761/eureka",
      "shouldRegisterWithEureka": false,
      "validateCertificates": false,
      "shouldFetchRegistry":  true
    }
  }
}
  1. How do I setup the code to have a resilliant setup?

Specifications

raman-m commented 9 months ago

If you need this feature, please contribute! We have no time for such development adventures.