Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud API Gateway routing not working

I have designed a micro service prototype using below technologies

  1. Eureka Server
  2. a service
  3. Spring Cloud API Gateway

Above mentioned service are registered in the Eureka Server

enter image description here

API Gateway routing Configuration

server.port=8080
eureka.client.serviceUrl.defaultZone = http://localhost:8083/eureka
spring.application.name=ApiGateway
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

spring.cloud.gateway.routes[0].id=service1
spring.cloud.gateway.routes[0].uri=lb://MICROSERVICE1
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**

The service Configuration

server.port=8081
server.address=127.0.0.1
eureka.client.serviceUrl.defaultZone = http://localhost:8083/eureka
spring.application.name=MicroService1
error.whitelabel.enabled= false

Controller

@RestController
@RequestMapping("/service1")
public class HomeController {
    @GetMapping("/message")
    public String hello() {
        return "response from micro service1";
    }

}

When I send a request to the gateway it's showing the below error

2020-12-16 22:26:09.770 ERROR 16700 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [d3334561-1]  500 Server Error for HTTP GET "/service1/message"

java.net.UnknownHostException: failed to resolve 'LAPTOP-KU56B6A8' after 3 queries 
    at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013) ~[netty-resolver-dns-4.1.55.Final.jar:4.1.55.Final]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP GET "/service1/message" [ExceptionHandlingWebHandler]

How can we solve the above issue?

like image 605
arj Avatar asked May 05 '26 23:05

arj


2 Answers

Add eureka.instance.hostname=localhost in both the microservices instances this will work and not give an error

like image 192
Shubham Chinchure Avatar answered May 08 '26 12:05

Shubham Chinchure


Check your pom.xml file, depending on the version of the dependencies with which the project got initialized there is a slight chance that you have :

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
</dependency>

instead of :

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

notice that in the first it is using spring-cloud-starter-gateway-mvc instead of the bare spring-cloud-starter-gateway

correct it and hopefully it might fix it for you.

like image 32
Yehdhih ANNA Avatar answered May 08 '26 12:05

Yehdhih ANNA