netty udp实现

1.pom.xml



	4.0.0

	com.cloudtech
	AWS310
	0.0.1-SNAPSHOT
	war

	AWS310
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.5.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
		
		    com.alibaba.boot
		    dubbo-spring-boot-starter
		    0.2.0
		
				

		
		
			io.netty
			netty-all
			 5.0.0.Alpha2
		
		
		
		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		
	

	
		
			
			
				org.apache.maven.plugins
				maven-war-plugin
				
					false
				
			
			
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



2.启动项目就开始加载

package com.cloudtech.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.cloudtech.config.NettyConfig;
import com.cloudtech.server.Server;
import com.cloudtech.util.Consts;

@SpringBootApplication
@ComponentScan(value = {"com.cloudtech"})
public class Aws310Application implements CommandLineRunner {
	protected static final Logger LOGGER = LoggerFactory.getLogger(Aws310Application.class);
	
	@Autowired
	NettyConfig nettyConfig;
	
	public static void main(String[] args) {
		SpringApplication.run(Aws310Application.class, args);
	}
    
	/**
	 * spring boot启动后,会进入该方法
	 */
	@Override
	public void run(String... args) throws Exception {
		init();
		//加载分钟监听线成
		new Thread("AWS310-recevice-thread") {
			@Override
			public void run() {
				super.run();
				Server server = new Server();
				server.start();
			}
		}.start();
	}

	/**
	 * 初始化信息
	 */
	@SuppressWarnings("unused")
	private void init() {
		Consts.SERVER_PORT = nettyConfig.getPort();
		Consts.MODULE_TYPE = nettyConfig.getModuleType();
	}
}

这里需要实现一个线程,不然会导致堵塞主线程

package com.cloudtech.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloudtech.util.Consts;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * 
* @ClassName: Server  
* @Description:netty服务端   
* @author wude  
* @date 2018年9月7日  
*
 */
public class Server {
	protected static final Logger logger = LoggerFactory.getLogger(Server.class);

	/**
	 * 启动
	 */
	public void start() {
		// 服务类
		Bootstrap b = new Bootstrap();

		// 创建boss和worker
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		//业务线程池,实现消息串行化
		EventLoopGroup busyGroup = new NioEventLoopGroup();

		try {
			// 设置循环线程组事例
			b.group(bossGroup);

			// 设置channel工厂
			b.channel(NioDatagramChannel.class);
			b.handler(new ServerHandler());

			// 设置管道
		/*	b.childHandler(new ChannelInitializer() {
				@Override
				public void initChannel(SocketChannel ch) throws Exception {
					//第一个参数 读超时
					//第二个参数 写超时
					//第三个参数读写超时
					ch.pipeline().addLast(new IdleStateHandler(5, 5, 60));
					ch.pipeline().addLast(new RequestDecoder());
					ch.pipeline().addLast(new RequestEncoder());
					ch.pipeline().addLast(busyGroup,new ServerHandler());
				}
			});*/

			b.option(ChannelOption.SO_BACKLOG, 2048);//serverSocketchannel的设置,链接缓冲池的大小

			logger.info("启动AWS310分钟采集成功!port={}",Consts.SERVER_PORT);
			//绑定端口
			b.bind(Consts.SERVER_PORT).sync().channel().closeFuture().await();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			//释放资源
			bossGroup.shutdownGracefully();
			busyGroup.shutdownGracefully();
		}
	}

}

3.业务类实现

	@Override
	protected void messageReceived(ChannelHandlerContext ctx, DatagramPacket dataPacket) throws Exception {
		ByteBuf buffer = Unpooled.buffer(7);
		buffer.writeByte(ResponseCode.REGISTER_REPLY);
		buffer.writeShort(0);
		buffer.writeShort(1234);
		buffer.writeByte(1);
		buffer.writeByte(0);
		
		DatagramPacket dp = new DatagramPacket(buffer, dataPacket.sender());
		ctx.writeAndFlush(dp);
		//handlerMessage(new SessionImpl(ctx.channel()), dataPacket);
	}

如果你热衷技术,喜欢交流,欢迎加入我们!

netty udp实现_第1张图片

你可能感兴趣的:(netty)