Web Flux
WebFlux简单介绍
Flux是一个非阻塞异步的框架,其核心是Reactor相关API实现的,相对于其较早的 struts2 , springmvc
等来说它可在 Nettt , Undertow , Servlet3.1+
的容器运行。
Flux的特点体现在非阻塞和函数式编程端点。非阻塞利用socket事件的消息机制,Server端与Client端之间的通信处于异步状态下,其在servlet3.1中提供了非阻塞的API,WebFlux体现的更加完美;
WebFlux和SpringMVC
WebFlux和SpringMVC非常的相似,从上面官方图片中可以看出都可以运行在tomcat,jetty,undertow等servlet容器当中,只不过SpringMVC采用命令式编程方式而WebFlux采用响应式编程,如果在SpringMVC正常运行的应用程序,不需要更改。
在性能方面,反应和非阻塞通常不会使应用成勋运行的更快。
一个简单WebFlux实现的实例:
使用的jar包:
spring-webflux-5.1.2.RELEASE.jar
WebFluxConfig:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringWebFluxTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
@ComponentScan
@EnableWebFlux
public class WebFluxConfig implements WebMvcConfigurer {
@Autowired
private ApplicationContext applicationContext;
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafReactiveViewResolver thymeleafReactiveViewResolver =
new ThymeleafReactiveViewResolver();
SpringResourceTemplateResolver springResourceTemplateResolver =
new SpringResourceTemplateResolver();
springResourceTemplateResolver.setApplicationContext(applicationContext);
springResourceTemplateResolver.setPrefix("classpath:/templates/");
springResourceTemplateResolver.setSuffix(".html");
springResourceTemplateResolver.setCharacterEncoding("utf-8");
springResourceTemplateResolver.setTemplateMode(TemplateMode.HTML);
SpringWebFluxTemplateEngine springWebFluxTemplateEngine = new SpringWebFluxTemplateEngine();
springWebFluxTemplateEngine.setTemplateResolver(springResourceTemplateResolver);
thymeleafReactiveViewResolver.setApplicationContext(applicationContext);
thymeleafReactiveViewResolver.setTemplateEngine(springWebFluxTemplateEngine);
registry.viewResolver((ViewResolver) thymeleafReactiveViewResolver);
}
}复制
Controller:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class InlcentController {
@RequestMapping("/Login")
public String Login(){
return "index";
}
}复制
Main:
import cj.zhang.flux.WebFluxConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import reactor.netty.http.server.HttpServer;
import java.io.IOException;
public class Mian {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(WebFluxConfig.class);
//通过ApplicationContext创建HttpHandler
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build();
ReactorHttpHandlerAdapter httpHandlerAdapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer.create().port(8080).handle(httpHandlerAdapter).bind().block();
System.in.read();
}
}复制
文章转载自产教Code,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
1271次阅读
2025-04-27 16:53:22
2025年4月国产数据库中标情况一览:4个千万元级项目,GaussDB与OceanBase大放异彩!
通讯员
748次阅读
2025-04-30 15:24:06
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
611次阅读
2025-04-14 09:40:20
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
519次阅读
2025-04-17 17:02:24
一页概览:Oracle GoldenGate
甲骨文云技术
483次阅读
2025-04-30 12:17:56
GoldenDB数据库v7.2焕新发布,助力全行业数据库平滑替代
GoldenDB分布式数据库
474次阅读
2025-04-30 12:17:50
优炫数据库成功入围新疆维吾尔自治区行政事业单位数据库2025年框架协议采购!
优炫软件
363次阅读
2025-04-18 10:01:22
给准备学习国产数据库的朋友几点建议
白鳝的洞穴
334次阅读
2025-05-07 10:06:14
XCOPS广州站:从开源自研之争到AI驱动的下一代数据库架构探索
韩锋频道
292次阅读
2025-04-29 10:35:54
国产数据库图谱又上新|82篇精选内容全览达梦数据库
墨天轮编辑部
281次阅读
2025-04-23 12:04:21