Reactor

[转]Netty进阶之路--ChannelHandler并发安全、NioEventLoop防阻塞挂死

北城余情 提交于 2019-12-05 18:00:56
  《Netty进阶之路》第7章、第8章分别提出ChannelHandler并发安全问题,NioEventLoop线程阻塞导致消息接收和处理缓慢。ChannelHandler的并发安全问题很好分析,NioEventLoop线程阻塞则需要一些技巧。    1 ChannelHandler并发安全   默认每个Channel中有各自的ChannelHandler实例,因此如果所有业务在NioEventLoop中完成则没有线程安全问题,因为从消息接收到响应写出阶段都由同一个NioEventLoop线程处理;若在ChannelHandler消息处理过程中新开启一个线程,则新线程和NioEventLoop同时读写ChannelHandler中的变量时会有线程不安全的风险。 @Component(value = "gatewayChannelInitializer") public class GatewayChannelInitializer extends ChannelInitializer { private static final String ENCODER = "encoder"; private static final String DECODER = "decoder"; @Autowired private BusyMan busyMan; protected void

IO复用,AIO,BIO,NIO,同步,异步,阻塞和非阻塞 区别

血红的双手。 提交于 2019-12-05 14:56:53
如果面试问到IO操作,这篇文章提到的问题,基本是必问,百度的面试官问我三个问题 (1)什么是NIO(Non-blocked IO),AIO,BIO (2) java IO 与 NIO(New IO)的区别 (3)select 与 epoll,poll区别 我胡乱说了一气,自己边说边觉得完蛋了。果然,二面没过,很简单的问题,回来后赶紧作了总结: 一、什么是socket?什么是I/O操作? 我们都知道 unix(like)世界里,一切皆文件,而文件是什么呢?文件就是一串二进制流而已,不管socket,还是FIFO、管道、终端,对我们来说,一切都是文件,一切都是流。在信息 交换的过程中,我们都是对这些流进行数据的收发操作,简称为I/O操作(input and output) ,往流中读出数据,系统调用read,写入数据,系统调用write。不过话说回来了 ,计算机里有这么多的流,我怎么知道要操作哪个流呢?对,就是 文件描述符 ,即通常所说的fd,一个fd就是一个整数,所以,对这个整数的操作,就是对这个文件(流)的操作。我们创建一个socket,通过系统调用会返回一个文件描述符,那么剩下对socket的操作就会转化为对这个描述符的操作。不能不说这又是一种 分层和抽象的思想 。 二、同步异步,阻塞非阻塞区别联系 实际上同步与异步是针对应用程序与内核的交互而言的

Why does it make sense to use asynchronous clients for Redis?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 09:46:57
In this page listing the redis clients , I counted 8 asynchronous libraries. My understanding is that frameworks like as node.js or tornado only make sense when the asynchronous callback functions are not fighting with each other for I/O, otherwise you might as well go synchronous. But Redis is single-threaded. So they are actually fighting for I/O. Doesn't the single-threaded nature of Redis cancel all the potential benefits of asynchronous callbacks? Why does it make sense to use asynchronous clients with Redis? The single-threaded nature of Redis is irrelevant regarding the potential

Windows,Linux的select函数功能差异

☆樱花仙子☆ 提交于 2019-12-03 18:24:41
Windows,Linux的select函数功能差异 感谢主,Windows当年也实现了select函数,这让我们的跨平台大业至少顺畅了一节。但由于Windows渗入骨髓的叛逆心理,他总要和UNIX的实现保持一些差别,让你无可奈何。首先是Windows的select函数的参数接口设计和Linux下有较大差别,这个在我的《设计极其糟糕的select函数》就讨论过,相对而言,在参数设计上,Windows的设计明显好于Linux。这次我们聊聊他们的功能差异。 1 无句柄等待触发时的处理的差异 最近的新的重构代码,发现在Windows下,程序的CPU很高,测试发现select函数并没有等待,return-1,我们的代码原来使用的是rector模式,里面会用select当作反应器,处理IO事件,在没有IO事件时当作sleep。但我们的业务服务器没有任何要处理的IO句柄,所以就相当于调用的是 select(0,NULL,NULL,NULL,wait_timeval); 这种方式在Linux下就相当于sleep,而Windows下却之间返回了-1,认为你传递的参数错误。查询了一下MSDN发现有如下说明。 Microsoft MSDN: Any two of the parameters, readfds, writefds, or exceptfds, can be given as null.

reactor-rabbitmq小试牛刀

折月煮酒 提交于 2019-12-03 13:07:12
序 本文主要研究一下如何使用reactor-rabbitmq maven <dependency> <groupId>io.projectreactor.rabbitmq</groupId> <artifactId>reactor-rabbitmq</artifactId> <version>1.0.0.M2</version> </dependency> rabbitmq 参考 docker搭建rabbitmq集群 当前使用的镜像是bijukunjummen/rabbitmq-server:3.7.0,docker-compose文件配置的账号密码为myuser/mypass 访问 http://192.168.99.100:15672可以查看界面 实例 @Test public void testProducer() throws InterruptedException { int count = 100; ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.useNio(); connectionFactory.setUsername("myuser"); connectionFactory.setPassword("mypass"); SenderOptions

深入了解Netty【五】线程模型

家住魔仙堡 提交于 2019-12-03 04:30:29
引言 不同的线程模型对程序的性能有很大的影响,Netty是建立在Reactor模型的基础上,要搞清Netty的线程模型,需要了解一目前常见线程模型的一些概念。 具体是进程还是线程,是和平台或者编程语言相关,本文为了描述方便,以线程描述。 目前存在的线程模型有: 传统阻塞IO服务模型 Reactor模型 Proactor模型 1、传统阻塞IO服务模型 采用阻塞IO模型获取输入的数据。 每个连接需要独立的完成数据的输入,业务的处理,数据返回。 当并发数大的时候,会创建大量的线程,占用系统资源,如果连接创建后,当前线程没有数据可读,会阻塞,造成线程资源浪费。 2、Reactor模型 IO多路复用 线程池 = Reactor模型 根据Reactor的数量和处理线程的数量,Reactor模型分为三类: 单Reactor单线程 单Reactor多线程 主从Reactor多线程 下面分别描述。 2.1、单Reactor单线程 图中: Reactor中的select模块就是IO多路复用模型中的选择器,可以通过一个阻塞对象监听多路连接请求。 Reactor对象通过Select监控客户端请求事件,收到事件后,通过Dispatch进行分发。 如果是 建立连接 事件,则用Acceptor通过Accept处理连接请求,然后创建一个Handler对象,处理连接完成后的业务处理。 如果不是建立连接事件

Akka or Reactor [closed]

梦想与她 提交于 2019-12-03 01:32:03
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . I am in the process of starting a new project (java-based). I need to build it as a modular, distributed and resilient architecture. Therefore I would like to have the business processes to communicate among themselves, be interoperable, but also independent. I am looking

Correct way of throwing exceptions with Reactor

社会主义新天地 提交于 2019-12-02 16:47:05
I'm new to project Reactor and reactive programming in general. I'm currently working on a piece of code similar to this: Mono.just(userId) .map(repo::findById) .map(user-> { if(user == null){ throw new UserNotFoundException(); } return user; }) // ... other mappings This example is probably silly and there are surely better ways of implementing this case, but the point is: Is it wrong to use a throw new exception in a map block or should I replace this with a return Mono.error(new UserNotFoundException()) ? Is there any actual difference in these two ways of doing? Oleh Dokuka There are a

聊聊reactor-netty的AccessLog

不打扰是莪最后的温柔 提交于 2019-12-02 15:21:21
序 本文主要研究一下reactor-netty的AccessLog 开启access log 对于使用tomcat的spring boot应用,可以server.tomcat.accesslog.enabled=true来开启 对于使用jetty的spring boot应用,可以server.jetty.accesslog.enabled=true来开启 对于使用undertow的spring boot应用,可以server.undertow.accesslog.enabled=true来开启 对于使用webflux的应用,没有这么对应的配置,但是可以通过-Dreactor.netty.http.server.accessLogEnabled=true来开启 ReactorNetty reactor-netty-0.8.5.RELEASE-sources.jar!/reactor/netty/ReactorNetty.java /** * Internal helpers for reactor-netty contracts * * @author Stephane Maldini */ public final class ReactorNetty { //...... // System properties names /** * Default worker thread

聊聊reactor extra的retry

谁说胖子不能爱 提交于 2019-12-02 15:20:52
序 本文主要研究一下reactor extra的retry maven <dependency> <groupId>io.projectreactor.addons</groupId> <artifactId>reactor-extra</artifactId> <version>3.1.4.RELEASE</version> </dependency> 实例 TcpClient client = TcpClient.create("localhost", 8888); client.newHandler((inbound,outbound) -> { return outbound.sendString(Mono.just("Hello World!")).then(inbound.receive() .asString().next().log().then()); }).doOnError(e -> e.printStackTrace()) .subscribe(); 上面这个TcpClient,在server没有启动的情况下连接会直接报错 io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:8888 at sun.nio.ch