当前位置: 首页 > news >正文

三、Spring-WebFlux实战案例-流式

目录

一、springboot之间通讯方式

1. 服务端 (Spring Boot)  

1.1 添加依赖

1.2 控制器

 2. 客户端 (WebClient)  

 2.1 添加依赖  

2.2 客户端代码  

3. 运行  

二、web与服务之间通讯方式

1、服务端代码  

2、客户端代码

3、注意事项

三、移动端与服务端之间通讯方式

1、添加依赖  

2、配置路由  

3、客户端连接  

4、注意事项  


一、springboot之间通讯方式


        为了使用 WebClient 实现流式响应,我们需要在服务端创建一个能够发送流数据的 HTTP 服务,并在客户端使用 WebClient 来接收这些流数据。

下面我将分别展示服务端(Spring Boot 应用)和客户端(使用 WebClient 的应用)的实现

1. 服务端 (Spring Boot)  

首先,我们需要创建一个简单的 Spring Boot 项目来作为服务端。

1.1 添加依赖

创建 Spring Boot 项目  创建一个新的 Spring Boot 项目,添加 Web 和 Actuator 依赖。

<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

1.2 控制器

 创建一个控制器类,用于处理流式请求。

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;@RestController
public class StreamController {    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream() {
        return Flux.interval(Duration.ofSeconds(1))
                .map(i -> "Message " + i);
    }
}

这里我们使用了 Flux 来生成一个无限的数据流,每秒发送一条消息。

 2. 客户端 (WebClient)  

接下来,我们将创建一个简单的 Java 应用程序来作为客户端,使用 WebClient 来接收服务端的流式响应。 

 2.1 添加依赖  

在客户端项目的 pom.xml 文件中添加 spring-webflux 依赖:

<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
</dependencies>

2.2 客户端代码  

创建一个简单的 Java 类来接收流式数据。

import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;public class WebClientStreamExample {    public static void main(String[] args) {
        WebClient client = WebClient.create("http://localhost:8080");        Flux<String> stream = client.get()
                .uri("/stream")
                .retrieve()
                .bodyToFlux(String.cla
http://www.lryc.cn/news/414285.html

相关文章:

  • html+css 实现hover双层按钮
  • SPIFFS与LittleFS的对gz文件格式的区别
  • STM32L051K8U6-开发资料
  • Markdown语法学习
  • [最短路Floyd],启动!!!
  • 7月29(信息差)
  • ubuntu中禁止使用鼠标拖动来移动文件
  • 【密码学】椭圆曲线密码体制(ECC)
  • 第25集《大佛顶首楞严经》
  • python 读写文件之 open 和 with open() 详细解析
  • 操作系统:内存----知识点
  • pfx如何配置到nginx中
  • 详细测评下搬瓦工香港CN2 GIA VPS
  • Java中的五种线程池类型
  • FFmpeg Windows安装教程
  • ‘#‘ is not followed by a macro parameter 关于宏定义的错误
  • 内网穿透--meterpreter端口转发实验
  • Python 数据类:减少样板并提高可读性
  • 家庭教育系列—北京海淀区”鸡娃“攻略
  • DLMS/COSEM中的信息安全:DLMS/COSEM安全概念(下)
  • 基于 systemc-2.3.1的virtual device 接入 qemu-arm
  • (七)自动化测试
  • 【信创】virtualbox内虚拟机连接U盘 _ 统信 _ 麒麟 _ 中科方德
  • 【2024】Datawhale AI夏令营 Task4笔记——vllm加速方式修改及llm推理参数调整上分
  • 腾讯OCR签名算法
  • CTFHUB-SSRF-DNS重绑定 Bypass
  • 【oracle】数据库基本使用
  • Action部署在线上写文章
  • CC链 (Commons Collections)
  • 左手坐标系、右手坐标系、坐标轴方向