HTTP与网络
Spring风格的Restful的Controller:
RestfulController 通用Restful API Controller
–org.titan.framework.facade.controller.RestfulController
![](https://img.haomeiwen.com/i9159277/328ea63eb01fdd1e.png)
RestfulController进行HTTP API Path注释同Spring风格
![](https://img.haomeiwen.com/i9159277/2989a87e0a29ddb8.png)
Tips:RestfulController各方法中的ServiceName可以用于区分API版本号,并且进行API版本兼容处理,例如https://{base_url}/login/v1 [POST]。
完整的RestfulController实例
![](https://img.haomeiwen.com/i9159277/4d814754f71faaa4.png)
QueryController 通用查询专用API Controller
–org.titan.framework.facade.controller.QueryController
![](https://img.haomeiwen.com/i9159277/e5a18a41d5f85011.png)
完整的QueryController实例
![](https://img.haomeiwen.com/i9159277/c65c0109693eb652.png)
org.titan.framework.facade.controller的自定义方式
Titan Framework对Controller的自定义方式类似Spring风格,如果曾经使用过SpringMVC的用户将会很快上手。
![](https://img.haomeiwen.com/i9159277/3a83ca94c2c8acd0.png)
CommandHandler&Command 通用指令模型
–org.titan.framework.facade.application.CommandHandler
–org.titan.framework.facade.application.Command
Titan Framework中采用如下逻辑进行Command捕捉与传递。
–第一步:Controller中return一个Command至Titan的Controller基类;
–第二步:Titan的Controller基类中将会完成Command类型的识别与自动匹配;
–第三步:Titan将Command发送到对应的应用层CommandHandler(注意:必须有@CmdHandler注释)
![](https://img.haomeiwen.com/i9159277/7b3cb49b64a69e91.png)
一个简单的CommandHandler案例如下:
![](https://img.haomeiwen.com/i9159277/20154dd230ac9ced.png)
Command&Command类型
Command是Titan框架中的指令单元,用来关联Controller与对应的具体处理逻辑,实现Controller与逻辑处理的解耦。Titan Framework为用户预置了下面这些Command类型,大家可以直接使用。
![](https://img.haomeiwen.com/i9159277/11690596570b08c2.png)
当然,大家也可以通过实现Command接口来自定义自己的Command。对于自定义Command,Titan Framework同样提供自动匹配支持。
Command中实现了以下默认方法:
![](https://img.haomeiwen.com/i9159277/cfc9fdb4783a7d9f.png)
Result与Result的使用
Result是Titan Framework中通用返回值的包装类,通过Result可以直接获取结果、状态、错误信息等。
Result有以下构造方式:
![](https://img.haomeiwen.com/i9159277/8011b5252f714ea0.png)
Titan内置的Result信号类型,可以快速构建对应的result:
![](https://img.haomeiwen.com/i9159277/6edf1cc3286931a6.png)
Result的方法:
![](https://img.haomeiwen.com/i9159277/fec03710f8522258.png)
微服务远程调度
Publish&EventHandler
–org.titan.framework.facade.kit.Publish
–org.titan.framework.facade.event.EventHandler
Publish是Titan Framework的微服务远程调用入口,用于有针对性地向远程服务发起请求事件。
Publish有以下方法:
![](https://img.haomeiwen.com/i9159277/15501269ec84d176.png)
Publish的使用示例:
Send模式(Remote)
![](https://img.haomeiwen.com/i9159277/6a88a24446cd8a19.png)
Send模式(Local)
![](https://img.haomeiwen.com/i9159277/94f2a229032de09d.png)
![](https://img.haomeiwen.com/i9159277/01dd825704343abd.png)
与Publish相对应的是其他微服务的EventHandler,Publish发送Event,EventHandler接收响应Event。
EventHandler和CommandHandler类似,通过Event的类型来进行远程匹配。
EventHandler使用示例:
![](https://img.haomeiwen.com/i9159277/6c14de253ea5b133.png)
EventHandler中关键方法为Feedback.content(),用来通过非阻断的方式将处理结果返回至调用者。
![](https://img.haomeiwen.com/i9159277/b43793f4909c797d.png)
Tips:自定义AppEvent和Result中都可以携带数据进行返回,Titan官方不建议返回数据过大,如果需要返回大量数据或者二进制流数据,可以通过Event驱动加MessageQueue的方式进行处理。
数据库&消息队列支持
Titan Framework对MySql、MongoDB、Redis提供直接支持。
Titan Framework的MySql支持
Titan与Mysql的对接包括以下三部分:
–conf包下的mysql.properties
–spring包下的application-dynamic-ds-context.xml、application-context.xml
–基于Titan SpringMysqlRepository、SpringMysqlFindRepository的功能模块
其中SpringMysqlRepository是数据库写模型,SpringMysqlFindRepository是数据库读模型。
SpringMySqlRepository用法如下:
![](https://img.haomeiwen.com/i9159277/69e6e72245c0b4cc.png)
![](https://img.haomeiwen.com/i9159277/174fa5e6545a73d4.png)
SpringMysqlRepository中包含以下方法:
![](https://img.haomeiwen.com/i9159277/3fe47edcf948bf34.png)
SpringMysqlFindRepository示例如下:
![](https://img.haomeiwen.com/i9159277/919121e428f6e324.png)
mysql.properties示例如下:
![](https://img.haomeiwen.com/i9159277/6886cd8957ca9095.png)
application-context.xml示例如下:
![](https://img.haomeiwen.com/i9159277/b08e8dcff138aea5.png)
application-dynamic-ds-context.xml示例如下:
![](https://img.haomeiwen.com/i9159277/6303f45a61027267.png)
![](https://img.haomeiwen.com/i9159277/0daee13d446b5abb.png)
Titan Framework的MongoDB支持
Titan与MongoDB的对接包括以下两部分:
–spring包下的application-mongoDB-context.xml、application-context.xml
–conf包下的mongoDB.properties
基于Titan MongodbRepository的功能模块
其中需要实现StorageDataProcessor.Mongo。
MongodbRepository用法如下:
![](https://img.haomeiwen.com/i9159277/9f767cdfe5fd101c.png)
application-mongoDB-context.xml示例如下:
![](https://img.haomeiwen.com/i9159277/4a34ee63007d72d7.png)
application-context.xml示例如下:
![](https://img.haomeiwen.com/i9159277/104541cff2d176d7.png)
mongoDB.properties示例如下:
![](https://img.haomeiwen.com/i9159277/21aca5d047d3f2e9.png)
Titan Framework的Redis支持
Titan与Mysql的对接包括以下三部分:
–spring包下的application-redis-context.xml、application-context.xml
–conf包下的redis.properties
–基于Titan ShardedJedisPool的功能模块
ShardedJedisPool用法如下:
![](https://img.haomeiwen.com/i9159277/2e3805f222e32cc2.png)
application-redis-context.xml用法如下:
![](https://img.haomeiwen.com/i9159277/5fe93ce6bc854fa9.png)
![](https://img.haomeiwen.com/i9159277/cbdf660bfbf52a75.png)
application-context.xml用法如下:
![](https://img.haomeiwen.com/i9159277/421436d3ec0f95a8.png)
redis.properties用法如下:
![](https://img.haomeiwen.com/i9159277/ab073701a39fd9f2.png)
![](https://img.haomeiwen.com/i9159277/22b11c20fcf0a023.png)
网友评论