2
votes

I have design a micro service prototype using below technologies

  1. Eureka Server(Discovery Server)
  2. Spring boot (back-end Service)
  3. Spring Cloud API GateWay

Above mentioned service are getting up,ApI Gateway and back end services are registered in the Eureka Server

enter image description here

API Gate Way 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/**

Micro 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 gate way its showing 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 we can solve the above issue.

9
Add : eureka.instance.prefer-ip-address=true to application.properties file to all your microservices & api-gateway - Vipul Gupta

9 Answers

4
votes

i have modified the API Gate Way routing Configuration like below

spring.cloud.gateway.routes[0].id=service1
spring.cloud.gateway.routes[0].uri=http://localhost:8081/service1/
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**

Now is working fine

2
votes

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

0
votes

hello jebji if you still have this problem add spring.cloud.discovery.enabled=true in application.properties

0
votes

Only Add the following property into your API gateway:

spring.cloud.discovery.enabled=true

Make sure you already added DevTool maven dependency into your API gateway project but if not then restart it.

0
votes

add flowing property in application.property file of all eruka client microservice and api gateway , i face same issue and resolve doing same activity

spring.cloud.discovery.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id= true
spring.cloud.gateway.discovery.locator.enabled= true
eureka.instance.hostname=localhost
0
votes

Add spring.cloud.discovery.enabled=true in your application.properties

0
votes

Add below to both gateway and individual microservice fix the issue

eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8010/eureka/
0
votes

after adding all the above properties then also if you are facing issue then try the below one,

don't use lb://albums_service , but use lb://albums-service .Because URI don‘t support underline.

0
votes

You can add the following in application.yml file

spring:
  cloud:
    gateway:
      routes:
        - id: test-service
          uri: lb://MICROSERVICE1
          predicates:
            - Path=/microservice1/**
          filters:
            - RewritePath=/microservice1/(?<segment>.*), /$\{segment}

with this it should works.

Like let say if your microservice1 is url is localhost:8081/service1/message then you can define the base path of your microservice1 in api-gateway by setting up the path as i did in above configuration.