简体   繁体   中英

Spring boot app with microservices eureka on Heroku getting UnknownHostException

Eureka Server

Application.yaml

server:
  port: ${PORT:8761}


eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0


server:
    enableSelfPreservation: false

EurekaServer.java

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {

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

}

RatingService

Application.yaml

  eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URL:http://user:password@localhost:8761}/eureka/

---
spring:
  profiles: cloud
eureka:
  instance:
    hostname: ${APPLICATION_DOMAIN}
    nonSecurePort: 80

MovieCatalogService This is where is issue is happening. I'm calling two other microservices here and marshalling them to their respective objects. Once I hit this enpoint it returns I/O error on GET request for "http://ratings-data-service/ratingsdata/users/foo": ratings-data-service; nested exception is java.net.UnknownHostException: ratings-data-service . Which is the first line in the controller method.

      @RequestMapping("microservices/{userId}")
        public List<CatalogItem> getCatalog(@PathVariable("userId") String userId) {

          UserRating ratings = restTemplate.getForObject("http://ratings-data-service/ratingsdata/users/"+userId, UserRating.class);
    
   
            List<CatalogItem> catalogItems = new ArrayList<>();
    
            for (Rating rating: ratings.getUserRating()) {
                //for each movie ID, call movie info service and get all details
    
                Movie movie =restTemplate.getForObject("http://movie-info-service/movies/"+rating.getMovieId(), Movie.class);
                catalogItems.add(new CatalogItem(movie.getName(), "Desc", rating.getRating()));
            }

MovieCatalogservice - application.yaml

eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URL:http://user:password@localhost:8761}/eureka/

---
spring:
  profiles: cloud
eureka:
  instance:
    hostname: ${APPLICATION_DOMAIN}
    nonSecurePort: 80

Using heroku to host my microservices.

Check if the restemplate you're using is load-balanced(you can use @LoadBalanced annotation) and also check if your RatingService is named ratings-data-service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM