Merge pull request #121 from nutzam/add_tio_mvc

add: 添加tio-mvc的相关支持
This commit is contained in:
蛋蛋的忧伤 2018-02-06 14:48:13 +08:00 committed by GitHub
commit 085f6a0a39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 413 additions and 7 deletions

View File

@ -4,10 +4,12 @@
## dev 迭代中
* 变更:
* add: starter-tio-mvc 基于tio的高性能MVC框架
* add: starter-freemarker by [蛋蛋](https://github.com/TopCoderMyDream)
* update: 优化starter-tio的逻辑 by [zjSniper](https://gitee.com/zjSniper)
* update: j2cache配置文件融入nutzboot的主配置信息 by [蛋蛋](https://github.com/TopCoderMyDream)
* update: starter-beetl将GroupTemplate放入ioc容器,方便用户自定义和获取
* update: 更新tio版本到2.0.8.v20180205-RELEASE, 感谢tio社区的大力支持
* fix: 一主多从的逻辑有问题,修正之
* fix: tomcat与shiro一起使用时报错, report by [蛋蛋](https://github.com/TopCoderMyDream)

View File

@ -168,10 +168,10 @@ public class MainLauncher {
- [ ] ssdb
- Mvc
- [x] starter-nutz-mvc Nutz自带的Mvc框架
- [ ] t-io mvc
- [x] starter-[tio-mvc](https://gitee.com/tywo45/t-io) 基于tio的Mvc框架
- [ ] [jersey](https://jersey.github.io/)
- 非servlet容器
- [x] starter-tio 国产高性能网络开发包 by [蛋蛋](https://github.com/TopCoderMyDream)
- [x] starter-[tio](https://gitee.com/tywo45/t-io) 国产高性能网络开发包 by [蛋蛋](https://github.com/TopCoderMyDream)
- 安全鉴权
- [x] [Shiro](http://shiro.apache.org)
- 分布式Session

View File

@ -0,0 +1,75 @@
<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">
<parent>
<artifactId>nutzboot-demo-simple</artifactId>
<groupId>org.nutz</groupId>
<version>2.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nutzboot-demo-simple-tio-mvc</artifactId>
<packaging>jar</packaging>
<name>nutzboot-demo-simple-tio-mvc</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-tio-mvc</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-nutz-dao</artifactId>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/nutz/org.nutz.boot.starter.NbStarter</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.nutz.demo.simple.MainLauncher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,33 @@
package io.nutz.demo.simple;
import org.nutz.boot.NbApp;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import io.nutz.demo.simple.bean.User;
/**
* @Author wendal
*/
@IocBean(create="init")
public class MainLauncher {
@Inject
protected Dao dao;
public void init() {
dao.create(User.class, true);
dao.insert(new User("apple", 40, "北京"));
dao.insert(new User("ball", 30, "未知"));
dao.insert(new User("cat", 50, "温哥华"));
dao.insert(new User("fox", 51, "纽约"));
dao.insert(new User("bra", 25, "济南"));
dao.insert(new User("lina", 50, "深圳"));
}
public static void main(String[] args) throws Exception {
new NbApp().setPrintProcDoc(true).run();
}
}

View File

@ -0,0 +1,51 @@
package io.nutz.demo.simple.bean;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;
@Table("t_user")
public class User {
@Id
private long id;
@Name
private String name;
private int age;
private String location;
public User() {
}
public User(String name, int age, String location) {
super();
this.name = name;
this.age = age;
this.location = location;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}

View File

@ -0,0 +1,30 @@
package io.nutz.demo.simple.tio.mvc;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.server.annotation.RequestPath;
import org.tio.http.server.util.Resps;
import io.nutz.demo.simple.bean.User;
@IocBean
@RequestPath(value = "/user")
public class UserController {
@Inject
protected Dao dao;
@RequestPath(value = "/count")
public HttpResponse count(HttpRequest request) {
return Resps.json(request, dao.count(User.class));
}
@RequestPath(value = "/query")
public HttpResponse query(HttpRequest request) {
return Resps.json(request, dao.query(User.class, Cnd.orderBy().asc("age"), null));
}
}

View File

@ -0,0 +1,6 @@
#server.port=9900
#tio_mvc.port=9900
#tio_mvc.host=0.0.0.0
jdbc.url=jdbc:h2:mem:~

View File

@ -0,0 +1,8 @@
log4j.rootLogger=debug,Console
log4j.logger.org.eclipse.jetty=info
log4j.logger.org.tio=info
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%-5p] %d{HH:mm:ss.SSS} %l - %m%n

View File

@ -43,7 +43,8 @@
<module>nutzboot-demo-simple-j2cache</module>
<module>nutzboot-demo-simple-dao-with-slave</module>
<module>nutzboot-demo-simple-freemarker</module>
</modules>
<module>nutzboot-demo-simple-tio-mvc</module>
</modules>
<dependencies>
<dependency>

View File

@ -0,0 +1,20 @@
<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>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter</artifactId>
<version>2.2-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-starter-tio-mvc</artifactId>
<dependencies>
<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-core</artifactId>
</dependency>
<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-http-server</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,137 @@
package org.nutz.boot.starter.tio.mvc;
import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.tio.http.common.HttpConfig;
import org.tio.http.common.session.id.ISessionIdGenerator;
import org.tio.http.server.HttpServerStarter;
import org.tio.http.server.handler.DefaultHttpRequestHandler;
import org.tio.http.server.intf.HttpServerInterceptor;
import org.tio.http.server.mvc.Routes;
import org.tio.http.server.mvc.intf.ControllerFactory;
import org.tio.utils.cache.ICache;
@IocBean
public class TioMvcHttpServerBeans implements ControllerFactory {
private static final Log log = Logs.get();
@Inject
private AppContext appContext;
protected static final String PRE = "tio_mvc.";
@Inject
protected PropertiesProxy conf;
@PropDoc(value = "tio监听端口", defaultValue = "9900")
public static final String PROP_PORT = PRE + "port";
@PropDoc(value = "tio监听ip/主机名", defaultValue = "0.0.0.0")
public static final String PROP_IP = PRE + "host";
@PropDoc(value = "tio mvc会话超时时间", defaultValue="1800")
public static final String PROP_SESSION_TIMEOUT = PRE + "sessionTimeout";
@PropDoc(value = "tio mvc上下文路径")
public static final String PROP_CONTEXT_PATH = PRE + "contextPath";
@PropDoc(value = "服务器信息")
public static final String PROP_SERVER_INFO = PRE + "serverInfo";
@PropDoc(value = "默认后缀")
public static final String PROP_SUFFIX = PRE + "suffix";
@PropDoc(value = "字符集", defaultValue="UTF-8")
public static final String PROP_CHARSET = PRE + "charset";
@PropDoc(value = "默认Welcome File", defaultValue="index.html")
public static final String PROP_WELCOME_FILE = PRE + "welcomeFile";
@PropDoc(value = "允许访问的域名,逗号分隔")
public static final String PROP_ALLOW_DOMAINS = PRE + "allowDomains";
@PropDoc(value = "会话缓存的名称")
public static final String PROP_SESSION_CACHE_NAME = PRE + "sessionCacheName";
@PropDoc(value = "会话cookie的名字")
public static final String PROP_SESSION_COOKIE_NAME = PRE + "sessionCookieName";
@PropDoc(value = "maxLiveTimeOfStaticRes设置")
public static final String PROP_MAX_LIVE_TIME_OF_STATIC_RES = PRE + "maxLiveTimeOfStaticRes";
@PropDoc(value = "404页面", defaultValue="/404.html")
public static final String PROP_PAGE_404 = PRE + "page404";
@PropDoc(value = "500页面", defaultValue="/500.html")
public static final String PROP_PAGE_500 = PRE + "page500";
@PropDoc(value = "是否使用Session机制", defaultValue="true")
public static final String PROP_USE_SESSION = PRE + "useSession";
@PropDoc(value = "拦截器", defaultValue="apiInterceptor")
public static final String PROP_API_INTERCEPTOR = PRE + "apiInterceptor";
@PropDoc(value = "会话id生成器", defaultValue="sessionIdGenerator")
public static final String PROP_SESSION_ID_GENERATOR = PRE + "sessionIdGenerator";
@PropDoc(value = "会话id缓存提供者", defaultValue="sessionStore")
public static final String PROP_SESSION_STORE = PRE + "sessionStore";
@IocBean
public HttpConfig getHttpConfig() {
String ip = appContext.getServerHost(PROP_IP);
int port = appContext.getServerPort(PROP_PORT, 9900);
HttpConfig httpConfig = new HttpConfig(port,
conf.getLong(PROP_SESSION_TIMEOUT, 1800),
conf.get(PROP_CONTEXT_PATH),
conf.get(PROP_SUFFIX));
httpConfig.setBindIp(ip);
if (conf.has(PROP_ALLOW_DOMAINS)) {
httpConfig.setAllowDomains(Strings.splitIgnoreBlank(conf.get(PROP_ALLOW_DOMAINS)));
}
if (conf.has(PROP_SERVER_INFO)) {
httpConfig.setServerInfo(conf.get(PROP_SERVER_INFO));
}
httpConfig.setCharset(conf.get(PROP_CHARSET, "UTF-8"));
httpConfig.setWelcomeFile(conf.get(PROP_WELCOME_FILE, "index.html"));
if (conf.has(PROP_SESSION_CACHE_NAME)) {
httpConfig.setSessionCacheName(PROP_SESSION_CACHE_NAME);
}
if (conf.has(PROP_SESSION_COOKIE_NAME)) {
httpConfig.setSessionCookieName(conf.get(PROP_SESSION_COOKIE_NAME));
}
if (conf.has(PROP_MAX_LIVE_TIME_OF_STATIC_RES)) {
httpConfig.setMaxLiveTimeOfStaticRes(conf.getInt(PROP_MAX_LIVE_TIME_OF_STATIC_RES));
}
httpConfig.setPage404(conf.get(PROP_PAGE_404, "/404.html"));
httpConfig.setPage500(conf.get(PROP_PAGE_500, "/500.html"));
httpConfig.setUseSession(conf.getBoolean(PROP_USE_SESSION, true));
if (httpConfig.isUseSession()) {
String sessionIdGeneratorName = conf.get(PROP_SESSION_ID_GENERATOR, "sessionIdGenerator");
if (appContext.getIoc().has(sessionIdGeneratorName)) {
httpConfig.setSessionIdGenerator(appContext.getIoc().get(ISessionIdGenerator.class, sessionIdGeneratorName));
}
else {
log.debugf("sessionIdGenerator name=%s not found in ioc , skiped", sessionIdGeneratorName);
}
String sessionStoreName = conf.get(PROP_SESSION_STORE, "sessionStore");
if (appContext.getIoc().has(sessionStoreName)) {
httpConfig.setSessionStore(appContext.getIoc().get(ICache.class, sessionStoreName));
}
else {
log.debugf("sessionStore name=%s not found in ioc , skiped", sessionStoreName);
}
}
return httpConfig;
}
@IocBean
public HttpServerStarter getHttpServerStarter(HttpConfig httpConfig) {
String[] scanPackages = new String[]{appContext.getPackage()};//tio mvc需要扫描的根目录包
Routes routes = new Routes(scanPackages, this);
DefaultHttpRequestHandler requestHandler = new DefaultHttpRequestHandler(httpConfig, routes);
String apiInterceptorName = conf.get(PROP_API_INTERCEPTOR, "apiInterceptor");
if (appContext.getIoc().has(apiInterceptorName)) {
requestHandler.setHttpServerInterceptor(appContext.ioc().get(HttpServerInterceptor.class, apiInterceptorName));
}
else {
log.debugf("apiInterceptor name=%s not found in ioc , skiped", apiInterceptorName);
}
return new HttpServerStarter(httpConfig, requestHandler);
}
public Object getInstance(Class<?> controllerClazz) throws Exception {
if (controllerClazz.getAnnotation(IocBean.class) == null)
return controllerClazz.newInstance();
return appContext.getIoc().get(controllerClazz);
}
}

View File

@ -0,0 +1,30 @@
package org.nutz.boot.starter.tio.mvc;
import org.nutz.boot.AppContext;
import org.nutz.boot.starter.ServerFace;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.tio.http.server.HttpServerStarter;
/**
*
* @Author wendal
*/
@IocBean
public class TioMvcStarter implements ServerFace {
@Inject
private AppContext appContext;
protected HttpServerStarter httpServerStarter;
public void start() throws Exception {
httpServerStarter = appContext.getIoc().get(HttpServerStarter.class);
httpServerStarter.start();
}
public void stop() throws Exception {
if (httpServerStarter != null)
httpServerStarter.stop();
}
}

View File

@ -0,0 +1,2 @@
org.nutz.boot.starter.tio.mvc.TioMvcHttpServerBeans
org.nutz.boot.starter.tio.mvc.TioMvcStarter

View File

@ -1,4 +1,4 @@
package org.nutz.boot.starter.tio;
package org.nutz.boot.starter.tio.server;
import java.util.List;
@ -21,7 +21,7 @@ import org.tio.server.intf.ServerAioListener;
* @Time 2018年1月11日 19:00:01
*/
@IocBean(create = "init")
public class TioStarter implements ServerFace {
public class TioServerStarter implements ServerFace {
private static final Log log = Logs.get();

View File

@ -1 +1 @@
org.nutz.boot.starter.tio.TioStarter
org.nutz.boot.starter.tio.server.TioServerStarter

View File

@ -56,6 +56,7 @@
<module>nutzboot-starter-config-client</module>
<module>nutzboot-starter-j2cache</module>
<module>nutzboot-starter-freemarker</module>
<module>nutzboot-starter-tio-mvc</module>
</modules>
<dependencies>
<dependency>

12
pom.xml
View File

@ -484,6 +484,11 @@
<artifactId>nutzboot-starter-tio</artifactId>
<version>${nutzboot.version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-tio-mvc</artifactId>
<version>${nutzboot.version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-nutz-dao</artifactId>
@ -781,7 +786,12 @@
<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-core</artifactId>
<version>2.0.5.v20180115-RELEASE</version>
<version>2.0.8.v20180205-RELEASE</version>
</dependency>
<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-http-server</artifactId>
<version>0.0.8-tio-http</version>
</dependency>
<dependency>
<groupId>net.oschina.j2cache</groupId>