Adnan Mamajiwala
Adnan Mamajiwala

Reputation: 678

Spring cloud gateway unable to resolve service id from eureka server

Followed the spring guides to build the eureka server, spring cloud gateway and a sample rest service.

However, the gateway is unable to retrieve the urls using the service name from the eureka server. The registering of the gateway and service seems to be fine. It works fine when the actual endpoint is provided instead of the service id. I am Unable to understand why the gateway is not resolving the service id from the eureka. Am I missing any configurations ?

Error message:

2018-09-05 23:20:17.751  INFO 47037 --- [ctor-http-nio-2] c.netflix.loadbalancer.BaseLoadBalancer  : Client: localhost instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2018-09-05 23:20:17.756  INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2018-09-05 23:20:17.760  INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client localhost initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@107dc063
2018-09-05 23:20:17.822 ERROR 47037 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/rest-service/hello]

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for localhost
    at org.springframework.cloud.gateway.filter.LoadBalancerClientFilter.filter(LoadBalancerClientFilter.java:72) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.cloud.gateway.handler.FilteringWebHandler$GatewayFilterAdapter.filter(FilteringWebHandler.java:133) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.lambda$filter$0(FilteringWebHandler.java:115) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:45) ~[reactor-core-3.1.8.RELEASE.jar:3.1.8.RELEASE]

Service Registry: enter image description here

Attaching the links to the code:

Upvotes: 6

Views: 8168

Answers (4)

Natnael Kagnaw
Natnael Kagnaw

Reputation: 75

For me using the following beans resolved the issue

@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder() {
   return WebClient.builder();
}

Upvotes: 0

Sasuke Uchiha
Sasuke Uchiha

Reputation: 491

If you were using WebClient to make API calls to other micro-services from the spring cloud gateway-service, make sure that you annotate WebClient.Builder in your configuration class.

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
@Component
public class ContentConfig {

    @Bean
    @LoadBalanced
    public WebClient.Builder loadBalancedWebClientBuilder() {
        return WebClient.builder();
    }
}

And then inside your provider or service class @AutoWired the WebClient bean by,

@Autowired
public WebClient.Builder webClientBuilder;

Upvotes: 1

Sal
Sal

Reputation: 1238

In addition to the accepted answer make sure to add the DiscoveryClient dependency:

10.2 DiscoveryClient Route Definition Locator

The Gateway can be configured to create routes based on services registered with a DiscoveryClient compatible service registry.

To enable this, set spring.cloud.gateway.discovery.locator.enabled=true and make sure a DiscoveryClient implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper).

See 10.2 DiscoveryClient Route Definition Locator

Upvotes: 0

Adnan Mamajiwala
Adnan Mamajiwala

Reputation: 678

The issue was with the way spring cloud gateway accesses the service names from eureka, it was case sensitive. The fix was to add the following properties in application.yml

spring.cloud.gateway.discovery.locator.lower-case-service-id= true
spring.cloud.gateway.discovery.locator.enabled= true

I have created a sample project to show all of them working together.

Upvotes: 7

Related Questions