spring-210730-02---Spring5新功能-Webflux-响应式编程-Java实现

spring-210730-03---Spring5新功能-Webflux-响应式编程-Reactor实现&操作符 spring-210730-03—Spring5新功能-Webflux-响应式编程-Reactor实现&操作符 Reactor有两个核心类,Mono和Flux 操作符 Reactor演示 引入依赖 TestReactor.java Reactor有两个核心类,Mono和Flux 响应式编程操作中,Reactor是满足Reactive规范框架。 Reactor有两个核心类,Mono和Flux,这两个类实现了接口Publisher,提供了丰富的操作符。 1) Flux对 阅读详情

spring-210730-02—Spring5新功能-Webflux-响应式编程-Java实现


什么是响应式编程

什么是响应式编程:
	简称RP(Reactive Programming)
	响应式编程是一种面向数据流和变化传播的编程范式。这意味着可以在编程语言中很方便地表达静态或动态的数据流,
	而相关的计算模型会自动将变化的值通过数据流进行传播。
例如:
	对于 a=b+c 这个表达式的处理,在命令式编程中,会先计算 b+c 的结果,
	再把此结果赋值给 变量a,因此 b,c 两值的变化不会对 变量a 产生影响。
	但在响应式编程中,变量a 的值会随时跟随 b,c 的变化而变化。

	电子表格程序就是响应式编程的一个例子。
	单元格可以包含字面值或类似"=B1+C1"的公式,
	而包含公式的单元格的值会依据其他单元格的值的变化而变化。

观察者模式(演示)

目前我也不懂,只是看着做了,有一个了解。2021.07.30

Java8以及之前版本提供了观察者模式的两个类:
	Observer,Observable

ObserverTest.java

package com.bgy.spring.reactorjava8;

import java.util.Observable;

public class ObserverTest extends Observable {

    public static void main(String[] args) {
        ObserverTest observer = new ObserverTest();

        // 添加观察者
        observer.addObserver((o,element)->{
            System.out.println("发生了变化");
        });
        observer.addObserver((o,element)->{
            System.out.println("手动通知被观察者,准备改变");
        });

        // 数据变化
        observer.setChanged();

        // 通知
        observer.notifyObservers();
    }
}
/*
    运行结果:
        手动通知被观察者,准备改变
        发生了变化
 */

尚硅谷视频给的例子

我还是看不懂2021.07.30

这个把Java9做了封装,

整个Spring5框架基于Java8,运行时兼容JDK9,这是Spring特点,可能这就是其中一个吧

Flow.java

public final class Flow {

    private Flow() {} // uninstantiable

    /**
     * A producer of items (and related control messages) received by
     * Subscribers.  Each current {@link Subscriber} receives the same
     * items (via method {@code onNext}) in the same order, unless
     * drops or errors are encountered. If a Publisher encounters an
     * error that does not allow items to be issued to a Subscriber,
     * that Subscriber receives {@code onError}, and then receives no
     * further messages.  Otherwise, when it is known that no further
     * messages will be issued to it, a subscriber receives {@code
     * onComplete}.  Publishers ensure that Subscriber method
     * invocations for each subscription are strictly ordered in <a
     * href="package-summary.html#MemoryVisibility"><i>happens-before</i></a>
     * order.
     *
     * <p>Publishers may vary in policy about whether drops (failures
     * to issue an item because of resource limitations) are treated
     * as unrecoverable errors.  Publishers may also vary about
     * whether Subscribers receive items that were produced or
     * available before they subscribed.
     *
     * @param <T> the published item type
     */
    @FunctionalInterface
    public static interface Publisher<T> {
        /**
         * Adds the given Subscriber if possible.  If already
         * subscribed, or the attempt to subscribe fails due to policy
         * violations or errors, the Subscriber's {@code onError}
         * method is invoked with an {@link IllegalStateException}.
         * Otherwise, the Subscriber's {@code onSubscribe} method is
         * invoked with a new {@link Subscription}.  Subscribers may
         * enable receiving items by invoking the {@code request}
         * method of this Subscription, and may unsubscribe by
         * invoking its {@code cancel} method.
         *
         * @param subscriber the subscriber
         * @throws NullPointerException if subscriber is null
         */
        public void subscribe(Subscriber<? super T> subscriber);
    }

    /**
     * A receiver of messages.  The methods in this interface are
     * invoked in strict sequential order for each {@link
     * Subscription}.
     *
     * @param <T> the subscribed item type
     */
    public static interface Subscriber<T> {
        /**
         * Method invoked prior to invoking any other Subscriber
         * methods for the given Subscription. If this method throws
         * an exception, resulting behavior is not guaranteed, but may
         * cause the Subscription not to be established or to be cancelled.
         *
         * <p>Typically, implementations of this method invoke {@code
         * subscription.request} to enable receiving items.
         *
         * @param subscription a new subscription
         */
        public void onSubscribe(Subscription subscription);

        /**
         * Method invoked with a Subscription's next item.  If this
         * method throws an exception, resulting behavior is not
         * guaranteed, but may cause the Subscription to be cancelled.
         *
         * @param item the item
         */
        public void onNext(T item);

        /**
         * Method invoked upon an unrecoverable error encountered by a
         * Publisher or Subscription, after which no other Subscriber
         * methods are invoked by the Subscription.  If this method
         * itself throws an exception, resulting behavior is
         * undefined.
         *
         * @param throwable the exception
         */
        public void onError(Throwable throwable);

        /**
         * Method invoked when it is known that no additional
         * Subscriber method invocations will occur for a Subscription
         * that is not already terminated by error, after which no
         * other Subscriber methods are invoked by the Subscription.
         * If this method throws an exception, resulting behavior is
         * undefined.
         */
        public void onComplete();
    }

    /**
     * Message control linking a {@link Publisher} and {@link
     * Subscriber}.  Subscribers receive items only when requested,
     * and may cancel at any time. The methods in this interface are
     * intended to be invoked only by their Subscribers; usages in
     * other contexts have undefined effects.
     */
    public static interface Subscription {
        /**
         * Adds the given number {@code n} of items to the current
         * unfulfilled demand for this subscription.  If {@code n} is
         * less than or equal to zero, the Subscriber will receive an
         * {@code onError} signal with an {@link
         * IllegalArgumentException} argument.  Otherwise, the
         * Subscriber will receive up to {@code n} additional {@code
         * onNext} invocations (or fewer if terminated).
         *
         * @param n the increment of demand; a value of {@code
         * Long.MAX_VALUE} may be considered as effectively unbounded
         */
        public void request(long n);

        /**
         * Causes the Subscriber to (eventually) stop receiving
         * messages.  Implementation is best-effort -- additional
         * messages may be received after invoking this method.
         * A cancelled subscription need not ever receive an
         * {@code onComplete} or {@code onError} signal.
         */
        public void cancel();
    }

    /**
     * A component that acts as both a Subscriber and Publisher.
     *
     * @param <T> the subscribed item type
     * @param <R> the published item type
     */
    public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
    }

    static final int DEFAULT_BUFFER_SIZE = 256;

    /**
     * Returns a default value for Publisher or Subscriber buffering,
     * that may be used in the absence of other constraints.
     *
     * @implNote
     * The current value returned is 256.
     *
     * @return the buffer size value
     */
    public static int defaultBufferSize() {
        return DEFAULT_BUFFER_SIZE;
    }

}

Main.java

package com.atguigu.demo;

import java.util.concurrent.Flow;

public class Main {

    public static void main(String[] args) {
        Flow.Publisher<String> publisher = subscriber -> {
            subscriber.onNext("1"); // 1
            subscriber.onNext("2");
            subscriber.onError(new RuntimeException("出错")); // 2
            //  subscriber.onComplete();
        };


        publisher.subscribe(new Flow.Subscriber<>() {
            @Override
            public void onSubscribe(Flow.Subscription subscription) {
                subscription.cancel();
            }

            @Override
            public void onNext(String item) {
                System.out.println(item);
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("出错了");
            }

            @Override
            public void onComplete() {
                System.out.println("publish complete");
            }
        });
    }
}
响应式编程-基于Reactor模式WebFlux框架的Spring Gateway 响应式编程在一些java生态的框架中应用很多,如Spring Cloud Gateway、Spring AI等中,Spring Cloud Gateway依靠响应式编程框架WebFlux基于Netty实现了一个非阻塞的网关,解决传统网关请求量大时候的线程耗尽问题,另外对于日常AIGC生产一般的大模型流式数据返回也是使用到了响应式编程,这样可以第一时间拿到响应内容,提升用户体验。 阅读详情

相关推荐

JAVA:Spring Boot 集成 WebFlux 实现响应式编程

Spring WebFlux 是一个基于响应式编程的框架,支持非阻塞异步应用开发。本文介绍了其核心组件(Publisher/Subscriber/Reactive Streams)和常用类型(Mono/Flux),并通过构建用户管理系统演示了集成过程,包括数据模型定义、MongoDB存储库配置、服务层封装和控制器实现。此外,还探讨了高级应用场景,如背压控制机制(onBackpressureDrop/Buffer/Error/Latest)和WebClient非阻塞HTTP调用。

木头 1570

尚硅谷Spring课件(自己整理的)

本文为个人整理的SSM部分的Spring课件

weixin_45773002的博客 1322

从零开始理解二极管限幅:5种典型电路设计及实际应用场景解析

本文系统解析了二极管限幅电路的五大经典拓扑,包括串联与并联型上下限幅及双向限幅。通过剖析其核心原理与二极管的单向导电性、正向压降特性,结合ADC输入保护、电源管理、音频处理等实际应用场景,提供了从器件选型到设计陷阱规避的完整实战指南,帮助工程师为信号构建可靠的“安全笼”。

2y3u4i5o6p的博客 1034

Java实战:响应式编程WebFlux实战

本文将详细介绍F响应式编程WebFlux的实战应用,包括WebFlux的基本概念、优势、核心组件、编程模型、数据绑定、异常处理、安全性、测试等方面的内容。

oandy0的博客 1788

SpringBoot整合oceanbase,实现oracle无缝切换到oceanbase

随着去“IOE”化的潮流,近些年来,许多与国家相关的项目不约而同到地往”国产化“方向靠拢,不可避免地,一些项目可能需要进行数据库迁移。

4624

AMESim柱塞泵建模全指南:从HCD库搭建到联合仿真

液压系统仿真中,轴向柱塞泵的建模始终是难点与热点。柱塞泵结构紧凑、功率密度高,在工程机械与航空航天领域应用广泛,然而其运动学与流体细节深度耦合,给仿真带来不小挑战。AMESim平台中的HCD库提供了一种从物理结构出发的建模思路,将柱塞泵分解为容积腔、节流口与机械约束等基础单元,从而真实刻画柱塞腔压力波动、配流特性和容积效率等关键行为,摆脱黑盒模型的局限。基于机理建模的仿真结果,可用于泵参数优化、故障特征分析以及控制策略验证。进一步结合AMESim与Simulink联合仿真,可以高效开发变量泵排量控制系统,研

weixin_29159441的博客 290

Spring5 框架新功能(Webflux)

介绍Spring5新的模块SpringWebflux,通过Java和Reator实现响应式编程

m0_62946761的博客 2万+

保姆级教程:用ESP32和SPH0645麦克风做个无线录音笔(附Python服务端实时播放)

本文提供了一份详细的ESP32无线录音笔开发指南,涵盖从硬件搭建到Python服务端实时回放的全过程。通过SPH0645麦克风采集高质量音频数据,利用ESP32的I2S接口和WiFi功能实现无线传输,并结合Python服务端实现实时播放和WAV文件保存,适合远程音频监控和创意交互装置开发。

weixin_30700099的博客 643

响应式编程实战(08)-WebFlux,使用注解编程模式构建异步非阻塞服务

从今天开始,我们将引入 Spring WebFlux 来构建响应式的 RESTful Web 服务。作为一款全新的开发框架,WebFlux 具有广泛的应用场景,同时也支持两种不同的开发模型。本讲针对注解编程模型给出了 RESTful 服务的开发方法。

JavaEdge全是干货的技术号 1959

基于LightGBM和多模型融合的量化交易研究:沪深300为例【附代码】

📊 数据科学与大数据专业 | 数据分析与模型构建 | 数据驱动决策✨ 专业领域:数据挖掘与清洗大数据处理与存储技术机器学习与深度学习模型数据可视化与报告生成分布式计算与云计算数据安全与隐私保护💡 擅长工具:Python/R/Matlab 数据分析与建模Hadoop/Spark 大数据处理平台SQL数据库管理与优化Tableau/Power BI 数据可视化工具TensorFlow/PyTorch 深度学习框架✅数据科学与大数据技术毕业论文【数据】

lagougongzuoshi的博客 1670

MFC工具栏增加EditBox和Button

1. 在资源视图中增加EditBox, Button控件,ID分别为ID_EDIT_SEED, ID_BTN_SEED 2. 在CMainFrame中增加成员变量 CMFCToolBarEditBoxButton *m_editSeed; CMFCToolBarButton *m_btnSeed; 3. 在CMainFrame::OnCreate中创建控件 m_editSeed = new CMFCToolBarEditBoxButton(ID_EDIT_SEED, GetCmdMgr()-&.

学习是为了探索这个世界的本质 803

Webflux响应式编程(SpringBoot 2.0新特性)——完整版

学习webflux前需要学习三个基础: 函数式编程和lambda表达式 Stream流编程 Reactive stream 响应式流 接下来进入学习 一、函数式编程和lambda表达式 1. 什么是函数式编程 函数式编程是一种相对于命令式编程的一种编程范式,它不是一种具体的技术,而是一种如何搭建应用程序的方法论 2. 为什么要使用函数式编程 * 能让我们以一种更加优雅的方式进行编程 *...

AlgerFan 5806

TC397 EB MCAL开发从0开始系列 之 [17.1] ETH配置实战 - ping/发送

TC397 MCAL 从0开始之 ETH ping测试

yy九歌的博客 1507

spring-210730-01---Spring5新功能-Webflux-基本概念

spring-210730-01—Spring5新功能-Webflux-基本概念 SpringWebFlux介绍 异步非阻塞解释 WebFlux特点 比较SpringMVC SpringWebFlux介绍 WebFlux官方文档说明地址: https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html SpringWebFlux介绍: 1)是Spring5中新添加的模块,用于web开发,功

白光一的博客 248

rk3588 适配以太网(百兆)

虽然 RK3588 的 GMAC 控制器原生支持千兆速率(10/100/1000 Mbps),但在某些工业控制、物联网网关或成本敏感型硬件设计中,开发者会选择将其配置为 百兆以太网(Fast Ethernet) 模式。

weixin_35723192的博客 220

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

响应式编程四Spring WebFlux高阶实战,2W字干货!!

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,Spring Cloud Gateway 旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式。因此,Spring团队在过去几年中的核心挑战,就是如何构建一个新的解决方案,以在使用基于注解的编程模型的同时,提供异步非阻塞服务的所有优势。Spring MVC和Spring WebFlux并不是分立的。

lsd&xql的博客 1620

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【Java开发300个实用技巧】157.响应式编程WebFlux

本文深入探讨了响应式编程框架WebFlux的核心概念与实践技巧。首先,通过对比传统Servlet的同步阻塞模型与WebFlux的异步非阻塞模型,揭示了WebFlux在高并发场景下的优势。其次,详细介绍了Reactor编程模型中的三大核心组件:Flux、Mono与Scheduler,并强调了避免阻塞操作的重要性。接着,讨论了背压机制在流量控制中的关键作用,以及如何通过函数式端点设计(RouterFunction)实现灵活的路由配置。最后,分享了针对异步代码的测试策略,强调使用StepVerifier进行分步验

925

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

14小时吃透Spring5新特性,重点讲解WebFlux响应式编程

可以是你正在学习的东西,也可以是你认为很酷的东西,更可以是你正在工作的东西,皆可。可以是你正在学习的东西,也可以是你认为很酷的东西,更可以是你正在工作的东西,皆可。但是,最重要的是,你将了解到其他人是如何看待编程的。还有你遇到的那些小问题,已经有无数前辈也曾遇到过,他们总结了并分享在互联网上了,前人的经验是你的宝贵资源,要善于利用谷歌等软件。还有你遇到的那些小问题,已经有无数前辈也曾遇到过,他们总结了并分享在互联网上了,前人的经验是你的宝贵资源,要善于利用谷歌等软件。你需要挑战自我,并超越熟悉的领域。

m0_59290508的博客 248

Java 响应式编程与 Spring WebFlux

本文系统介绍了响应式编程和Spring WebFlux框架。首先阐述了响应式编程的核心思想——基于异步数据流和变化传播的编程范式,通过背压机制实现高效资源利用。其次详细解析了Project Reactor的核心类型Mono和Flux及其丰富的操作符。然后对比了WebFlux与传统Spring MVC的区别,介绍了两种编程风格(注解控制器和函数式端点)以及响应式数据库支持。最后指出WebFlux适用于高并发、实时流式场景,但需考虑调试难度和生态系统支持。建议根据实际场景选择,从简单API改造入手逐步体验响应式

M_Reus_11的博客 1246

Java 中的响应式编程:以 Spring WebFlux 为例

Spring WebFlux 是 Spring Framework 5 中引入的全新响应式 Web 框架,它是为响应式编程模型设计的,基于非阻塞的 Reactor 项目(Project Reactor),并实现了 Reactive Streams 规范,使得 Java 在 Web 编程中具备异步处理高并发请求的能力。

一名热衷于技术的全栈开发者,专注于前端与后端的全面技术探索。在这里,我将分享我在技术领域的学习与成长,助力更多开发者的进步。 1495

【2024】JAVA实现响应式编程Web Flux的Reactor具体API文档使用说明

响应式编程 (Reactive Programming) 是一种声明式编程范式,专注于数据流和变化的传播。随着软件系统日益复杂,对高并发、实时性和弹性的需求不断增长,响应式编程正逐渐成为主流。特别从Spring Boot3开始逐渐越来越重视使用,并且Spring框架为了全面拥抱响应式编程,提供了Spring WebFlux、Spring Data Reactive等模块,为Java开发者构建响应式应用提供了强大的支持。

weixin_52315708的博客 3919

一文详解响应式编程springwebflux

详解springwebflux使用

工匠精神coding,终生学习 1249

Spring WebFlux:响应式编程

WebFlux提供了一个非阻塞、异步的Web框架,允许开发者构建高性能、可伸缩的 Web 应用程序,特别适合处理大量并发连接,如在微服务架构和云环境中。 WebFlux是Spring Framework 5引入的一个重要组件,它代表了Spring对于响应式编程(Reactive Programming)的支持。

yuanchun的知识整理 6512

【Java进阶】今天聊聊Java的响应式编程

Java响应式编程是一种编程范式,它专注于处理异步数据流,并且能够高效地管理这些数据流的传播。这种编程方式特别适用于需要高并发、非阻塞操作的应用场景,比如实时数据分析、微服务架构中的通信等。通过上述示例,我们展示了如何在Java中使用响应式编程模型来处理异步数据流。这不仅提高了系统的响应性和效率,还使得处理大量并发请求变得更加容易。响应式编程的核心在于有效地管理和控制数据流,特别是在高负载环境下,Backpressure机制尤为重要。希望这个详细的解释能帮助你更好地理解和应用Java响应式编程。

wendao76的专栏 3672

【Spring WebFlux】什么是响应式编程

响应式编程的本质,是让系统资源从 “独占式” 变为 “共享式”,从 “等待式” 变为 “通知式”。对于新手来说,理解它的关键不是记住 API,而是转变思维模式:从 “我要等待结果” 到 “结果准备好了告诉我”从 “一个线程干到底” 到 “多个环节协同完成”从 “尽量多开线程” 到 “高效复用线程”就像城市交通从 “拓宽马路” 到 “优化交通信号” 的演进,响应式编程正在改变我们构建高性能系统的方式。下一次当你遇到系统卡顿、内存飙升的问题时,不妨想想:这个场景能用响应式编程优化吗?

会飞的架狗师的博客 1342

WebFlux响应式编程基础之 5 webflux服务端开发讲解

第5章 webflux服务端开发讲解

炫的博客 3689
上一篇: spring-210730-01---Spring5新功能-Webflux-基本概念
下一篇: spring-210730-03---Spring5新功能-Webflux-响应式编程-Reactor实现&操作符
白光一
博客等级 码龄9年 0粉丝 174原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值