简体   繁体   English

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

[英]Spring Boot - fetching data from another microservice

I am using Spring Boot for my API.我正在为我的 API 使用 Spring 引导。 I am rewriting my API, to adopt the microservices architecture.我正在重写我的 API,以采用微服务架构。

I have 2 classes:我有 2 节课:

1) Product 1) 产品

2) Ingredient 2) 成分

My Code:我的代码:

Here is my Product class:这是我的产品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;
}

Here is my Ingredient class:这是我的成分class:

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

In the Product microservice, I am doing an API call to the Ingredient microservice:产品微服务中,我正在对成分微服务进行 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);
}

However, the above getIngredients() method is not working.但是,上面的 getIngredients() 方法不起作用。

My Question:我的问题:

I want to fetch data from the Ingredient microservice, however, I get the following error:我想从成分微服务中获取数据,但是,我收到以下错误:

"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$9(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.ZC1C425268E68385D1AB5074C “错误”:“内部服务器错误”,“跟踪”:“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.ExchangeFunction(9$Functions.client.ExchangeFunctionFunction java:141)\r\n\tSuppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: \n在以下站点观察到错误:\n\t|_ checkpoint ⇢ Request to GET Z80791B3AE7002FACB88C2468876D /api/components/component/list [DefaultWebClient]\n堆栈跟踪:\r\n\t\tat org.springframework.web.reactive.ZC1C425268E68385D1AB5074C 17A94F14Z.client. 17A94F14Z.client。

Connection refused: no further information: localhost/127.0.0.1:80 , exception says that: Connection refused: no further information: localhost/127.0.0.1:80 ,异常说:

  1. there's nothing listening on port 80 on your local machine本地机器上的 80 端口没有监听
  2. you're making a request to localhost:80 not localhost:8090, are you sure myWebClient instance is the one that is configured with the correct URL?您正在向 localhost:80 而不是 localhost:8090 发出请求,您确定myWebClient实例是配置了正确的 URL 的实例吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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