欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > c# >内容正文

c#

微服务系列-凯发k8官方网

发布时间:2023/11/13 c# 33 coder
凯发k8官方网 收集整理的这篇文章主要介绍了 微服务系列-使用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文件并添加以下依赖项:

		
			org.springframework.boot
			spring-boot-starter-webflux
		
		
			io.netty
			netty-resolver-dns-native-macos
			osx-aarch_64
		

可以看到上面还添加了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

总结

以上是凯发k8官方网为你收集整理的微服务系列-使用webflux的webclient进行spring boot 微服务通信示例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图