博客概述
本博客讲了之前的demo的一些细节,以及在开发中框架该如何使用的问题。
关于write和flush
如果客户端连续写多条数据,然后一次flush出去。服务端会一次性收到所有的信息。这个就出现了tcp的粘包拆包的问题,我们在下一篇博客中进行这个问题的讨论与解决。因为这个现象,实际开发中使用writeandflush的api进行回写数据。
实现长连接与短连接
这两个连接的区别就在于通信完毕之后,服务器是否选择把连接断开。在服务器端的handler是很简单的一句话。
ChannelFuture future = ctx.writeAndFlush(Unpooled.copiedBuffer("hi,client".getBytes()));
future.addListener(ChannelFutureListener.CLOSE);
然后服务器端执行了这个方法之后,客户端会执行下面的代码,执行完了之后就关闭了。
channelFuture1.channel().closeFuture().sync();
关于谁来关闭连接的问题
肯定是服务器端,因为如果客户端关闭,得获得channel对象,只能出现在2个地方。
- clienthandler里面,目前仅仅是接收数据功能,如果要关闭,必须得拿到future对象,得写点东西给服务器端。然后调用监听。很明显多写了数据了,强制规定了一些业务细节,这个处理很不好。
- 客户端连接之后,写完数据直接关闭。这种操作的问题很明显,客户端的反馈收不到。不知道server的处理结果,并不知道处理成功还是失败。
netty绑定多个端口
serverbootstrap对象可以绑定多给端口,接收数据能力变强了,但是处理能力没有变化。都是走的一个handler。
netty实际工作中的部署选择
一般是有两种,跟tomcat同生命周期或者独立一个jar文件。
- tomcat里面放着,随着tomcat启动。这样的话netty的生命周期和tomcat是紧密耦合的。同生共死系列片。同时netty服务和你的web服务抢占内存,因为是同一个jvm。因此不建议使用这种方式。
- 单独写个jar,启动。这种方式的问题是,公用数据库,写两套dao,维护两套代码。web应用与netty程序独立运行。两套代码可以用maven的多模块来解决,引入dao模块即可。
如何打jar包
新建一个quickstart项目,配置pom文件。主要进行的操作就是复制jar包到指定目录,生成mainfest文件,然后生成jar包。附上具体的pom文件。需要注意的是:
- 配置pom文件的时候,插件不要用插件管理器标签包裹,否则不生效。
- 调用的时候,使用idea中的lifeCycle中的指令,直接运行install就可以达成jar包了。不要用plugins下面的指令。
pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>deploy</groupId>
<artifactId>com.zero</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--打包不包括测试-->
<maven.test.skip>true</maven.test.skip>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling -->
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling</artifactId>
<version>1.3.0.CR9</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-serial</artifactId>
<version>1.3.0.CR9</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--打jar包插件-->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<classDirectory>target/classes/</classDirectory>
<archive>
<manifest>
<!--主程序入口-->
<mainClass>deploy.NettyServer</mainClass>
<!--打包MANIFEST文件不记录时间戳版本-->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<!--依赖包存放与jar包的索引位置-->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<!--复制jar包的插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeType>jar</includeType>
<useUniqueVersions>false</useUniqueVersions>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>