Spring 启动 - 从另一个微服务获取数据

Spring Boot - fetching data from another microservice

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

我有 2 个 classes:

1) 产品

2) 成分

我的代码:

这是我的产品 class:

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

这是我的成分 class:

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

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

// 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() 方法不起作用。

我的问题:

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

"error": "Internal Server Error", "trace": "org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: no further information: localhost/127.0.0.1:80; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:80\r\n\tat org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException(ExchangeFunctions.java:141)\r\n\tSuppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: \nError has been observed at the following site(s):\n\t|_ checkpoint ⇢ Request to GET http://localhost/api/components/component/list [DefaultWebClient]\nStack trace:\r\n\t\tat org.springframework.web.reactive.function.client.

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

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