博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java springboot netty 史上最简单的网关转发程序
阅读量:322 次
发布时间:2019-03-01

本文共 6429 字,大约阅读时间需要 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/

你可能感兴趣的文章