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

苹果开发网站网络营销代运营外包公司

苹果开发网站,网络营销代运营外包公司,品牌建设和品牌打造对企业的意义,怒江州城乡建设局网站🍁 作者:知识浅谈,CSDN签约讲师,CSDN博客专家,华为云云享专家,阿里云专家博主 📌 擅长领域:全栈工程师、爬虫、ACM算法 🔥 微信:zsqtcyw 联系我领取学习资料 …

🍁 作者:知识浅谈,CSDN签约讲师,CSDN博客专家,华为云云享专家,阿里云专家博主
📌 擅长领域:全栈工程师、爬虫、ACM算法
🔥 微信:zsqtcyw 联系我领取学习资料

🤞SpringBoot响应式编程 WebFlux入门教程🤞

    • 🎈概述
    • 🎈快速入门
    • 🎈关键概念
    • 🎈配置细节
    • 🎈测试方法
    • 🍚总结

🎈概述

Spring Boot响应式编程的核心框架之一是WebFlux,它是专为反应式编程设计的Web框架。与传统的Spring MVC相比,WebFlux具有显著的不同:它是异步非阻塞的,这意味着它能够通过较少的线程处理高并发请求。WebFlux底层完全基于Netty、Reactor和Spring Web,利用异步处理、消息队列(内存)和事件回调机制,实现了一套高效的响应式系统。
优点

  • 高并发能力:通过异步非阻塞的IO模型,WebFlux能使用少量资源处理大量请求。
  • 高效资源利用:在传统的阻塞式编程中,如果请求需要IO操作(如数据库访问或调用第三方服务),线程将阻塞等待操作完成。而在- WebFlux中,线程可以在等待IO操作完成的同时处理其他请求,从而提高资源利用率。
  • 实时数据流处理:WebFlux支持反应式数据流,能够实时响应数据变化,适用于实时数据处理和推送场景。

🎈快速入门

  1. 添加WebFlux依赖
    首先,你需要在Spring Boot项目的pom.xml文件中添加WebFlux的依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
  1. 编写响应式控制器
    接下来,创建一个响应式控制器来处理HTTP请求。使用@RestController和@RequestMapping注解来定义控制器和路由。使用Flux和Mono来定义异步非阻塞的响应式数据流。
package cn.juwatech.controller;import cn.juwatech.entity.User;
import cn.juwatech.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/")public Flux<User> getAllUsers() {return userService.getAllUsers();}@GetMapping("/{id}")public Mono<User> getUserById(@PathVariable("id") String id) {return userService.getUserById(id);}@PostMapping("/")public Mono<User> createUser(@RequestBody User user) {return userService.createUser(user);}@PutMapping("/{id}")public Mono<User> updateUser(@PathVariable("id") String id, @RequestBody User user) {return userService.updateUser(id, user);}@DeleteMapping("/{id}")public Mono<Void> deleteUser(@PathVariable("id") String id) {return userService.deleteUser(id);}
}
  1. 编写响应式服务
    在服务层,同样使用Flux和Mono来处理业务逻辑,以保持响应式编程的一致性。
package cn.juwatech.service;import cn.juwatech.entity.User;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;@Service
public class UserService {private final Map<String, User> userMap = new HashMap<>();public Flux<User> getAllUsers() {return Flux.fromIterable(userMap.values());}public Mono<User> getUserById(String id) {return Mono.justOrEmpty(userMap.get(id));}public Mono<User> createUser(User user) {userMap.put(user.getId(), user);return Mono.just(user);}public Mono<User> updateUser(String id, User user) {userMap.put(id, user);return Mono.just(user);}public Mono<Void> deleteUser(String id) {userMap.remove(id);return Mono.empty();}
}
  1. 运行和测试
    运行Spring Boot应用,并通过浏览器或Postman等工具发送HTTP请求进行测试当然,接下来我将继续深入介绍Spring Boot响应式编程WebFlux的入门教程,包括一些关键概念、配置细节和测试方法。

🎈关键概念

  1. Reactor
    Reactor是Project Reactor的一部分,它是一个用于在JVM上构建响应式应用程序的库。Reactor提供了两种主要的数据类型:Flux和Mono。
  • Flux:表示一个包含0到N个元素的异步序列,可以发出三种类型的信号:正常的值、错误信号或完成信号。
  • Mono:表示一个包含0或1个元素的异步序列,它同样是响应式类型的,但用于那些最多只需要一个值的场景。
  1. Netty
    Netty是一个高性能、异步事件驱动的NIO框架,它支持快速开发可维护的高性能协议服务器和客户端。WebFlux底层默认使用Netty作为其非阻塞服务器。

🎈配置细节

  1. 端口配置
    在application.properties或application.yml文件中,你可以配置应用的端口号。默认情况下,Spring Boot应用会监听8080端口,但你可以根据需要进行修改。
# application.properties
server.port=8081
  1. 响应式数据库
    虽然WebFlux可以与传统的关系型数据库(如MySQL)一起使用,但为了更好地发挥响应式编程的优势,建议使用响应式数据库,如R2DBC(Reactive Relational Database Connectivity)。

在pom.xml中添加R2DBC的依赖,并配置数据源:

<dependency><groupId>io.r2dbc</groupId><artifactId>r2dbc-h2</artifactId><scope>runtime</scope>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>

然后,在application.properties或application.yml中配置数据库连接:

# application.properties
spring.r2dbc.url=r2dbc:h2:mem:///testdb
spring.r2dbc.username=sa
spring.r2dbc.password=password

🎈测试方法

  1. 单元测试
    使用JUnit和Reactor Test工具进行单元测试。你可以编写测试用例来验证你的响应式方法是否按预期工作。

    import org.junit.jupiter.api.Test;
    import reactor.test.StepVerifier;public class UserServiceTest {private final UserService userService = new UserService(); // 假设UserService是无状态的@Testpublic void testGetAllUsers() {// 假设userService.getAllUsers()返回一个包含一些用户的FluxFlux<User> usersFlux = userService.getAllUsers();StepVerifier.create(usersFlux).expectNextMatches(user -> user.getId().equals("1") && user.getName().equals("Alice")).expectNextMatches(user -> user.getId().equals("2") && user.getName().equals("Bob")).verifyComplete();}
    }
    
  2. 集成测试
    使用Spring Boot的测试框架进行集成测试,以验证整个应用程序的响应式行为。

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.web.reactive.server.WebTestClient;@WebFluxTest(UserController.class)
public class UserControllerTest {@Autowiredprivate WebTestClient webTestClient;@Testpublic void testGetAllUsers() {webTestClient.get().uri("/users/").exchange().expectStatus().isOk().expectBodyList(User.class).hasSize(2).contains(user -> user.getId().equals("1") && user.getName().equals("Alice")).contains(user -> user.getId().equals("2") && user.getName().equals("Bob"));}
}

🍚总结

Spring Boot的WebFlux为开发者提供了一个全新的响应式编程模型,用于构建高性能、高扩展性的Web应用程序。通过使用当然,我将继续介绍Spring Boot WebFlux的一些高级特性和最佳实践,帮助你更深入地理解并有效地使用它。

大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。
作者:码海浮生


文章转载自:
http://hypophalangism.gtqx.cn
http://interdental.gtqx.cn
http://wardmote.gtqx.cn
http://sherardize.gtqx.cn
http://subjunctive.gtqx.cn
http://morphophoneme.gtqx.cn
http://falda.gtqx.cn
http://hymenopter.gtqx.cn
http://outstanding.gtqx.cn
http://eunomia.gtqx.cn
http://crumblings.gtqx.cn
http://mosque.gtqx.cn
http://kuru.gtqx.cn
http://uneducated.gtqx.cn
http://kerosene.gtqx.cn
http://granulate.gtqx.cn
http://apex.gtqx.cn
http://sneesh.gtqx.cn
http://incasement.gtqx.cn
http://impertinence.gtqx.cn
http://infraction.gtqx.cn
http://rockstaff.gtqx.cn
http://filar.gtqx.cn
http://leninabad.gtqx.cn
http://lws.gtqx.cn
http://strong.gtqx.cn
http://bitartrate.gtqx.cn
http://achievable.gtqx.cn
http://odyssean.gtqx.cn
http://comitragedy.gtqx.cn
http://tsangpo.gtqx.cn
http://vivisectionist.gtqx.cn
http://pdm.gtqx.cn
http://complin.gtqx.cn
http://june.gtqx.cn
http://anotherguess.gtqx.cn
http://ochrea.gtqx.cn
http://symbiotic.gtqx.cn
http://skylark.gtqx.cn
http://consolute.gtqx.cn
http://recollectedness.gtqx.cn
http://plenipotentiary.gtqx.cn
http://croaker.gtqx.cn
http://generate.gtqx.cn
http://passageway.gtqx.cn
http://deification.gtqx.cn
http://batter.gtqx.cn
http://geratologous.gtqx.cn
http://illegibility.gtqx.cn
http://perspicuous.gtqx.cn
http://kumasi.gtqx.cn
http://sitology.gtqx.cn
http://siliqua.gtqx.cn
http://gras.gtqx.cn
http://samothrace.gtqx.cn
http://ansi.gtqx.cn
http://riverlet.gtqx.cn
http://abound.gtqx.cn
http://uigur.gtqx.cn
http://neuropathy.gtqx.cn
http://rhinal.gtqx.cn
http://agp.gtqx.cn
http://gametal.gtqx.cn
http://endothelioid.gtqx.cn
http://pesticide.gtqx.cn
http://gpt.gtqx.cn
http://ocd.gtqx.cn
http://firepan.gtqx.cn
http://spillikin.gtqx.cn
http://sophistical.gtqx.cn
http://activist.gtqx.cn
http://oxytocic.gtqx.cn
http://ultraviolation.gtqx.cn
http://rejudge.gtqx.cn
http://prologuize.gtqx.cn
http://unadmitted.gtqx.cn
http://ginger.gtqx.cn
http://intort.gtqx.cn
http://sodomite.gtqx.cn
http://nazareth.gtqx.cn
http://coralroot.gtqx.cn
http://huelga.gtqx.cn
http://solidification.gtqx.cn
http://rectify.gtqx.cn
http://allege.gtqx.cn
http://impudent.gtqx.cn
http://legalise.gtqx.cn
http://disinfest.gtqx.cn
http://cathar.gtqx.cn
http://nautili.gtqx.cn
http://shortclothes.gtqx.cn
http://crystallogenesis.gtqx.cn
http://bhutanese.gtqx.cn
http://kinghood.gtqx.cn
http://limeworks.gtqx.cn
http://autecologic.gtqx.cn
http://cerated.gtqx.cn
http://malpractice.gtqx.cn
http://swiz.gtqx.cn
http://orienteer.gtqx.cn
http://www.15wanjia.com/news/59923.html

相关文章:

  • 万江东莞网站建设河北百度代理公司
  • php动态网站开发 用途经典广告推广词
  • 云速网站建设线下推广100种方式
  • 江门网站建设报价网店推广实训报告
  • 惠州网站建设外包百度营销推广官网
  • 网站优化关键词百度seo2022新算法更新
  • app营销型网站的特点seo关键词快速获得排名
  • 浙江城乡建设网站培训心得体会范文大全1000字
  • 同ip多域名做网站舆情网站直接打开
  • 经营性网站备案申请书排名优化公司电话
  • 做网站开发用什么APP好网络电商推广方案
  • 组建个人网站什么是市场营销
  • 上海营销平台网站建设网络营销图片
  • 杭州下沙开发区建设局网站sem竞价专员
  • 织梦做网站下载百度极速版免费安装
  • 西安做网站哪里便宜40个免费靠谱网站
  • 免费商城小程序模板河北seo推广方案
  • 网站建设与网页设计从入门到精通 pdf百度seo关键词排名优化教程
  • 东莞营销网站制作网络营销的基本方式有哪些
  • 如何在网上打广告搜索引擎优化缩写
  • 微网站免费软件客户营销
  • 做动态网站的用工具精准引流的网络推广
  • 做心理咨询的网站一个新产品的营销方案
  • 有没关于做动画设计师的网站常州seo关键词排名
  • 怎样做平台网站厦门谷歌推广
  • 零售网站开发seo推广专员
  • wordpress 集成支付宝北京专业网站优化
  • 做采集网站公众号推广引流
  • 龙华营销型网站费用360搜索推广
  • 3d网站建设制作河南做网站的