HyukHyukBoi
HyukHyukBoi

Reputation: 463

Spring Cloud Gateway not able to load balance and gives error 500

I am following the tutorial here: https://www.javainuse.com/spring/cloud-gateway-eureka.

I have 2 applications, accessible through localhost (localhost:8080/employee/messages, localhost:8082/consumer/messages). I also have a service discovery (Spring Cloud Eureka), and Spring Cloud Gateway for load balancing. I have registered all three of these with eureka.enter image description here

Also, I running everything from my local machine, what is host.docker.internal here?

The application.properties file for gateway application looks like this:

server:
  port: 9090

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8083/eureka  

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
      - id: employeeModule
        uri: lb://FIRST-SERVICE
        predicates:
        - Path=/employee/**
      - id: consumerModule
        uri: lb://SECOND-SERVICE
        predicates:
        - Path=/consumer/**

But, whenever I try to access localhost:9090/employee/message, I get error code 500. The error logs in the gateway service look like this:

io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: no further information: host.docker.internal/10.7.250.57:8080
Caused by: java.net.ConnectException: Connection timed out: no further information
        at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
        at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:579) ~[na:na]
        at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:820) ~[na:na]
        at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:327) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:336) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:685) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:632) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:549) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:511) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918) ~[netty-common-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.38.Final.jar!/:4.1.38.Final]
        at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]

Can someone tell me what is the error here.

Edit: I'm behind corporate proxy and not able to do something like: curl host.docker.internal:8080/employee/message

Upvotes: 2

Views: 5600

Answers (2)

Dionys Seidel
Dionys Seidel

Reputation: 39

Shouldn't the Path item under predicates be indented with another tab?

Upvotes: 0

barnwaldo
barnwaldo

Reputation: 504

The message is telling you that you did not connect to the microservice REST endpoint from the gateway. It appears that your URI is not properly specified; suggest to use URL such as uri: http://localhost:8000/employee or something like this. (Works in my application...)

As for load balancing, there is no load balancing anywhere in this post. The Spring Cloud Gateway matches routes on your request attributes. That is what you have tried to facilitate in your yml file. Spring Cloud Gateway does not equal Load Balancing; it is an entry way into microservice architecture.

Load balancing would either be done with Netflix Ribbon (which is apparently being deprecated) or the new Spring Cloud Load Balancer. You are not using either so there is no load balancing. See for example:

https://spring.io/blog/2020/03/25/spring-tips-spring-cloud-loadbalancer

https://spring.io/guides/gs/spring-cloud-loadbalancer/

Moreover, you would need to setup side-by-side apps that can be discovered locally or across networks. And then implement Netflix Ribbon or preferably the new Spring Load Balancer.

I believe that it is possible to setup parallel Spring Cloud Gateways and load balance across these for very high traffic applications. I would prefer that one Gateway is not a bottleneck for high traffic even if it is asynchronous as is the case here.

I have not yet found any examples of load balancing across gateways and would appreciate feedback here. Specifically, is there anything specific to load balancing across gateways that would differ from the Hello apps in the Spring documentation (which is evolving)?

Upvotes: 2

Related Questions