5

I have this simple microservices application with Spring Boot and I try to add the Spring Cloud Gateway service, but it doesn't work. All microservices, including the Eureka server, work well, but when I try to access to some microservice using the Gateway route, it doesn't find the microservice.

ApiGateway pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.futurex.microservices</groupId>
    <artifactId>APIGateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>APIGateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

API Gateway application.yml

spring:
  application:
    name: API-GATEWAY
  cloud:
    gateway:
      routes:
        - id: FX-SERVICE1
          uri: http://localhost:8005
          predicates:
            - Path=/fx-service1/**
        - id: FX-SERVICE2
          uri: http://localhost:8006
          predicates:
            - Path=/fx-service2/**
    discovery:
      enabled: true
    config:
      uri: http://localhost:8081
server:
  port: 8081
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: localhost

API Gateway main

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }


}

Api Gateway Not found 404 error

Karla Calero
  • 51
  • 1
  • 2
  • 1
    I had a similar problem a few days ago playing around with Gateway, but without Eureka. I did two things and fixed this, but I don't actually know which solved the issue: 1) Clear cache from the browser and 2) Change `http://localhost:xxxx` to just `localhost:xxxx`. Give that a try. – Jetto Martínez Aug 04 '21 at 20:22
  • I tried it but it didn't work. Still thank you very much. – Karla Calero Aug 04 '21 at 21:37

3 Answers3

2

Sometimes it's happened case of the service name issue add this property on the edge-service :

For application.properties file:

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

For application.yml file:

 spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
Imranmadbar
  • 4,681
  • 3
  • 17
  • 30
0

i've principaly do 2 things and it works for me.

  • all services name have to be in UPERCASE like

    uri: lb://UPERCASE-NAME

  • when you specify URI your it will have to end by slash "/"

    uri: lb://UPERCASE-NAME/

  • specify only one path predicates by route id i don't know why but it's work like that

    • id: ACCOUNTING-SERVICE uri: lb://ACCOUNTING/ predicates: - Path=/api/v1/act/**
0

In case it can help to someone try this configuration. In application.properties:

spring.application.name=service-gateway-server
server.port=8090

eureka.client.service-url.defaultZone=http://localhost:8761/eureka

And i show the rest of my configuration in application.yml:

spring:
  cloud:
    gateway:
      routes:
      - id: service-one
        uri: lb://service-one
        predicates:
          - Path=/api/service1/**
        filters:
          - StripPrefix=2
      - id: service-two
        uri: lb://service-two
        predicates:
          - Path=/api/service2/**
        filters:
          - StripPrefix=2
Vikcen
  • 153
  • 2
  • 9