Spring环境下使用Netty写Socket和Http详解

news/2024/5/17 18:00:17 标签: spring, netty, socket, http, tcp
http://www.w3.org/2000/svg" style="display: none;">

Spring环境下使用Netty写Socket和Http详解

文章本来名字是《Spring和Netty整合详解》,因为它跟Spring整合并不严谨,固更名为《Spring环境下使用Netty写Socket和Http详解》,后面会补充一篇《Spring和Netty整合详解》。

官方主页

Spring

Netty

一、概述

Netty是目前最流行的由JBOSS提供的一个Java开源框架NIO框架,Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。

相比JDK原生NIO,Netty提供了相对十分简单易用的API,非常适合网络编程。Netty是完全基于NIO实现的,所以Netty是异步的。

Mina同样也是一款优秀的NIO框架,而且跟Netty是出自同一个人之手,但是Netty要晚一点,优点更多一些,想了解更多可以直接搜索mina和netty比较。

使用Netty,我们可以作为Socket服务器,也可以用来做Http服务器,这里,我们将这两种方式都详细介绍一下。

Git地址:
Gitee

项目地址:
品茗IT-同步发布

品茗IT 提供在线支持:

一键快速构建Spring项目工具

一键快速构建SpringBoot项目工具

一键快速构建SpringCloud项目工具

一站式Springboot项目生成

如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。

二、依赖Jar包

我们假定你已经建好了Spring环境。这里只引入Netty的jar包。

<dependency>
	<groupId>io.netty</groupId>
	<artifactId>netty-all</artifactId>
	<version>4.1.17.Final</version>
</dependency>

完整依赖可以在Spring组建化构建的NettyNew组件中查看

父pom地址:
https://www.pomit.cn/spring/SpringWork/pom.xml

三、Socket服务器和Http服务器的使用时的些许差异

可以说,没有差异,只是ChannelHandler不同而已。

因此,这里先说明一些公共的使用,然后再针对sockethttp做区分。

四、Netty服务器配置

我们可以编写一个公共的服务器配置模板NettyServiceTemplate,这个模板既可以被tcp使用,也可以被http使用。
NettyServiceTemplate:

package cn.pomit.springwork.nettynew.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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 javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public abstract class NettyServiceTemplate {
	static private EventLoopGroup bossGroup = new NioEventLoopGroup();
	static private EventLoopGroup workerGroup = new NioEventLoopGroup();

	abstract protected ChannelHandler[] createHandlers();

	abstract public int getPort();

	abstract public String getName();

	@PostConstruct
	public void start() throws Exception {
		ServerBootstrap b = new ServerBootstrap();
		b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
				.childHandler(new ChannelInitializer<SocketChannel>() {
					@Override
					public void initChannel(SocketChannel ch) throws Exception {
						ChannelHandler[] handlers = createHandlers();
						for (ChannelHandler handler : handlers) {
							ch.pipeline().addLast(handler);
						}
					}
				}).option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_REUSEADDR, true)
				.childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.SO_REUSEADDR, true);

		ChannelFuture cf = b.bind(getPort()).await();
		// cf.channel().closeFuture().await();
		if (!cf.isSuccess()) {
			System.out.println("无法绑定端口:" + getPort());
			throw new Exception("无法绑定端口:" + getPort());
		}

		System.out.println("服务[{" + getName() + "}]启动完毕,监听端口[{" + getPort() + "}]");
	}

	@PreDestroy
	public void stop() {
		bossGroup.shutdownGracefully().syncUninterruptibly();
		workerGroup.shutdownGracefully().syncUninterruptibly();
		System.out.println("服务[{" + getName() + "}]关闭。");
	}
}

代码贴出来之后,我们还是要讲下里面的一些重点:

bossGroup和workerGroup就是为了建立线程池,这个自行百度。

ChannelHandler使用抽象方法由子类去实现,这样就可以根据不同的ChannelHandler实现不同的功能。

以上注释了

cf.channel().closeFuture().await();

这部分代码是阻塞了当前主线程一直等待结束,后面的代码就不能执行了。

两次await的不同:两次await虽然都是针对ChannelFuture的,但是两次的ChannelFuture不一样,打断点看了下,分别是:

AbstractBootstrap$PendingRegistrationPromise@177655b7(success)
AbstractChannel$CloseFuture@3c4f9fbb(incomplete)

原理就不细说了,我也不知道,毕竟不是专精这方面的。

五、启动Server

这里先说server的启动,下面会写具体server的实现。

5.1 直接启动

因为后面要写不同的Server,所以这里先把启动Server写出来,免得眼晕。

我们可以直接脱离Spring环境启动,如果不放心也可以放到线程池中启动:

TestApp:

package cn.pomit.springwork.nettynew;

import cn.pomit.springwork.nettynew.server.http.JsonHttpServer;
import cn.pomit.springwork.nettynew.server.tcp.StringTcpServer;

public class TestApp {
	
	public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException{		
		StringTcpServer stringTcpServerTest = new StringTcpServer(8088);
		JsonHttpServer jsonHttpServer = new JsonHttpServer(8880);
		try {
			stringTcpServerTest.start();
			jsonHttpServer.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

5.2 Spring环境下启动

Spring环境下,将每个Server加上@Service注解,因为父类有注解@PostConstruct,所以可以调用start方法启动server。
@Service注解的的service可以通过@Autowired注入需要使用的bean。

注意: 理论上ChannelHandler也可以作为bean的,但是netty要求ChannelHandler是每个线程一份的,就算指定bean的scope是原型也无效。当然可以使用@Sharable注解解决,@Sharable注解的handler可以被复用。但没特殊需要,尽量避免,使用了@Sharable,又会牵扯到线程安全问题,我们的bean是可以通过引用传入来使用的。

下面的xml只是将server作为bean做配置,并没使用bean注入等。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:jms="http://www.springframework.org/schema/jms" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                    http://www.springframework.org/schema/aop 
                    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                    http://www.springframework.org/schema/context      
                    http://www.springframework.org/schema/context/spring-context-4.0.xsd
                    http://www.springframework.org/schema/cache 
                    http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
                    http://www.springframework.org/schema/jms 
                    http://www.springframework.org/schema/jms/spring-jms-4.0.xsd   
                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">

	<context:annotation-config />
	<context:component-scan base-package="com.cff.springwork">
	</context:component-scan>
	
	<bean id="annotationPropertyConfigurerNettyNew"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<value>classpath:nettynew.properties</value>
			</list>
		</property>
	</bean>
	<bean id="stringTcpServer" class="cn.pomit.springwork.nettynew.server.tcp.StringTcpServer" init-method="start" destroy-method="stop">
		<property name="port" value="${nettynew.tcp.port}" />
		<property name="name" value="${nettynew.tcp.name}" />
	</bean>
	<bean id="jsonHttpServer" class="cn.pomit.springwork.nettynew.server.http.JsonHttpServer" init-method="start" destroy-method="stop">
		<property name="port" value="${nettynew.http.port}" />
		<property name="name" value="${nettynew.http.name}" />
	</bean>
</beans>

配置文件nettynew.properties:

nettynew.tcp.port=6666
nettynew.tcp.name=TcpServer
nettynew.http.port=6666
nettynew.http.name=HttpServer

六、Socket(Tcp)的Server配置

6.1 Socket(Tcp)的Server

如果我们单纯的使用tcp传递数据,比如String数据,我们可以这样定义一个Server:

StringTcpServer:

package cn.pomit.springwork.nettynew.server.tcp;

import cn.pomit.springwork.nettynew.handler.tcp.StringTcpServerHandler;
import cn.pomit.springwork.nettynew.server.NettyServiceTemplate;
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class StringTcpServer extends NettyServiceTemplate {
	private int port = 8088;
	private String name = "String Server";

	// SpringBeanService springBeanService;

	public StringTcpServer(int port) {
		this.port = port;
	}

	// 可以启动server时将spring的bean传递进来。
	// public StringTcpServerTest(int port, SpringBeanService springBeanService)
	// {
	// this.port = port;
	// this.springBeanService = springBeanService;
	// }

	@Override
	protected ChannelHandler[] createHandlers() {
		return new ChannelHandler[] { 
				new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()),
				new StringDecoder(), 
				new StringEncoder(),

				// 如果想使用spring的bean,可以将springBeanService传递给StringTcpServerHandler,如new
				// StringTcpServerHandler(springBeanService)
				new StringTcpServerHandler() };
	}

	@Override
	public int getPort() {
		return port;
	}

	@Override
	public String getName() {
		return name;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setName(String name) {
		this.name = name;
	}

}

这里,DelimiterBasedFrameDecoder定义了 以("\n")为结尾分割的 解码器。

然后是String解码与编码器

然后是自定义的一个处理器Handler。

6.2 Socket(Tcp)的ChannelHandler

StringTcpServerHandler:

package cn.pomit.springwork.nettynew.handler.tcp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class StringTcpServerHandler extends SimpleChannelInboundHandler<String> {
	String charset = "UTF-8";

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		System.out.println("内容:" + msg);
		ctx.writeAndFlush("返回内容:" + msg);
	}
}

七、HTTP 的Server配置

7.1 HTTP 的Server

如果我们想使用http传输json数据,我们可以这样玩:

JsonHttpServer:

package cn.pomit.springwork.nettynew.server.http;

import org.springframework.stereotype.Service;

import cn.pomit.springwork.nettynew.coder.http.HttpMsgRequestDecoder;
import cn.pomit.springwork.nettynew.coder.http.HttpMsgResponseEncoder;
import cn.pomit.springwork.nettynew.handler.http.JsonHttpServerHandler;
import cn.pomit.springwork.nettynew.server.NettyServiceTemplate;
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

public class JsonHttpServer extends NettyServiceTemplate {
	int port = 8888;
	String name = "Json Server";
	private String charset = "UTF-8";
	private int timeout = 60;
	
	public JsonHttpServer(int port) {
		this.port = port;
	}

	@Override
	protected ChannelHandler[] createHandlers() {
		return new ChannelHandler[] { 
				new HttpResponseEncoder(), 
				new HttpRequestDecoder(),
				new HttpObjectAggregator(1048576), 
				new HttpMsgResponseEncoder(charset, timeout),
				new HttpMsgRequestDecoder(charset), 
				new JsonHttpServerHandler() };
	}

	@Override
	public int getPort() {
		return port;
	}

	@Override
	public String getName() {
		return name;
	}

}


其中,HttpResponseEncoder和HttpRequestDecoder是编码解码器。
HttpObjectAggregator将请求合并,如果没有,同一个http请求我们可能需要处理两次。

HttpMsgResponseEncoder和HttpMsgRequestDecoder是自定义的通用http处理方式。JsonHttpServerHandler是自定义的json处理器。

我们可以自定义一个http实体HttpResponseMsg,方便处理http响应等:

7.2 HTTP 的ChannelHandler

JsonHttpServerHandler:

package cn.pomit.springwork.nettynew.handler.http;

import cn.pomit.springwork.nettynew.model.http.HttpResponseMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class JsonHttpServerHandler extends SimpleChannelInboundHandler<String> {
	String charset = "UTF-8";

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		System.out.println("post内容:" + msg);
		HttpResponseMsg hrm = new HttpResponseMsg();
    	hrm.setResType(HttpResponseMsg.ResType.JSON.getValue());
    	hrm.setResCode(HttpResponseMsg.ResCode.OK.getValue());
    	hrm.setMessage(msg);
    	ctx.writeAndFlush(hrm);
	}

}

7.3 HTTP 的实体

HttpResponseMsg:

package cn.pomit.springwork.nettynew.model.http;

public class HttpResponseMsg {
	public enum ResType {  
		HTML("text/html"),
	    JSON("application/json"),
	    JS("application/javascript"),
	    PNG("image/png"),
	    JPG("image/jpg");
	    String value = null;
	    ResType(String value) {
	        this.value = value;
	    }
	    public String getValue() {
	        return value;
	    }
	}  
	
	public enum ResCode {  
		NOT_FOUND(404),
	    OK(200),
	    INTERNAL_ERROR(500);
	    int value = 200;
	    ResCode(int value) {
	        this.value = value;
	    }
	    public int getValue() {
	        return value;
	    }
	}  
	public int resCode;
	
	public String resType;
	
	public String message;

	public int getResCode() {
		return resCode;
	}

	public void setResCode(int resCode) {
		this.resCode = resCode;
	}

	public String getResType() {
		return resType;
	}

	public void setResType(String resType) {
		this.resType = resType;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}


7.4 HTTP 的自定义的Encoder和Decoder

HttpMsgResponseEncoder:

package cn.pomit.springwork.nettynew.coder.http;

import java.util.List;

import cn.pomit.springwork.nettynew.model.http.HttpResponseMsg;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

public class HttpMsgResponseEncoder extends MessageToMessageEncoder<HttpResponseMsg> {
	private String charset;
	private int timeout;

	public HttpMsgResponseEncoder(String charset, int timeout) {
		super();
		this.charset = charset;
		this.timeout = timeout;
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, HttpResponseMsg message, List<Object> out) {
		try {
			DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(message.getResCode()),
					Unpooled.wrappedBuffer(message.getMessage().getBytes(charset)));
			response.headers().set(HttpHeaderNames.CONTENT_TYPE, message.getResType()+";charset=" + charset);
			response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());

			// 强制keep-alive
			response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
			response.headers().set("Keep-Alive", "timeout=" + timeout);

			out.add(response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

HttpMsgRequestDecoder:

package cn.pomit.springwork.nettynew.coder.http;

import java.nio.charset.Charset;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpObject;

public class HttpMsgRequestDecoder extends MessageToMessageDecoder<HttpObject>{
	private String charset;

	public HttpMsgRequestDecoder(String charset) {
		super();
		this.charset = charset;
	}

	@Override
	protected void decode(ChannelHandlerContext ctx, HttpObject in,
			List<Object> out) throws Exception {
		FullHttpRequest request = (FullHttpRequest) in;

		ByteBuf buf = request.content();
		String jsonStr = buf.toString(Charset.forName(charset));
		out.add(jsonStr);
	}
}

至此,以上tcphttp处理方式已完成。

快速构建项目

Spring组件化构建

喜欢这篇文章么,喜欢就加入我们一起讨论Spring技术吧!
https://img-blog.csdnimg.cn/20190430181302249.png" alt="品茗IT交流群" />


http://www.niftyadmin.cn/n/1051064.html

相关文章

Struts中使用displaytag简单小教程

Display Tag Lib是一个标签库&#xff0c;用来处理jsp网页上的Table&#xff0c;功能非常强&#xff0c;可以对的Table进行分页、数据导出、分组、对列排序等等&#xff0c;能够大大减少代码量。 这个是Display Tag的官方网站http://displaytag.sourceforge.net。 …

TCP/IP三次握手四次挥手分析

流程图 全部11种状态 客户端独有的&#xff1a;&#xff08;1&#xff09;SYN_SENT &#xff08;2&#xff09;FIN_WAIT1 &#xff08;3&#xff09;FIN_WAIT2 &#xff08;4&#xff09;CLOSING &#xff08;5&#xff09;TIME_WAIT 服务器独有的&#xff1a;&#xff08;1&am…

操作系统之进程的描述与控制

进程的描述与控制 &#xff08;程序的抽象&#xff0c;抽象成一个实体&#xff0c;给出描述实体的信息&#xff0c;实体间的交互协作、实体间的调用、OS对实体的调用&#xff0c;OS存储实体的信息&#xff09; &#xff08;OS建立一个世界&#xff0c;划分不同的角色&#xf…

Java数据结构和算法概览

Java数据结构和算法概览 数据结构 线性数据结构&#xff1a;常见的有一维数组&#xff0c;线性表&#xff0c;栈&#xff0c;队列&#xff0c;双队列&#xff0c;串。 非线性数据结构&#xff1a;常见的有&#xff1a;多维数组&#xff0c;集合&#xff0c;树&#xff0c;图…

操作系统之处理机调度

操作系统之处理机调度 将军调度士兵打胜仗 处理机调度的层次和调度算法的目标 处理机调度是对处理机资源的分配。 处理机调度算法是指根据处理机分配策略所规定的处理机分配算法。 处理机调度的层次 高级调度&#xff08;High Level Scheduling&#xff09; 又称长程调度或…

用u盘做系统0基础教程

在笔记本电脑早已普及到会议室的这个年代&#xff0c;商务人士拿笔记本来演示PPT以及做电子版的会议记录&#xff1b;在笔记本电脑已经普及到教室的这个年代&#xff0c;学生们甚至在用笔记本翻阅资料进行开卷考试。 随着笔记本电脑正在成为人们生活中不可或缺的一部分&#x…

博客开通啦~

大学就听闻老师介绍博客园&#xff0c;终于也能开通自己的了&#xff0c; and its so easy. . 本博客主要记录 我的 c/c 学习之路&#xff0c;做过3年MFC&#xff0c;看到公司项目被c#取代&#xff0c;已决心转向无界面开发。目前努力学习linux 下开发&#xff0c;深知apue只是…

java集合及concurrent并发包

java集合及concurrent并发包 集合包 集合包最常用的有Collection和Map两个接口的实现类&#xff0c;Colleciton用于存放多个单对象&#xff0c;Map用于存放Key-Value形式的键值对。 Collection中最常用的又分为三种类型的接口&#xff1a;List、Queue和Set&#xff0c;List和…