博客
关于我
java springboot netty 史上最简单的网关转发程序
阅读量:324 次
发布时间:2019-03-01

本文共 6417 字,大约阅读时间需要 21 分钟。

最简单的网关转发程序

  1. 监控端口2222,接收端口接到的数据
  2. 将数据从第四位开始截取
  3. 将数据转发到另外一台主机 本示例是 本机 192.168.35.1:3333
    盗用同事梁某版权,

使用技术: springboot netty

git hub 地址 :

https://github.com/wei198621/GetwayForwardByNettyUdp

在这里插入图片描述在这里插入图片描述

4.0.0
com.tiza.leo
UdpGetwayForwardByNetty
1.0-SNAPSHOT
org.apache.maven.plugins
maven-compiler-plugin
8
8
org.springframework.boot
spring-boot-maven-plugin
com.tiza.leo.UdpGetwayForwardByNetty.Boot
repackage
org.springframework.boot
spring-boot-dependencies
2.2.1.RELEASE
pom
import
org.springframework.boot
spring-boot-starter-actuator
io.netty
netty-all
4.1.42.Final
org.projectlombok
lombok
1.18.12
org.apache.commons
commons-lang3
3.11
spring:  profiles:    active: devgateway:  inPort: 2222  outIp: 192.168.35.1  outPort: 3333
package com.tiza.leo.UdpGetwayForwardByNetty;import com.tiza.leo.UdpGetwayForwardByNetty.deamon.GatewayDeamon;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @author leowei * @date 2021/3/30  - 23:00 */@SpringBootApplicationpublic class Boot implements ApplicationRunner {       @Autowired    GatewayDeamon gatewayDeamon;    public static void main(String[] args) {           SpringApplication.run(Boot.class,args);    }    //执行网关收回进程    @Override    public void run(ApplicationArguments args) throws Exception {           gatewayDeamon.start();    }}
package com.tiza.leo.UdpGetwayForwardByNetty.handler;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.channel.socket.DatagramPacket;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;import java.net.InetSocketAddress;/** * @author leowei * @date 2021/3/30  - 23:01 */@Slf4j@ChannelHandler.Sharable@Componentpublic class ReceiveHandler extends ChannelInboundHandlerAdapter {       @Autowired    private Environment env;    /*    1. 从 channel  中取到msg  转换为buf 后 截取前9位扔掉    2. 将剩余的数据打包为一个DatagramPacket 发往 出口     */    @Override    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {           DatagramPacket datagramPacketIn =   (DatagramPacket) msg;        ByteBuf buf = datagramPacketIn.copy().content();        buf.skipBytes(3);        InetSocketAddress address = new InetSocketAddress(env.getProperty("gateway.outIp"), Integer.parseInt(env.getProperty("gateway.outPort")));        DatagramPacket datagramPacketOut = new DatagramPacket(buf, address);        ctx.channel().writeAndFlush(datagramPacketOut).sync();        datagramPacketIn.release();        //super.channelRead(ctx, msg);    }}
package com.tiza.leo.UdpGetwayForwardByNetty.deamon;import com.tiza.leo.UdpGetwayForwardByNetty.handler.ReceiveHandler;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.ChannelPipeline;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioDatagramChannel;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties;import org.springframework.core.env.Environment;import org.springframework.stereotype.Service;/** * @author leowei * @date 2021/3/30  - 23:02 */@Slf4j@Servicepublic class GatewayDeamon {       @Autowired    private Environment env;    @Autowired    private ReceiveHandler receiveHandler;    public void start(){           NioEventLoopGroup bossGroup = new NioEventLoopGroup();        NioEventLoopGroup workGroup = new NioEventLoopGroup();        Integer port = env.getRequiredProperty("gateway.inPort", Integer.class);        log.info("绑定udp端口{}",port);        try {               Bootstrap bootstrap = new Bootstrap();            bootstrap.group(bossGroup)                    .channel(NioDatagramChannel.class)                    .option(ChannelOption.SO_BROADCAST, true)                    //设置了UDP socket读缓冲区为2M                    .option(ChannelOption.SO_RCVBUF, 1024 * 2048)                    .handler(new ChannelInitializer
() { @Override protected void initChannel(NioDatagramChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("receive", receiveHandler); } }); ChannelFuture future = null; future = bootstrap.bind(port).sync(); future.channel().closeFuture().sync(); } catch (Exception e) { log.error(e.getMessage(),e); }finally { bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); } }}

转载地址:http://wrzo.baihongyu.com/

你可能感兴趣的文章
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
Mysql 整形列的字节与存储范围
查看>>
MySQL 日期时间类型的选择
查看>>
MySQL 是如何加锁的?
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>