1. 程式人生 > >Netty實戰手冊(二)

Netty實戰手冊(二)

  本篇主要講述通過JSTS整合的Netty構建一個的遊戲伺服器結構,然後你可以在其中擴充套件你需要的內容。下面請跟隨我將講述JSTS的用法,其中你可以瞭解Netty和Spring的一些用法,並可以在專案中進行實踐。

一、基礎配置

  首先,我們構建一個Maven專案,專案中引入一些基礎內容:JSTS、junit、log4j,因為某些怨音,JSTS引入JTLS,JTLS使用了jdk1.8的特性。於是乎初始的pom.xml的關鍵節點如下:

<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.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
<!-- jees 依賴項 分為2個部分 -->
<dependency>
	<groupId>com.jees</groupId>
	<artifactId>jees-jsts</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- junit -->
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>
<!-- log4j -->
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-core</artifactId>
	<version>2.9.1</version>
</dependency>
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-api</artifactId>
	<version>2.9.1</version>
</dependency>
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-1.2-api</artifactId>
	<version>2.9.1</version>
</dependency>

二、伺服器結構設計

  作為遊戲伺服器,我們可以先設計幾個內容的服務類:GameService、HandlerService、CommandService

  GameServce作為程式的啟動入口,負責啟動應用。啟動方式,上篇也介紹過,這裡還是先貼出來:

@SuppressWarnings( "resource" )
public static void main( String[] args ) {
	new ClassPathXmlApplicationContext( ICommonConfig.CFG_DEFAULT ) ;
	((ISupportSocket) CommonContextHolder.getBean( ISupportSocket.SOCKET_SUPER ) ).onload();
}

   HandlerService用於接收和處理Netty的客戶端響應事件。ISupportHandler提供了針對Netty或者其他Socket框架的事件相應模型,初始的程式碼如下:

@Service
public class HandlerService implements ISupportHandler< ChannelHandlerContext , Object > , ICommonConfig {

	@Override
	public void receive( ChannelHandlerContext _ctx , Object _obj ) {
		// TODO 收到訊息觸發
	}

	@Override
	public void enter( ChannelHandlerContext _ctx ) {
		// TODO 建立連線觸發
	}

	@Override
	public void leave( ChannelHandlerContext _ctx ) {
		// TODO 連線斷開觸發
	}

	@Override
	public void stand( ChannelHandlerContext _ctx ) {
		// TODO 預設的心跳檢測機制,超過1分鐘未發生任何請求的客戶端會觸發
	}

	@Override
	public void error( ChannelHandlerContext _ctx , Throwable _thr ) {
		// TODO 當事件變化,或者客戶端請求觸發異常時,觸發
	}
}

  CommandService處理客戶端的請求命令。

@Service
public class CommandService implements ISupportCommand< ChannelHandlerContext , ByteBuf > {
	@Override
	public void docommand( ChannelHandlerContext _ctx , ByteBuf _buf ) {
	}
}

  其實CommandService僅僅是提供給給位參考的,主要用拆解來自客戶端的各種訊息,以及事件的處理。這部分可以根據各自的理解,自行實現。

  客戶端的模擬我就不寫了,網上的例子太多。最後貼出Spring的核心內容和檔案:

<!-- 基礎配置部分 -->
<util:properties id="defineConfig" location="define.properties" />
<context:property-placeholder properties-ref="defineConfig" ignore-unresolvable="true" />
	
<!-- 前置功能 -->	
<context:annotation-config />
	
<context:component-scan base-package="com.jees.core.socket.*" />
<!-- 使用Netty作為Socket伺服器  -->
<context:component-scan base-package="com.jees.jsts.netty.*" />
<context:component-scan base-package="com.jees.demo.service" />

  define.properties 檔案中僅定義了埠,後面可以把其他的內容定義放在這裡。

socket.port1	= 1000

三、 結尾

    到這裡,一個最基本的Netty伺服器模型就建立好了,貼出來的沒有多餘或者缺少的配置,是可以獨立執行的列子。當然你可以改進和優化它。因為總體的思路比較簡單,雖然我沒有給出完整的程式碼,但依靠上面的內容,就可以獨立運行了。

    Netty沒有想像中的複雜,對於伺服器開發來說算得上是簡單好用了。

下篇,我將介紹如何配合ISupportHandler來實現與客戶端的通訊的,並且介紹一些框架中的使用方式。