在Heroku上使用微服务的Spring启动应用程序eureka出现未知主机异常

bqujaahr  于 2022-11-13  发布在  Spring
关注(0)|答案(1)|浏览(136)

Eureka 服务器

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);
    }

}

评级服务

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这就是问题发生的地方。我在这里调用了另外两个微服务,并将它们封送到各自的对象。一旦我命中这个端点,它就会返回 * 对“http://ratings-data-service/ratingsdata/users/foo”的GET请求的I/O错误:评级数据服务;嵌套异常为java.net。未知主机异常:ratings-data-service*。这是控制器方法中的第一行。

@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()));
            }

电影目录服务- application.yaml

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

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

用heroku来托管我的微服务。

vbopmzt1

vbopmzt11#

检查您正在使用的restemplate是否是负载平衡的(您可以使用@LoadBalanced注解),并检查您的RatingService是否命名为ratings-data-service。

相关问题