Spring cloud gateway application does not route to the configured microservice

Viewed 25

I have a microservices project here. I am running two microservices USER-SERVICE and DEPARTMENT-SERVICE which are registered to eureka server and also I have created a API/cloud gateway for the mentioned microservices to route API call to correct microservice.

USER-SERIVCE:

main class:

package com.dailycodebuffer.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class UserServiceApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

Controller:

package com.dailycodebuffer.user.controller;

import com.dailycodebuffer.user.VO.RestTemplateVO;
import com.dailycodebuffer.user.entity.User;
import com.dailycodebuffer.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {

     @Autowired
     private UserService userService;

     @PostMapping("/")
     public User saveUser(@RequestBody User user){
         log.info("inside usercontroller method saveuser");
         return userService.saveUser(user);
     }

     @GetMapping("/{id}")
     public RestTemplateVO getUserWithDepartment(@PathVariable("id") Long userId){
         log.info("inside usercontroller method getUserWithDepartment");
         return userService.getUserWithDepartment(userId);
     }

}

config file of USER-SERVICE

application.yml

server:
  port: 9092

spring:
  application:
    name: USER-SERVICE

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost

DEPARTMENT-SERVICE:

Controller:

package com.dailycodebuffer.department.controller;

import com.dailycodebuffer.department.entity.Department;
import com.dailycodebuffer.department.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/departments")
@Slf4j
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/")
    public Department saveDepartment(@RequestBody Department department){
      log.info("inside departmentcontroller method saveDepartment");
      return departmentService.saveDepartment(department);
    }

    @GetMapping("/{id}")
    public Department findDepartmentById(@PathVariable("id") Long departmentId){
        log.info("inside of departmentcontroller method findDepartmentById");
        return departmentService.findDepartmentById(departmentId);
    }
}

config file of DEPARTMENT-SERVICE application.yml

server:
  port: 9001

spring:
  application:
    name: DEPARTMENT-SERVICE

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost

EUREKA SERVER configuration: application.yml

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

cloud gateway config:

application.yml

server:
  port: 9191

spring:
  application:
    name: API-GATEWAY
    cloud:
      gateway:
        routes:
          - id: USER-SERVICE
            uri: lb://USER-SERVICE
            predicates:
              - Path=/users/**
          - id: DEPARTMENT-SERVICE
            uri: lb://DEPARTMENT-SERVICE
            predicates:
              - Path=/departments/**

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost

But if I trigger API call "localhost:9191/departments/" which is of port cloud gateway, to insert new department into my DB, I get 404 error as shown in following image. However, I trigger same above API with port of DEPARTMENT-SERVICE 9001, it is successful. This means that my spring cloud application is not able to route API call to DEPARTMENT-SERVICE, can help me out here and tell me how to fix this?

404 screenshot here, click here to see image

This microservice project is inspired from this video: https://www.youtube.com/watch?v=BnknNTN8icw&t=2499s

0 Answers
Related