【问题标题】:Spring Boot - fetching data from another microserviceSpring Boot - 从另一个微服务获取数据
【发布时间】:2021-08-06 19:02:49
【问题描述】:

我正在为我的 API 使用 Spring Boot。我正在重写我的 API,以采用微服务架构。

我有 2 节课:

1) 产品

2) 成分

我的代码:

这是我的产品类:

    public class Product{
       private String name;
       @ElementCollection
       private List<Long> productIngredients = new ArrayList<>(); //Ingredient
       private Double quantity = 0.0;
       private Double productCost = 0.0;
}

这是我的Ingredient类:

public class Ingredient{    
     private String name;
     private String unit;
     private Double quantity = 0.0;
}

Product 微服务中,我正在对 Ingredient 微服务进行 API 调用:

// Making a call to the Ingredients microservice from the product microservice

WebClient myWebClient = WebClient.create("http://localhost:8090/api");

@GetMapping("/ingredients/get")
public Flux<Product> getIngredients(){
   
        return myWebClient
                .get()
                .uri("/ingredients/ingredient/list")
                .retrieve()
                .bodyToFlux(Product.class);
}

但是,上面的 getIngredients() 方法不起作用。

我的问题:

我想从成分微服务中获取数据,但是,我收到以下错误:

"error": "内部服务器错误", “trace”:“org.springframework.web.reactive.function.client.WebClientRequestException:连接被拒绝:没有更多信息:localhost/127.0.0.1:80;嵌套异常是io.netty.channel.AbstractChannel$AnnotatedConnectException:连接被拒绝:没有更多信息:localhost/127.0.0.1:80\r\n\tat org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:141)\r\n\tSuppressed : reactor.core.publisher.FluxOnAssembly$OnAssemblyException: \n在以下站点观察到错误:\n\t|_ checkpoint ⇢ 请求获取 http://localhost/api/components/component/list [DefaultWebClient ]\n堆栈跟踪:\r\n\t\tat org.springframework.web.reactive.function.client.

【问题讨论】:

    标签: java spring spring-boot spring-mvc spring-data-jpa


    【解决方案1】:

    Connection refused: no further information: localhost/127.0.0.1:80,异常表示:

    1. 您的本地计算机上的端口 80 上没有任何东西在监听
    2. 您正在向 localhost:80 而非 localhost:8090 发出请求,您确定 myWebClient 实例是使用正确 URL 配置的实例吗?

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2020-11-24
      • 2017-12-28
      • 2019-07-02
      • 1970-01-01
      • 2020-02-04
      • 2020-08-07
      • 2016-11-22
      相关资源
      最近更新 更多