日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

微服务系列-使用WebFlux的WebClient进行Spring Boot 微服务通信示例

發(fā)布時間:2023/11/13 C# 59 coder
生活随笔 收集整理的這篇文章主要介紹了 微服务系列-使用WebFlux的WebClient进行Spring Boot 微服务通信示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

公眾號「架構(gòu)成長指南」,專注于生產(chǎn)實踐、云原生、分布式系統(tǒng)、大數(shù)據(jù)技術(shù)分享。

概述

在之前的教程中,我們看到了使用 RestTemplate 的 Spring Boot 微服務(wù)通信示例。
從 5.0 開始,RestTemplate處于維護模式,很快就會被棄用。因此 Spring 團隊建議使用org.springframework.web.reactive.client.WebClient ,它支持同步、異步和流場景。

在本教程中,我們將學(xué)習(xí)如何使用WebClient在多個微服務(wù)之間進行 REST API 調(diào)用(同步通信)。

WebClient是一個非阻塞的響應(yīng)式客戶端,用于執(zhí)行 HTTP 請求,通過底層 HTTP 客戶端庫(例如 Reactor Netty)來實現(xiàn)。

要在 Spring boot 項目中使用WebClient,我們必須將Spring WebFlux依賴項添加到類路徑中。

我們需要做什么

下面將創(chuàng)建兩個微服務(wù),例如 部門服務(wù) 和 用戶服務(wù),并且我們將使用WebClient從 用戶服務(wù) 到 部門服務(wù) 進行 REST API 調(diào)用 ,以獲取特定的用戶部門數(shù)據(jù)。

基礎(chǔ)配置

我們在上一篇文章中創(chuàng)建了兩個微服務(wù): 使用 RestTemplate 的 Spring Boot 微服務(wù)通信示例。

第1步:添加Spring WebFlux依賴

打開user-service項目的pom.xml文件并添加以下依賴項:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-resolver-dns-native-macos</artifactId>
			<classifier>osx-aarch_64</classifier>
		</dependency>

可以看到上面還添加了netty-resolver-dns-native-macos的pom,原因是如果不添加此報會拋出相關(guān)異常,問題詳情

第2步:將WebClient配置為Spring Bean

package io.wz.userservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;

@SpringBootApplication
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }

    @Bean
    public WebClient webClient(){
        return WebClient.builder().build();
    }
}

第三步:注入并使用WebClient調(diào)用REST API

讓我們注入WebClient并使用它來進行 REST API 調(diào)用:

 DepartmentDto departmentDto = webClient.get()
                 .uri("http://localhost:8080/api/departments/" + user.getDepartmentId())
                         .retrieve()
                                 .bodyToMono(DepartmentDto.class)
                                         .block();

下面是UserServiceImpl類的完整代碼, 供大家參考:

package io.wz.userservice.service.impl;

import io.wz.userservice.dto.DepartmentDto;
import io.wz.userservice.dto.ResponseDto;
import io.wz.userservice.dto.UserDto;
import io.wz.userservice.entity.User;
import io.wz.userservice.repository.UserRepository;
import io.wz.userservice.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    private UserRepository userRepository;
    private WebClient webClient;

    @Override
    public User saveUser(User user) {
        return userRepository.save(user);
    }

    @Override
    public ResponseDto getUser(Long userId) {

        ResponseDto responseDto = new ResponseDto();
        User user = userRepository.findById(userId).get();
        UserDto userDto = mapToUser(user);

        DepartmentDto departmentDto = webClient.get()
                .uri("http://localhost:8080/api/departments/" + user.getDepartmentId())
                .retrieve()
                .bodyToMono(DepartmentDto.class)
                .block();
        responseDto.setUser(userDto);
        responseDto.setDepartment(departmentDto);

        return responseDto;
    }

    private UserDto mapToUser(User user){
        UserDto userDto = new UserDto();
        userDto.setId(user.getId());
        userDto.setFirstName(user.getFirstName());
        userDto.setLastName(user.getLastName());
        userDto.setEmail(user.getEmail());
        return userDto;
    }
}

下面運行兩個微服務(wù)并進行測試。

測試:啟動兩個微服務(wù)

首先啟動部門服務(wù)項目,然后啟動用戶服務(wù)項目,一旦兩個項目都啟動并在不同的端口上運行。接下來,我們調(diào)用Get User REST API來測試user-service REST API 對Department-service 的調(diào)用。

獲取用戶 REST API:


請注意,響應(yīng)結(jié)果包含了用戶的部門。這說明我們已成功使用WebClient從用戶服務(wù)到部門服務(wù)進行 REST API 調(diào)用。

結(jié)論

在本教程中,我們學(xué)習(xí)了 如何使用WebClient 在多個微服務(wù)之間進行 REST API 調(diào)用(同步通信)。

源碼下載:
github
gitee

總結(jié)

以上是生活随笔為你收集整理的微服务系列-使用WebFlux的WebClient进行Spring Boot 微服务通信示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。