title: Problem with kubernetes dicovery client in gateway microservice
body: <p>I'm working on a microservices architecture just on spring and kubernetes. Right now I'm working on the gateway microservice. During the startup of the microservice I have the following error at runtime:</p>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <pre><code>2020-01-15 11:00:57.312 ERROR 1 --- [      elastic-4] reactor.core.scheduler.Schedulers        : Scheduler worker in group main failed with an uncaught exception
  
  reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalStateException
  Caused by: java.lang.IllegalStateException: null
      at java.util.Optional.orElseThrow(Optional.java:290) ~[na:1.8.0_212]
      at org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClient.findEndpointPort(KubernetesDiscoveryClient.java:218) ~[spring-cloud-kubernetes-discovery-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
      at org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClient.getNamespaceServiceInstances(KubernetesDiscoveryClient.java:161) ~[spring-cloud-kubernetes-discovery-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
      at org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClient.getInstances(KubernetesDiscoveryClient.java:118) ~[spring-cloud-kubernetes-discovery-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
      at org.springframework.cloud.kubernetes.discovery.reactive.KubernetesReactiveDiscoveryClient.lambda$getInstances$0(KubernetesReactiveDiscoveryClient.java:58) ~[spring-cloud-kubernetes-discovery-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
      at reactor.core.publisher.FluxDefer.subscribe(FluxDefer.java:46) ~[reactor-core-3.3.2.RELEASE.jar!/:3.3.2.RELEASE]
      at reactor.core.publisher.FluxSubscribeOn$SubscribeOnSubscriber.run(FluxSubscribeOn.java:194) ~[reactor-core-3.3.2.RELEASE.jar!/:3.3.2.RELEASE]
      at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84) ~[reactor-core-3.3.2.RELEASE.jar!/:3.3.2.RELEASE]
      at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37) ~[reactor-core-3.3.2.RELEASE.jar!/:3.3.2.RELEASE]
      at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_212]
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_212]
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[na:1.8.0_212]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_212]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_212]
      at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_212]
  
  2020-01-15 11:00:57.481  INFO 1 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 9080
  2020-01-15 11:00:57.485  INFO 1 --- [           main] i.m.gateway.GatewayApplication           : Started GatewayApplication in 7.941 seconds (JVM running for 8.67)
  </code></pre>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>The problem appeared with the move to kubernetes service discovery by adding the spring-cloud-starter-kubernetes dependency. By using another service discovery such as eureka or consul the gateway works correctly. Below is the pom:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;project&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.2.2.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; 
      &lt;/parent&gt;
      &lt;groupId&gt;it.milionaires&lt;/groupId&gt;
      &lt;artifactId&gt;gateway&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;gateway&lt;/name&gt;
      &lt;description&gt;Gateway service&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;spring-cloud.version&gt;Hoxton.SR1&lt;/spring-cloud.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;!--Dipendenza per gateway spring--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-gateway&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-loadbalancer&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.projectreactor&lt;/groupId&gt;
              &lt;artifactId&gt;reactor-core&lt;/artifactId&gt;
              &lt;version&gt;3.3.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-kubernetes&lt;/artifactId&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-cloud-netflix-hystrix&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-cloud-starter-netflix-ribbon&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-config-client&lt;/artifactId&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
                  &lt;version&gt;${spring-cloud.version}&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;com.spotify&lt;/groupId&gt;
                  &lt;artifactId&gt;dockerfile-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.4.13&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;tag-latest&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;build&lt;/goal&gt;
                              &lt;goal&gt;tag&lt;/goal&gt;
                              &lt;goal&gt;push&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;repository&gt;gateway&lt;/repository&gt;
                              &lt;tag&gt;latest&lt;/tag&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                      &lt;execution&gt;
                          &lt;id&gt;tag-version&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;build&lt;/goal&gt;
                              &lt;goal&gt;tag&lt;/goal&gt;
                              &lt;goal&gt;push&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;repository&gt;gateway&lt;/repository&gt;
                              &lt;tag&gt;${project.version}&lt;/tag&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Has anyone encountered this problem or know how to fix it? Thanks in advance
  This is my deployment.yml :</p>
  
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: gateway-deployment
    labels:
      app: gateway
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: gateway
    template:
      metadata:
        labels:
          app: gateway
      spec:
        containers:
          - name: gateway
            image: liviucornel/gateway
            ports:
              - containerPort: 9080
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: gateway
  spec:
    selector:
      app: gateway
    ports:
      - port: 9080
        targetPort: 9080
  #      nodePort: 30010
    type: ClusterIP
  </code></pre>
  
  <p>This is the property file : </p>
  
  <pre><code>server:
    port: 9080
  
  spring:
    application:
      name: gateway 
    profiles:
      active: zone2 
    cloud:
      loadbalancer:
        ribbon:
          enabled: false 
      config:
        uri: http://configuration-server
        failFast: false
        discovery:
          service-id: configuration-server
          enabled: true
        retry:
          maxAttempts: 20
        enabled: true
        request-connect-timeout: 500
        request-read-timeout: 500
      kubernetes:
        discovery:
          service-name: gateway
          primary-port-name: 9080
          enabled: true
          all-namespaces: true
  
      gateway:
        routes:
          - id : servizio1
            uri : lb://servizio1 
            predicates :
              - Path=/servizio1/**
            filters :
              - RewritePath=/servizio1/servizio1/(?&lt;path&gt;.*), /$\{path}
  #            - name : ServiceGatewayPreFilter
  #            - name : ServiceGatewayPostFilter
          - id: servizio2
            uri: lb://servizio2
            predicates:
              - Path=/servizio2/**
            filters:
              - RewritePath=/servizio2/servizio2/(?&lt;path&gt;.*), /$\{path}
        default-filters:
          - name : ServiceGatewayGlobalFilter
        discovery:
          locator:
            enabled: true
  </code></pre>
  
  <p>This is the docker file :</p>
  
  <pre><code>FROM openjdk:8-jdk-alpine
  
  MAINTAINER Liviu &lt;liviucornelciobanu91@gmail.com&gt;
  
  # Add a volume pointing to /tmp
  VOLUME /tmp
  
  # Make port 9080 available to the world outside this container
  EXPOSE 9080
  
  # The application's jar file
  ARG JAR_FILE=target/gateway-0.0.1-SNAPSHOT.jar
  
  # Add the application's jar to the container
  ADD ${JAR_FILE} gateway.jar
  
  
  # Run the jar file
  ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/gateway.jar"]
  </code></pre>