微服务系列-使用WebFlux的WebClient进行Spring Boot 微服务通信示例
公眾號「架構成長指南」,專注于生產實踐、云原生、分布式系統、大數據技術分享。
概述
在之前的教程中,我們看到了使用 RestTemplate 的 Spring Boot 微服務通信示例。
從 5.0 開始,RestTemplate處于維護模式,很快就會被棄用。因此 Spring 團隊建議使用org.springframework.web.reactive.client.WebClient ,它支持同步、異步和流場景。
在本教程中,我們將學習如何使用WebClient在多個微服務之間進行 REST API 調用(同步通信)。
WebClient是一個非阻塞的響應式客戶端,用于執行 HTTP 請求,通過底層 HTTP 客戶端庫(例如 Reactor Netty)來實現。
要在 Spring boot 項目中使用WebClient,我們必須將Spring WebFlux依賴項添加到類路徑中。
我們需要做什么
下面將創建兩個微服務,例如 部門服務 和 用戶服務,并且我們將使用WebClient從 用戶服務 到 部門服務 進行 REST API 調用 ,以獲取特定的用戶部門數據。
基礎配置
我們在上一篇文章中創建了兩個微服務: 使用 RestTemplate 的 Spring Boot 微服務通信示例。
第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,原因是如果不添加此報會拋出相關異常,問題詳情
第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調用REST API
讓我們注入WebClient并使用它來進行 REST API 調用:
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;
}
}
下面運行兩個微服務并進行測試。
測試:啟動兩個微服務
首先啟動部門服務項目,然后啟動用戶服務項目,一旦兩個項目都啟動并在不同的端口上運行。接下來,我們調用Get User REST API來測試user-service REST API 對Department-service 的調用。
獲取用戶 REST API:
請注意,響應結果包含了用戶的部門。這說明我們已成功使用WebClient從用戶服務到部門服務進行 REST API 調用。
結論
在本教程中,我們學習了 如何使用WebClient 在多個微服務之間進行 REST API 調用(同步通信)。
源碼下載:
github
gitee
總結
以上是生活随笔為你收集整理的微服务系列-使用WebFlux的WebClient进行Spring Boot 微服务通信示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 甲肝的判断方法有哪些?如何预防甲肝?
- 下一篇: 理解Go中的零值