springboot整合netty的步骤

springboot整合netty框架

 Netty 是一个基于NIO的客户、服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、服务端应用。Netty相当于简化和流线化了网络应用的编程开发过程,例如:基于TCP和UDP的socket服务开发。


那么如何和springboot这个比较流行的框架进行整合呢?Netty与SpringBoot的整合,我想无非就是要整合几个地方

让netty跟springboot生命周期保持一致,同生共死
让netty能用上ioc中的Bean
让netty能读取到全局的配置


首先整个项目引入pom

<dependency>
	<groupId>io.netty</groupId>
	<artifactId>netty-all</artifactId>
</dependency>

是的,不用声明版本号。因为 spring-boot-dependencies 中已经声明了最新的netty依赖。


配置Netty的配置类

一、通过yaml配置基本的属性

server:
  port: 80

logging:
    level:
      root: DEBUG

management:
  endpoints: 
    web:
      exposure:
        include: "*"
    
  endpoint:
    shutdown:
      enabled: true

netty:
  websocket:
    # Websocket服务端口
    port: 1024
    # 绑定的网卡
    ip: 0.0.0.0
    # 消息帧最大体积
    max-frame-size: 10240
    # URI路径
    path: /channel

通过 ApplicationRunner 启动Websocket服务

二、在Controller里新建NettyBootsrapRunner 类

import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.springboot.netty.websocket.handler.WebsocketMessageHandler;

/**
 * 初始化Netty服务
 * @author Administrator
 */
@Component
public class NettyBootsrapRunner implements ApplicationRunner, ApplicationListener<ContextClosedEvent>, ApplicationContextAware {

	private static final Logger LOGGER = LoggerFactory.getLogger(NettyBootsrapRunner.class);
	
	@Value("${netty.websocket.port}")
	private int port;

	@Value("${netty.websocket.ip}")
	private String ip;
	
	@Value("${netty.websocket.path}")
	private String path;
	
	@Value("${netty.websocket.max-frame-size}")
	private long maxFrameSize;
	
	private ApplicationContext applicationContext;
	
	private Channel serverChannel;
	
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public void run(ApplicationArguments args) throws Exception {
		
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(bossGroup, workerGroup);
			serverBootstrap.channel(NioServerSocketChannel.class);
			serverBootstrap.localAddress(new InetSocketAddress(this.ip, this.port));
			serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel socketChannel) throws Exception {
					ChannelPipeline pipeline = socketChannel.pipeline();
					pipeline.addLast(new HttpServerCodec());
					pipeline.addLast(new ChunkedWriteHandler());
					pipeline.addLast(new HttpObjectAggregator(65536));
					pipeline.addLast(new ChannelInboundHandlerAdapter() {
						@Override
						public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
							if(msg instanceof FullHttpRequest) {
								FullHttpRequest fullHttpRequest = (FullHttpRequest) msg;
								String uri = fullHttpRequest.uri();
								if (!uri.equals(path)) {
									// 访问的路径不是 websocket的端点地址,响应404
									ctx.channel().writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND))
										.addListener(ChannelFutureListener.CLOSE);
									return ;
								}
							}
							super.channelRead(ctx, msg);
						}
					});
					pipeline.addLast(new WebSocketServerCompressionHandler());
					pipeline.addLast(new WebSocketServerProtocolHandler(path, null, true, maxFrameSize));

					/**
					 * 从IOC中获取到Handler
					 */
					pipeline.addLast(applicationContext.getBean(WebsocketMessageHandler.class));
				}
			});
			Channel channel = serverBootstrap.bind().sync().channel();	
			this.serverChannel = channel;
			LOGGER.info("websocket 服务启动,ip={},port={}", this.ip, this.port);
			channel.closeFuture().sync();
		} finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}

	public void onApplicationEvent(ContextClosedEvent event) {
		if (this.serverChannel != null) {
			this.serverChannel.close();
		}
		LOGGER.info("websocket 服务停止");
	}
}



NettyBootsrapRunner 实现了 ApplicationRunner, ApplicationListener, ApplicationContextAware 接口。

这样一来,NettyBootsrapRunner 可以在App的启动和关闭时执行Websocket服务的启动和关闭。而且通过 ApplicationContextAware 还能获取到 ApplicationContext

1.通过IOC管理 Netty 的Handler

WebsocketMessageHandler

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.springboot.netty.service.DiscardService;
/**
 * 
 * @author Administrator
 *
 */
@Sharable
@Component
public class WebsocketMessageHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(WebsocketMessageHandler.class);
	
	@Autowired
	DiscardService discardService;
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame msg) throws Exception {
		if (msg instanceof TextWebSocketFrame) {
			TextWebSocketFrame textWebSocketFrame = (TextWebSocketFrame) msg;
			// 业务层处理数据
			this.discardService.discard(textWebSocketFrame.text());
			// 响应客户端
			ctx.channel().writeAndFlush(new TextWebSocketFrame("我收到了你的消息:" + System.currentTimeMillis()));
		} else {
			// 不接受文本以外的数据帧类型
			ctx.channel().writeAndFlush(WebSocketCloseStatus.INVALID_MESSAGE_TYPE).addListener(ChannelFutureListener.CLOSE);
		}
	}
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		super.channelInactive(ctx);
		LOGGER.info("链接断开:{}", ctx.channel().remoteAddress());
	}
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		super.channelActive(ctx);
		LOGGER.info("链接创建:{}", ctx.channel().remoteAddress());
	}
}

handler已经是一个IOC管理的Bean,可以自由的使用依赖注入等Spring带来的快捷功能。由于是单例存在,所有的链接都使用同一个hander,所以尽量不要保存任何实例变量。

这个Handler处理完毕客户端的消息后,给客户端会响应一条:“我收到了你的消息:” + System.currentTimeMillis() 的消息

为了演示在Handler中使用业务层,这里假装注入了一个 DiscardService 服务。它的逻辑很简单,就是丢弃消息

2.DiscardService 服务

代码如下(示例):

public void discard (String message) {
	LOGGER.info("丢弃消息:{}", message);
}

演示

启动客户端

<!DOCTYPE html>
	<html>
	<head>
		<meta charset="UTF-8">
		<title>Websocket</title>
	</head>
	<body>
	
	</body>
	<script type="text/javascript">
		;(function(){
			const websocket = new WebSocket('ws://localhost:1024/channel');
			websocket.onmessage = e => {
				console.log('收到消息:', e.data);
			}
			websocket.onclose = e => {
				let {code, reason} = e;
				console.log(`链接断开:code=${code}, reason=${reason}`);
			}
			websocket.onopen = () => {
				console.log(`链接建立...`);
				websocket.send('Hello');
			}
			websocket.onerror = e => {
				console.log('链接异常:', e);
			}
		})();

	</script>
</html>




链接创建后就给服务端发送一条消息:Hello。Netty会在SpringBoot App启动后启动,App停止后关闭,可以正常的对外提供服务 并且Handler交给IOC管理可以注入Service,完成业务处理。

### 回答1: Spring Boot可以很方便地整合Netty,实现TCP协议的通信。具体实现步骤如下: 1. 引入Netty依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.25.Final</version> </dependency> ``` 2. 编写Netty服务端 编写一个Netty服务端,监听指定端口,接收客户端的请求,并返回响应。具体实现可以参考Netty官方文档。 3. 配置Spring Boot 在Spring Boot的配置文件中,配置Netty服务端的端口号和其他相关参数。 4. 启动Spring Boot应用程序 启动Spring Boot应用程序,Netty服务端会自动启动并监听指定端口。 5. 编写客户端程序 编写一个客户端程序,连接Netty服务端,并发送请求。具体实现可以参考Netty官方文档。 通过以上步骤,就可以实现Spring Boot整合Netty,实现TCP协议的通信。 ### 回答2: Spring Boot是一个非常流行的Java开源框架,它提供了一种简单且快捷的方式来构建可扩展的Web应用程序。而Netty是一个基于NIO的客户端/服务器框架,它可以轻松处理高负载的网络通信。 因此通过Spring Boot和Netty整合,可以实现高效,快速,可扩展的TCP通信,在需要高性能可扩展的应用程序中是有很大的优势的。 实现过程如下: 1. 通过Spring Boot创建一个Maven项目,引入Netty依赖。 2. 创建Netty服务端和客户端,用于实现TCP通讯。服务端可以监听端口,客户端则可以连接服务端。 3. 将Netty的ChannelHandler封装成Spring Bean,并在Spring Boot中进行注入。 4. 通过使用Spring Boot的自动配置功能,将服务端和客户端的配置信息进行注入,从而使整个过程的配置更加简单。 5. 为了更好地支持多个客户端并发操作,可以使用Netty的线程池功能来提高性能和稳定性。 6. 配置Spring Boot,使其运行在指定的端口,并且注册Netty ChannelHandler,使其能够接收和处理来自客户端的请求消息。 7. 编写客户端代码,建立与服务端的连接并发送数据。 8. 客户端与服务端完成通信后,可以将数据响应给客户端,并断开连接。 通过以上步骤,就可以使用Spring Boot和Netty实现高效,快速,可扩展的TCP通信。这种架构有很多优点,例如高并发,高性能,易于维护,容易扩展等。对于需要实现实时数据传输和高性能的应用程序而言,这是一种非常好的解决方案。 ### 回答3: Springboot是一款非常流行的Java开发框架,它提供了很多便捷的工具和库,帮助开发者更快地搭建高效的应用程序。Netty则是一款基于NIO的高性能网络通信框架,非常适合开发高并发、高性能的网络应用。 利用Springboot整合Netty实现TCP通信可以方便地实现异步、非阻塞IO,而不需要开发者手动处理Java NIO的细节。下面简要介绍如何利用Springboot整合Netty实现TCP通信。 1. 引入Netty的依赖 在pom.xml文件中引入Netty的依赖,例如: ``` <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.25.Final</version> </dependency> ``` 2. 实现Netty服务端 创建一个NettyServer类,继承自ChannelInboundHandlerAdapter,并实现以下方法: ``` public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {} public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {} public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {} ``` 在NettyServer类的构造方法中启动Netty服务端,示例代码如下: ``` public class NettyServer extends ChannelInboundHandlerAdapter { public NettyServer() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.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(new NettyServer()); } }); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 处理读事件 } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // 读事件完成处理 } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 处理异常 } // 启动Netty服务端 public static void main(String[] args) { new NettyServer(); } } ``` 3. 实现Netty客户端 创建一个NettyClient类,继承自SimpleChannelInboundHandler,并实现以下方法: ``` public void channelActive(ChannelHandlerContext ctx) throws Exception {} protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {} public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {} ``` 在NettyClient类的构造方法中启动Netty客户端,示例如下: ``` public class NettyClient extends SimpleChannelInboundHandler<String> { private final String host; private final int port; public NettyClient(String host, int port) { this.host = host; this.port = port; EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, Unpooled.copiedBuffer("$_".getBytes()))); ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new NettyClient(host, port)); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // 发送消息 } @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // 处理读事件 } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 处理异常 } // 启动Netty客户端 public static void main(String[] args) { String host = "127.0.0.1"; int port = 8080; new NettyClient(host, port); } } ``` 以上是利用Springboot整合Netty实现TCP通信的大致步骤。实际开发过程中还需要根据应用程序的具体需求进一步优化和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wyangcsdb

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

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

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

打赏作者

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

抵扣说明:

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

余额充值