flyingboot
項目介紹
基于Netty的輕量級web快速開發(fā)框架萎河。
使用netty+completeableFuture 異步方式提高吞吐量荔泳。
可用于網(wǎng)關(guān)開發(fā)等系統(tǒng),在使用習(xí)慣上模仿了spring的相關(guān)注解虐杯,學(xué)習(xí)成本低玛歌。
不扯啥重復(fù)造輪子,這個框架是在做api網(wǎng)關(guān)的過程中抽取出來的擎椰,本著實用方便的原則支子,封裝成了類似于springboot的樣子。
接入使用
1. 添加maven依賴
<dependency>
<groupId>com.github.gaojh</groupId>
<artifactId>flyingboot</artifactId>
<version>2.1.5</version>
</dependency>
2. 添加項目配置文件
flyingboot默認讀取application.properties配置文件中的配置
所有配置項可以使用Environment來進行獲取
application.properties默認參數(shù)如下达舒,如果需要修改值朋,可以在文件中重新定制:
#httpserver端口,默認8080
server.port=8080
#flyingboot通用連接池的core size 默認600
flying.thread.core.size=600
#flyingboot通用連接池的max size 默認2000
flying.thread.max.size=2000
#flyingboot通用連接池的keeplive time巩搏,默認0
flying.thread.keepalive.time=0
3. 新建啟動類
類似于springboot的Application類
@ComponentScan({"com.github.gaojh.example"})
public class FlyingbootDemo {
public static void main(String[] args) {
Flying.run(FlyingbootDemo.class);
}
}
如果不加@ComponentScan注解昨登,則包掃描路徑直接設(shè)置為啟動的包路徑塔猾。
4. 添加Controller
@Controller
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping("/hello")
public String hello(@RequestParam String name){
return demoService.getName(name);
}
@RequestMapping("/demo")
public Object demo(@RequestBody DemoBean demoBean){
return demoBean;
}
@RequestMapping("/h2/*")
public String h2(){
return "h2";
}
}
到此時,一個簡單的Flyingboot項目已經(jīng)可以運行了糯俗,跟springboot很相似。
5. 如何使用過濾器
@Interceptor(pathPatterns = {"/**"}, ignorePathPatterns = {"/hello"}, order = 5)
public class DemoInterceptor implements HandlerInterceptor {
private static Logger logger = LoggerFactory.getLogger(DemoInterceptor.class);
@Override
public boolean preHandle(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
logger.info("demo");
return true;
}
@Override
public void postHandle(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
logger.info("demo postHandle");
}
@Override
public void afterCompletion(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
logger.info("demo afterCompletion");
}
}
過濾器需要實現(xiàn)HandlerInterceptor接口睦擂,里面有三個方法
1得湘、preHandle前置處理器,在調(diào)用業(yè)務(wù)方法之前調(diào)用顿仇,如果返回true淘正,繼續(xù)調(diào)用下個過濾器,如果返回false臼闻,則不調(diào)用下個過濾器鸿吆。
2、postHandle后置處理器述呐,在調(diào)用業(yè)務(wù)方法之后調(diào)用惩淳。
3、afterCompletion此方法廢棄乓搬,后期會刪除思犁。
@Interceptor注解必須要加上代虾,否則會掃描不到該過濾器。
1激蹲、pathPatterns是用于匹配過濾的url棉磨。
2、ignorePathPatterns是用戶匹配忽略過濾的url学辱。
3乘瓤、order指定過濾器的順序
6. 如何使用動態(tài)Controller
1、首先定義Handler
@Component
public class DemoDynamicHandler implements RouterHandler {
@Override
public Object handle(HttpRequest httpRequest) {
return "ok";
}
}
實現(xiàn)RouterHandler接口项郊,實現(xiàn)handle方法馅扣,這個方法就是類似Controller中的方法是一樣的斟赚。
2着降、其次添加動態(tài)路由到Flyingboot
@Configuration
public class RouterConfig {
@Autowired
private DemoDynamicHandler demoDynamicHandler;
@Autowired
private DemoDynamicHandler2 demoDynamicHandler2;
@Autowired
private HttpClient httpClient;
@Bean
public RouterFunction router() {
return Routers.route().GET("/api/baidu", httpRequest -> httpClient.request("http://www.taobao.com", httpRequest)).GET("/hello", demoDynamicHandler).GET("/hello2", demoDynamicHandler2).build();
}
}
這樣就可以添加動態(tài)的router
7. 啟用websocket
在application.properties中添加配置
flying.websocket.enable=true
添加websocketHandler
@Component
public class MyWebSocketHandler implements WebSocketHandler {
@Override
public void onHandshake(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) throws Exception {
System.out.println(fullHttpRequest.uri());
}
@Override
public void onMessage(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("msg: "+msg);
sendMessage(ctx,"回復(fù):"+msg);
}
@Override
public void onClose(ChannelHandlerContext ctx) throws Exception {
}
}
這樣websocket就可以使用了
其他
相關(guān)example請參考flyingboot-test項目
本項目還在不斷的迭代中,歡迎關(guān)注并提出意見拗军!
項目地址:
https://gitee.com/gaojh/flyingboot
https://github.com/gaojh/flyingboot