博客
关于我
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中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>
MySql中给视图添加注释怎么添加_默认不支持_可以这样取巧---MySql工作笔记002
查看>>
Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
查看>>
Mysql中视图的使用以及常见运算符的使用示例和优先级
查看>>
Mysql中触发器的使用示例
查看>>
Mysql中设置只允许指定ip能连接访问(可视化工具的方式)
查看>>
mysql中还有窗口函数?这是什么东西?
查看>>