Netty整合Disruptor实战

1、Netty实现服务端与客户端数据传输

1)、依赖
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.12.Final</version>
        </dependency>
        <!-- 序列化框架marshalling -->
        <dependency>
            <groupId>org.jboss.marshalling</groupId>
            <artifactId>jboss-marshalling</artifactId>
            <version>1.3.0.CR9</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.marshalling</groupId>
            <artifactId>jboss-marshalling-serial</artifactId>
            <version>1.3.0.CR9</version>
        </dependency>
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
2)、common
@Data
public class TranslatorData implements Serializable {
    private String id;

    private String name;

    private String message;
}
/**
 * Marshalling工厂
 */
public final class MarshallingCodeCFactory {

    /**
     * 创建Jboss Marshalling解码器MarshallingDecoder
     *
     * @return MarshallingDecoder
     */
    public static MarshallingDecoder buildMarshallingDecoder() {
        // 首先通过Marshalling工具类的精通方法获取Marshalling实例对象 参数serial标识创建的是java序列化工厂对象。
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        // 创建了MarshallingConfiguration对象,配置了版本号为5
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        // 根据marshallerFactory和configuration创建provider
        UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
        // 构建Netty的MarshallingDecoder对象,俩个参数分别为provider和单个消息序列化后的最大长度
        return new MarshallingDecoder(provider, 1024 * 1024 * 1);
    }

    /**
     * 创建Jboss Marshalling编码器MarshallingEncoder
     *
     * @return MarshallingEncoder
     */
    public static MarshallingEncoder buildMarshallingEncoder() {
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
        // 构建Netty的MarshallingEncoder对象,MarshallingEncoder用于实现序列化接口的POJO对象序列化为二进制数组
        return new MarshallingEncoder(provider);
    }
}
3)、服务端
public class NettyServer {
    public NettyServer() {
        // 创建两个工作线程组:一个用于接收网络请求的,另一个用于实际处理业务的
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    // 缓存区动态调配(自适应)
                    .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
                    // 缓冲区池化操作
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
                            sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
                            sc.pipeline().addLast(new ServerHandler());
                        }
                    });
            // 绑定端口,同步等等请求连接
            ChannelFuture cf = serverBootstrap.bind(8765).sync();
            System.out.println("Server Startup...");
            cf.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 优雅停机
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            System.out.println("Sever ShutDown...");
        }
    }
}
public class ServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        TranslatorData request = (TranslatorData) msg;
        System.out.println("Server端:id=" + request.getId()
                + ", name=" + request.getName()
                + ", message=" + request.getMessage());
        TranslatorData response = new TranslatorData();
        response.setId("resp:" + request.getId());
        response.setName("resp:" + request.getName());
        response.setMessage("resp:" + request.getMessage());
        ctx.writeAndFlush(response);
    }
}
@SpringBootApplication
public class NettyServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NettyServerApplication.class, args);
        new NettyServer();
    }

}
4)、客户端
public class NettyClient {
    public static final String HOST = "127.0.0.1";

    public static final int PORT = 8765;

    private EventLoopGroup group = new NioEventLoopGroup();

    private Channel channel;

    private ChannelFuture cf;

    public NettyClient() {
        this.connect(HOST, PORT);
    }

    private void connect(String host, int port) {
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    // 缓存区动态调配(自适应)
                    .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
                    // 缓冲区池化操作
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
                            sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
                            sc.pipeline().addLast(new ClientHandler());
                        }
                    });
            this.cf = bootstrap.connect(host, port).sync();
            System.out.println("Client connected...");
            this.channel = cf.channel();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void sendData() {
        for (int i = 0; i < 10; i++) {
            TranslatorData request = new TranslatorData();
            request.setId("" + i);
            request.setName("请求消息名称" + i);
            request.setMessage("请求消息内容" + i);
            this.channel.writeAndFlush(request);
        }
    }

    public void close() throws Exception {
        cf.channel().closeFuture().sync();
        // 优雅停机
        group.shutdownGracefully();
        System.out.println("Sever ShutDown...");
    }
}
public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            TranslatorData response = (TranslatorData) msg;
            System.out.println("Client端:id=" + response.getId()
                    + ", name=" + response.getName()
                    + ", message=" + response.getMessage());
        } finally {
            // 释放缓存
            ReferenceCountUtil.release(msg);
        }
    }
}
@SpringBootApplication
public class NettyClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(NettyClientApplication.class, args);
        // 建立连接并发送消息
        new NettyClient().sendData();
    }

}

2、集成Disruptor

在这里插入图片描述

1)、common
@Data
public class TranslatorDataWrapper {
    private TranslatorData translatorData;

    private ChannelHandlerContext channelHandlerContext;
}
@Data
public abstract class MessageConsumer implements WorkHandler<TranslatorDataWrapper> {
    protected String consumerId;

    public MessageConsumer(String consumerId) {
        this.consumerId = consumerId;
    }
}
public class MessageProducer {
    private String producerId;
    private RingBuffer<TranslatorDataWrapper> ringBuffer;

    public MessageProducer(String producerId,
                           RingBuffer<TranslatorDataWrapper> ringBuffer) {
        this.producerId = producerId;
        this.ringBuffer = ringBuffer;
    }

    public void sendData(TranslatorData data, ChannelHandlerContext ctx) {
        long sequence = ringBuffer.next();
        try {
            TranslatorDataWrapper wrapper = ringBuffer.get(sequence);
            wrapper.setTranslatorData(data);
            wrapper.setChannelHandlerContext(ctx);
        } finally {
            ringBuffer.publish(sequence);
        }
    }
}
public class RingBufferWorkerPoolFactory {
    private static class SingletonHolder {
        static final RingBufferWorkerPoolFactory INSTANCE = new RingBufferWorkerPoolFactory();
    }

    private RingBufferWorkerPoolFactory() {

    }

    public static RingBufferWorkerPoolFactory getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static Map<String, MessageProducer> producers = new ConcurrentHashMap<>();

    private static Map<String, MessageConsumer> consumers = new ConcurrentHashMap<>();

    private RingBuffer<TranslatorDataWrapper> ringBuffer;

    private SequenceBarrier sequenceBarrier;

    private WorkerPool<TranslatorDataWrapper> workerPool;

    public void initAndStart(ProducerType producerType,
                             int bufferSize,
                             WaitStrategy waitStrategy,
                             MessageConsumer[] messageConsumers) {
        // 1.构建ringBuffer对象
        this.ringBuffer = RingBuffer.create(producerType,
                new EventFactory<TranslatorDataWrapper>() {
                    @Override
                    public TranslatorDataWrapper newInstance() {
                        return new TranslatorDataWrapper();
                    }
                },
                bufferSize,
                waitStrategy);
        // 2.设置序号栅栏
        this.sequenceBarrier = ringBuffer.newBarrier();
        // 3.设置工作池
        this.workerPool = new WorkerPool<>(
                this.ringBuffer,
                this.sequenceBarrier,
                new EventExceptionHandler(),
                messageConsumers);
        // 4.把所构建的消费者置入池中
        for (MessageConsumer consumer : messageConsumers) {
            consumers.put(consumer.getConsumerId(), consumer);
        }
        // 5.添加sequences
        this.ringBuffer.addGatingSequences(this.workerPool.getWorkerSequences());
        // 6.启动工作池
        this.workerPool.start(
                Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() / 2)
        );
    }

    public MessageProducer getMessageProducer(String producerId) {
        MessageProducer messageProducer = producers.get(producerId);
        if (null == messageProducer) {
            messageProducer = new MessageProducer(producerId, this.ringBuffer);
            producers.put(producerId, messageProducer);
        }
        return messageProducer;
    }

    static class EventExceptionHandler implements ExceptionHandler<TranslatorDataWrapper> {

        @Override
        public void handleEventException(Throwable ex, long sequence, TranslatorDataWrapper event) {

        }

        @Override
        public void handleOnStartException(Throwable ex) {

        }

        @Override
        public void handleOnShutdownException(Throwable ex) {

        }
    }
}
2)、服务端
public class ServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        TranslatorData request = (TranslatorData) msg;
        String producerId = "code:sessionId:001";
        MessageProducer messageProducer = RingBufferWorkerPoolFactory.getInstance().getMessageProducer(producerId);
        messageProducer.sendData(request, ctx);
    }
}
public class MessageConsumerImplForServer extends MessageConsumer {
    public MessageConsumerImplForServer(String consumerId) {
        super(consumerId);
    }

    @Override
    public void onEvent(TranslatorDataWrapper event) throws Exception {
        TranslatorData translatorData = event.getTranslatorData();
        System.out.println("Server端:id=" + translatorData.getId()
                + ", name=" + translatorData.getName()
                + ", message=" + translatorData.getMessage());
        TranslatorData response = new TranslatorData();
        response.setId("resp:" + translatorData.getId());
        response.setName("resp:" + translatorData.getName());
        response.setMessage("resp:" + translatorData.getMessage());
        event.getChannelHandlerContext().writeAndFlush(response);
    }
}
@SpringBootApplication
public class NettyServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NettyServerApplication.class, args);
        MessageConsumer[] consumers = new MessageConsumer[4];
        for (int i = 0; i < consumers.length; ++i) {
            consumers[i] = new MessageConsumerImplForServer("code:serverId:" + i);
        }
        RingBufferWorkerPoolFactory.getInstance().initAndStart(
                ProducerType.MULTI,
                1024 * 1024,
                new BlockingWaitStrategy(),
                consumers);
        new NettyServer();
    }

}
3)、客户端
public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        TranslatorData response = (TranslatorData) msg;
        String producerId = "code:sessionId:002";
        MessageProducer messageProducer = RingBufferWorkerPoolFactory.getInstance().getMessageProducer(producerId);
        messageProducer.sendData(response, ctx);
    }
}
public class MessageConsumerImplForClient extends MessageConsumer {
    public MessageConsumerImplForClient(String consumerId) {
        super(consumerId);
    }

    @Override
    public void onEvent(TranslatorDataWrapper event) throws Exception {
        TranslatorData translatorData = event.getTranslatorData();
        try {
            System.out.println("Client端:id=" + translatorData.getId()
                    + ", name=" + translatorData.getName()
                    + ", message=" + translatorData.getMessage());
        } finally {
            // 释放缓存
            ReferenceCountUtil.release(translatorData);
        }
    }
}
@SpringBootApplication
public class NettyClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(NettyClientApplication.class, args);
        MessageConsumer[] consumers = new MessageConsumer[4];
        for (int i = 0; i < consumers.length; ++i) {
            consumers[i] = new MessageConsumerImplForClient("code:clientId:" + i);
        }
        RingBufferWorkerPoolFactory.getInstance().initAndStart(
                ProducerType.MULTI,
                1024 * 1024,
                new BlockingWaitStrategy(),
                consumers);
        // 建立连接并发送消息
        new NettyClient().sendData();
    }

}

推荐资料

高性能Java并发框架disruptor源码解析与实战

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot可以很方便地实现与Netty整合,下面是一个简单的实现示例: 1. 添加依赖 在项目的`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.48.Final</version> </dependency> ``` 2. 编写Netty服务器 在Spring Boot项目中创建一个Netty服务器,实现以下功能: - 监听指定端口 - 接收客户端连接 - 处理客户端请求 - 返回响应数据 ```java @Component public class NettyServer { @Value("${netty.port}") private int port; @Autowired private NettyServerHandler nettyServerHandler; private ChannelFuture channelFuture; @PostConstruct public void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(); // 接收客户端连接 EventLoopGroup workerGroup = new NioEventLoopGroup(); // 处理客户端请求 ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(nettyServerHandler); } }); channelFuture = serverBootstrap.bind(port).syncUninterruptibly(); } @PreDestroy public void stop() { channelFuture.channel().closeFuture().syncUninterruptibly(); } } ``` 其中,`NettyServerHandler`是一个自定义的ChannelHandler,用于处理客户端请求。 3. 编写Netty客户端 在Spring Boot项目中创建一个Netty客户端,实现以下功能: - 连接指定服务器的指定端口 - 发送请求数据 - 接收响应数据 ```java @Component public class NettyClient { @Value("${netty.server.host}") private String host; @Value("${netty.server.port}") private int port; @Autowired private NettyClientHandler nettyClientHandler; private ChannelFuture channelFuture; @PostConstruct public void start() { EventLoopGroup workerGroup = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workerGroup) .channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(nettyClientHandler); } }); channelFuture = bootstrap.connect(host, port).syncUninterruptibly(); } public void send(Object message) { channelFuture.channel().writeAndFlush(message).syncUninterruptibly(); } @PreDestroy public void stop() { channelFuture.channel().closeFuture().syncUninterruptibly(); } } ``` 其中,`NettyClientHandler`是一个自定义的ChannelHandler,用于接收响应数据。 4. 配置文件 在Spring Boot项目的`application.properties`文件中添加以下配置: ```properties netty.port=8888 netty.server.host=localhost netty.server.port=8888 ``` 其中,`netty.port`是Netty服务器监听的端口,`netty.server.host`和`netty.server.port`是Netty客户端连接的服务器和端口。 5. 使用Netty 在Spring Boot项目中使用Netty,可以注入`NettyClient`和`NettyServer`,然后调用相应的方法即可。 例如,在Controller中发送请求并接收响应: ```java @RestController public class NettyController { @Autowired private NettyClient nettyClient; @PostMapping("/netty") public String netty(@RequestBody String message) { nettyClient.send(message); return "success"; } } ``` 以上就是Spring Boot整合Netty的基本实现。需要注意的是,Netty服务器和客户端的实现都是基于异步非阻塞的模型,因此需要使用`ChannelFuture`来处理相应的事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邋遢的流浪剑客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值