Search code examples
spring-bootmicroservicesdocker-swarmnetflix-eurekaservice-discovery

Docker swarm springboot and eureka service discoverer not working


Currently working on swarmifying our Springboot microservice back-end with eureka service discoverer. The first problem was making sure the service discoverer doesn't pick de ingress IP-adress but instead IP-address from the overlay network. After some searching I found a post that suggest the following Eureka Client Configuration:

@Configuration
@EnableConfigurationProperties
public class EurekaClientConfig {

    private ConfigurableEnvironment env;

    public EurekaClientConfig(final ConfigurableEnvironment env) {
        this.env = env;
    }

    @Bean
    @Primary
    public EurekaInstanceConfigBean eurekaInstanceConfigBean(final InetUtils inetUtils) throws IOException {
        final String hostName = System.getenv("HOSTNAME");
        String hostAddress = null;

        final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netInt : Collections.list(networkInterfaces)) {
            for (InetAddress inetAddress : Collections.list(netInt.getInetAddresses())) {
                if (hostName.equals(inetAddress.getHostName())) {
                    hostAddress = inetAddress.getHostAddress();
                    System.out.printf("Inet used: %s", netInt.getName());
                }

                System.out.printf("Inet %s: %s / %s\n", netInt.getName(),  inetAddress.getHostName(), inetAddress.getHostAddress());
            }
        }
        if (hostAddress == null) {
            throw new UnknownHostException("Cannot find ip address for hostname: " + hostName);
        }

        final int nonSecurePort = Integer.valueOf(env.getProperty("server.port", env.getProperty("port", "8080")));

        final EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
        instance.setHostname(hostName);
        instance.setIpAddress(hostAddress);
        instance.setNonSecurePort(nonSecurePort);
        System.out.println(instance);
        return instance;
    }
}

After deploying the new discoverer I got the correct result and the service discoverer had the correct overlay IP-address.

In order to understand the next step here is some information about the environment we run this docker swarm on. We currently have 2 droplets one for development and the other for production. Currently we are only working on the development server to Swarmify it. The production hasn't been touched in months.

The next step is to deploy a Discovery Client Springboot application that will connect to the correct service discoverer and also has the overly IP-address instead of the ingress. But when I build the application it always connects to our production service discoverer outside the docker swarm into the other droplet. I can see the application being deployed on the swarm but looking at the Eureka dashboard from the production server I can see that it connects to it.

The second problem is that the application also has the EurekaClient config you see above but it is ignored. Even the logs within the method is not called when starting up the applicaiton.

Here is the configuration from the Discovery Client application:

eureka:
  client:
    serviceUrl:
      defaultZone: service-discovery_service:8761/eureka
    enabled: false
  instance:
    instance-id: ${spring.application.name}:${random.value}
    prefer-ip-address: true

spring:
  application:
    name: account-service

I assume that you can use defaultZone to point at the correct service discoverer but I can be wrong.


Solution

  • Just dont use an eureka service discoverer but something else like treafik. Much easier solution.