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

Netty實戰手冊(四)

這次我在介紹Netty的使用時,會帶入JDBS的資料處理,沒有了解JDBS的可以在這裡看JDBS的實際使用方式。

另外我對JSTS部分介面進行了重新命名,具體內容請對比 Netty實戰手冊(三)中的部分,當然可以以示例程式碼為準。

使用JDBS時,需要先下載JDBS的專案到本地,然後新增到本地maven庫裡。關於Maven和Git的用法,這裡不做描述。

首先在專案中的Pom.xml中加入以下內容匯入JDBS:

<dependency>
	<groupId>com.jees</groupId>
	<artifactId>jees-jdbs</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
然後,參考JDBS中的資料庫配置檔案,加入我們自己的配置檔案,參考如下:

jees-core-dispatcher.xml: 加入資料庫配置

<import resource="jees-core-database.xml"/>
jees-core-database.xml:資料庫相關配置

<!-- 以下為專案配置參考 -->
<context:component-scan base-package="com.jees.demo.*" />
<import resource="jees-database-test.xml"/>

<!-- 資料庫池,程式通過關鍵字sessionFactoryMap獲取對應的資料庫session。ID為固定值,不可修改。 -->
<!-- 
Example:
dao.executeByHQL( _dbKey, _hql );
-->
<util:map id="sessionFactoryMap" >
	<!-- _dbmap_key為查詢的資料標識關鍵字 -->
	<entry key="default" value-ref="defaultSF"/>
</util:map>
其實這些配置內容和SpringMVC+Hibernate的框架基本一致,完全可以用在Web專案 中,為了方便理解,有些命名我也是參考Web專案來命名的。

資料庫的用法,可以定一個DaoService,並實現JDBS中的AbsSupportDao進行封裝:

DaoService.java

@Service
public class DaoService extends AbsSupportDao {
	final static String		DB_DEFAULT	= "default";
	
	// Common 資料庫通用方法 ==========================================
	// 呼叫AbsSupportDao的公共方法,也可以通過其他方式自己實現,例如
	// Session sess = _get_session( DB_DEFAULT );
	
	public void insert( Object _entity ) {
		insert( DB_DEFAULT , _entity );
	}
	
	public < T > T selectById( Class< T > _cls , Serializable _id ) {
		return selectById( DB_DEFAULT , _cls , _id );
	}
}
然後可以提供類似Controller的類來對客戶端命令做行為分解,這裡我叫DemoModel:

DemoModel.java:

@Component
public class DemoModel {
	
	@Autowired
	DaoService			daoService;
	/**
	 * 假設這裡是處理登陸等命令,加入@Transactional保證事務生效
	 * @param _request
	 * @param _response
	 */
	@Transactional
	public void requestLogin( ByteBuf _request, ByteBuf _response ) {
		// 如果需要字串,建議使用ProtoBuff,否則需要知道擷取字串的長度,參考如下:
		//int len = _request.readInt();
		//byte[] dst = new byte[len];
		//_request.readBytes( dst );
		//String account = new String( dst );
		//下面我僅作示意程式碼來當作使用者登陸
		
		int id = _request.readInt();
		
		TabA a = daoService.selectById( TabA.class , id );
		
		_response.writeInt( a == null ? -1 : a.getId() );
	}
	
	/**
	 * 假設這裡處理註冊
	 * @param _request
	 * @param _response
	 */
	@Transactional
	public void requestRegist( ByteBuf _request, ByteBuf _response ){
		TabA a = new TabA();
		daoService.insert( a );
		_response.writeInt( a.getId() );
	}
}
參考實戰手冊三,我這裡修改了CommandService的請求處理內容:
@Service
public class CommandService implements ISupportService< ChannelHandlerContext , ByteBuf > {
	private static Logger logger		= LogManager.getLogger( CommandService.class );
	
	@Autowired
	DemoModel	demoModel;
	
	@Override
	public void request( ChannelHandlerContext _ctx , ByteBuf _request ) {
		final int		UserLogin = 1;
		final int		UserRegist = 2;
		int cmd = _request.readInt();
		logger.info( "CommandService do command: CMD=[" + cmd + "]" );
		ByteBuf response = _ctx.alloc().buffer();
		
		//根據cmd來識別使用哪個模組來處理,
		switch( cmd ){
			case UserLogin:
				demoModel.requestLogin( _request, response );
				break;
			case UserRegist:
				demoModel.requestRegist( _request, response );
				break;
		}
		
		response( _ctx, response );
	}
	
	@Override
	public void response( ChannelHandlerContext _ctx , ByteBuf _msg ) {
		_ctx.writeAndFlush( _msg );
	}
}
基本上從網路請求到資料處理,現在已經形成了一個完整環境了,熟悉客戶端開發的人,可以使用這套環境來做伺服器。

由於最近事情比較多,本篇寫的比較倉促,無法很好的理解的,可以加群諮詢我。

Netty實戰篇暫時到這裡了,後面有時間會在示例程式碼中更新粘包的處理和Protobuff的使用。

接下來我會找時間把JEES或者JEESJS的使用完善和發文章做介紹。希望各位多多支援。

完整的程式碼下載地址如下:

https://github.com/aiyoyoyo/jeesupport/tree/master/demo/JSTSDemo

歡迎各位關注:JEES系列
也可以加QQ群討論:8802330