@LoadBalanced gives 500 Server Error for HTTP GET

54 Views Asked by At

I have 3 services registered with Eureka Discovery Server:

eureka service registered

and have a Rest Template configured as :

@Configuration
public class RestTemplateConfig {

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

My Controller looks like this:

@RestController
@RequestMapping ("/catalogs/rest")
public class RestTemplateCatalogController {

@Autowired
private CatalogService catalogService;

@GetMapping ("/{userId}")
public List<Catalog> getCatalog(@PathVariable String userId) {

    //1. get all movie details for userID (Movie-Info-Service)
    List<Movie> movies = catalogService.getMoviesRestTemplate(userId);

    List<Catalog> catalogList = new ArrayList<>();
    movies.forEach(movie -> {
        //2. get ratings for each movie (Rating-Info-Service)
        Rating rating = catalogService.getRatingRestTemplate(movie.getMovieId());

        //3. club movie+rating details for userID into catalog
        catalogList.add(new Catalog(
                "catalogId-"+movie.getMovieId(),
                movie.getMovieName(),
                movie.getMovieDesc(),
                rating));
    });

    return catalogList;
}

where step 1 and 2 call a Service which further calls two different microservices named MOVIE-INFO-SERVICE and MOVIE-RATING-SERVICE respectively. The Service class looks like this:

    @Service
    public class CatalogService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WebClient.Builder webClientBuilder;

    public List<Movie> getMoviesRestTemplate (String userId) {
        MovieInfo movieInfo = restTemplate.getForObject("http://MOVIE-INFO-SERVICE/movies/rest/"+userId, MovieInfo.class);
        return movieInfo.getMovies();
    }

    public Rating getRatingRestTemplate (String movieId) {
        Rating rating = restTemplate.getForObject("http://MOVIE-RATING-SERVICE/ratings/rest/"+movieId, Rating.class);
    }
   }

When I try to access the MOVIE-CATALOG-SERVICE I get the below error:

enter image description here

The strange thing is, as soon as I remove the @LoadBalanced annotation and replace the microservice URI with localhost:port rather than names of the services, it works.

I am not sure what is missing or what needs to be corrected. I have created a similar learning project earlier but it was working back then.

0

There are 0 best solutions below