Search code examples
springspring-bootmicroservicesspring-cloudspring-webclient

Springboot WebClientRequestException Error


I wanna build microservices with Springboot. I wanna communication between order service and inventory service. But when I try to connect inventory service from order service with webclient get method, IDE always returns

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: no further information: localhost/192.168.1.1:8080] with root cause

Whatever I tried, I couldn't solve this problem.

OrderManager =

@Service
@AllArgsConstructor
@Transactional
public class OrderManager implements OrderService {
    private OrderRepository orderRepository;
    private ModelMapperService modelMapperService;
    private WebClient webClient;
    @Override
    public void placeOrder(OrderRequest orderRequest) {
      Order order = new Order();
      order.setOrderNumber(UUID.randomUUID().toString());
      List<OrderLineItems> orderLineItemsList= orderRequest.getOrderLineItemsResponseList().stream()
                .map(orders -> this.modelMapperService.forResponse().map(orders, OrderLineItems.class)).collect(Collectors.toList());
      order.setOrderLineItemsList(orderLineItemsList);

      List<String> skuCodes =  order.getOrderLineItemsList().stream()
             .map(OrderLineItems::getSkuCode)
                .toList();

        InventoryResponse[] inventoryResponsesArray =  this.webClient.get()
        .uri("http://localhost:8080/api/v1/inventory", uriBuilder -> uriBuilder.queryParam("skuCode",skuCodes).build())
                .retrieve()
                .bodyToMono(InventoryResponse[].class)
                .block();

        boolean allProductIsInStock = Arrays.stream(inventoryResponsesArray)
                .allMatch(InventoryResponse::isInStock);
      if ( allProductIsInStock){
          this.orderRepository.save(order);
      }
      else {
          throw new IllegalArgumentException("Product is not in stock");
      }


    }
}

InventoryManager =

@Service
@AllArgsConstructor
public class InventoryManager implements InventoryService {

    private final InventoryRepository inventoryRepository;
    @Override
    @Transactional(readOnly = true)
    public List<InventoryResponse> isInStock(List<String> skuCode) {
      return  inventoryRepository.findBySkuCodeIn(skuCode).stream()
              .map(inventory ->
                      InventoryResponse.builder()
                      .skuCode(inventory.getSkuCode())
                      .isInStock(inventory.getQuantity()> 0)
                      .build()

              ).toList();
    }

OrderController =

@RestController
@RequestMapping("/api/v1/order")
@AllArgsConstructor
public class OrderController {
    private OrderService orderService;

    @PostMapping()
    @ResponseStatus(HttpStatus.CREATED)
    public String placeOrder(@RequestBody OrderRequest orderRequest){
       this.orderService.placeOrder(orderRequest);
        return "Order Placed Successfully";
    }
}

InventoryController =

@RestController
@RequestMapping("/api/v1/inventory")
@AllArgsConstructor
public class InventoryController {

    private final InventoryService inventoryService;
    @GetMapping()
    @ResponseStatus(HttpStatus.OK)
    public List<InventoryResponse> isInStock(@RequestParam() List<String> skuCode){
        return this.inventoryService.isInStock(skuCode);
    }
    @GetMapping("/gets")
    public List<Inventory> gets(){
        return this.inventoryService.find();
    }
}

I disabled firewall and I let the TCP response data at Firewall's settings. After all this, they did not help for solving my solution.


Solution

  • Localhost system is not same for the port URL. When I check from cmd:

    netstat -a
    

    My port 8080 is listening from 0.0.0.0. After I wrote 0.0.0.0 instead of localhost, my code is running as I expected.