Home > Net >  Spring Boot 2.5.6 API Gateway Eureka Server = 404
Spring Boot 2.5.6 API Gateway Eureka Server = 404

Time:11-14

I'm creating a project with Spring API gateway, Eureka Server and WebFlux.

With the spring documentations I've started the 3 services:

  • localhost:3761 - Eureka
  • localhost:4001 - Serivce
  • localhost:8080 - Api gateway

Eureka list the 2 services (gateway and my service) My service works fine from browser My gateway only responds if my route uri is localhost and I really don't know how to solve it.

Gateway application.yml - Not Working

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

spring:
  application:
    name: GATEWAY-SERVER
  cloud:
    gateway:
      
      discovery:
        locator:
          enabled: true
      routes:
        - id: my-service-route
          uri: lb://MY-SERVICE
          predicates:
            - Path=/api/my/service/**

Gateway application.yml - Working

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

spring:
  application:
    name: GATEWAY-SERVER
  cloud:
    gateway:
      
      discovery:
        locator:
          enabled: true
      routes:
        - id: my-service-route
          uri: http://localhost:4001
          predicates:
            - Path=/api/my/service/**

Notice that, the only diference is that the URI not looking for service name.

Eureka Server

CodePudding user response:

@spencergibb

Here the dependencies:

Parent (BoM)

<?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 http://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.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.site</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>pom</packaging>
    <name>Parent POM</name>

    <properties>
        <java.version>11</java.version>
        <encoding>UTF-8</encoding>
        <spring-cloud.version>2020.0.4</spring-cloud.version>
        <spring-native.version>0.10.3</spring-native.version>
        <feign.reactor.version>3.1.1</feign.reactor.version>

        <maven.release.version>3.0.0-M4</maven.release.version>
        <lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
        <org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
        <org.springdoc.version>1.5.11</org.springdoc.version>

        <project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
        <project.resources.sourceEncoding>${encoding}</project.resources.sourceEncoding>
        <archetype.encoding>${encoding}</archetype.encoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Develop -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>${lombok.mapstruct.binding.version}</version>
            </dependency>

            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct</artifactId>
                <version>${org.mapstruct.version}</version>
            </dependency>

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

            <dependency>
                <groupId>com.playtika.reactivefeign</groupId>
                <artifactId>feign-reactor-spring-cloud-starter</artifactId>
                <version>3.1.1</version>
                <type>pom</type>
            </dependency>

            <!-- Documentation -->
            <dependency>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-webflux-ui</artifactId>
                <version>${org.springdoc.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- DevOps -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/application*.yml</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layers>
                        <enabled>true</enabled>
                        <includeLayerTools>true</includeLayerTools>
                    </layers>
                    <image>
                        <name>${project.artifactId}:${project.version}</name>
                    </image>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source> <!-- depending on your project -->
                    <target>${java.version}</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>

                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>

                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>${lombok.mapstruct.binding.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>${maven.release.version}</version>
                <configuration>
                    <allowTimestampedSnapshots>true</allowTimestampedSnapshots>
                    <tagNameFormat>@{project.version}</tagNameFormat>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Eureka

<?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>com.site</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>eureka-server</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Gateway

<?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>com.site</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <artifactId>gateway-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway-server</name>
    <description>gateway-server</description>

    <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

My Service

<?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>com.site</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <artifactId>service</artifactId>
    <version>0.1.1-SNAPSHOT</version>

    <name>my-service</name>
    <description>my-service</description>


    <dependencies>
        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>com.playtika.reactivefeign</groupId>
            <artifactId>feign-reactor-spring-cloud-starter</artifactId>
            <type>pom</type>
        </dependency>

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

        <!-- Database -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>

        <!-- Developer -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
        </dependency>

        <!-- Documentation -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-webflux-ui</artifactId>
        </dependency>

        <!-- Validation -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <!-- Tests -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Also here the yaml for the services. I think i did something wrong on gateway or at the Eureka.

Eureka

server:
  port: 8761

spring:
  application:
    name: EUREKA-SERVER

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    register-with-eureka: false
    fetch-registry: false
    use-dns-for-fetching-service-urls: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

logging:
  level:
    com:
      netflix:
        eureka: off
        discovery: off

Gateway

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

spring:
  application:
    name: GATEWAY-SERVER
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: my-service-route
          uri: lb://MY-SERVICE
          predicates:
            - Path=/api/v1/**

My Service

server:
  port: 4001

spring:
  application:
    name: MY-SERVICE
  data:
    mongodb:
      uri: mongodb://localhost:27017/service

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

springdoc:
  api-docs:
    enabled: true
    path: /api-docs
  swagger-ui:
    path: /index.html

CodePudding user response:

Doing some tests on GATEWAY-SERVICE. I've noticed something strange for me:

I've listed the services at the Gateway:

GatewayServerApplication

@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication implements CommandLineRunner {
    @Autowired
    private DiscoveryClient discoveryClient;

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

    @Override
    public void run(String... args) throws Exception {
        System.out.println("LIST INSTANCES");
        List<String> list = discoveryClient.getServices();

        list.forEach(item -> {
            System.out.println("Service: "   item);
            List<ServiceInstance> instances = this.discoveryClient.getInstances(item);
            instances.forEach(instance -> {
                System.out.println("URI: "   instance.getUri());
                System.out.println("HOST: "   instance.getHost());
                System.out.println("PORT: "   instance.getPort());
                System.out.println("INSTANCE ID: "   instance.getInstanceId());
                System.out.println("SERVICE ID: "   instance.getServiceId());
            });
        });
    }
}


2021-11-13 09:24:35.633  INFO 1547 --- [           main] b.c.f.gateway.GatewayServerApplication   : Started GatewayServerApplication in 3.576 seconds (JVM running for 4.419)
LIST INSTANCES
Service: my-service
URI: http://192.168.0.103:8761
HOST: 192.168.0.103
PORT: 8761
INSTANCE ID: mbp-of-rafael:MY-SERVICE:4001
SERVICE ID: MY-SERVICE

The URI and the Port are defined as 8761 (eureka server port). But I think it should be 4001 (my service port). Am I Wrong?!

  • Related