<dependency>
<groupId>io.nettygroupId>
<artifactId>netty-allartifactId>
<version>4.1.56.Finalversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.75version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
<scope>providedscope>
dependency>
package com.zhk.study.tomcat;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class MainServer {
private static final Logger log = LoggerFactory.getLogger(MainServer.class);
/** 默认端口 */
private int port = 9999;
public static Map<String, Map<Method, Class<?>>> servlet = new HashMap<>();
static {
// 此处目的是装载http接口的处理类,使用反射实现装载Controller注解的类
// TODO pack包路径必须拷贝自己项目controller那层的(必须是包路径)
servlet = new AnnotationScanner().getRequestMapping("com.zhk.study.tomcat");
}
/**
* 默认端口9999启动
*/
public void start() {
doStart();
}
/**
* netty核心简单实现
*/
private void doStart() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
ServerBootstrap server = new ServerBootstrap();
try {
server.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
//客户端连接时启动
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel client) throws Exception {
// HTTP应答编码器
client.pipeline().addLast(new HttpResponseEncoder());
// HTTP请求解码器
client.pipeline().addLast(new HttpRequestDecoder());
// Tomcat之Servlet处理类
client.pipeline().addLast(new ServletHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = server.bind(port).sync();
//监听关闭状态启动
log.info("Netty Server Started, Port:" + port);
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
//关闭线程池
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
/**
* Netty启动总入口
* @param args
*/
public static void main(String[] args) {
new MainServer().start();
}
}
package com.zhk.study.tomcat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.HttpRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
public class ServletHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 判断消息包是否属于http类型
if(msg instanceof HttpRequest){
// 属于既强转为HttpRequest,该类是netty包中自带的。
HttpRequest req= (HttpRequest) msg;
// 自定义request、response的处理类
HttpRequestServlet request=new HttpRequestServlet(ctx,req);
HttpResponseServlet response=new HttpResponseServlet(ctx,req);
String url=request.getUri();
// 查找已装配的servlet集合中是否含有该请求
if(MainServer.servlet.containsKey(url)){
// 下方将有多处反射查找请求对应接口的方法及请求参数
Map<Method, Class<?>> handler = MainServer.servlet.get(url);
Map<String, List<String>> requestParam = request.getParameters();
for(Map.Entry<Method, Class<?>> entry : handler.entrySet()) {
Method method = entry.getKey();
Class<?>[] paramType = method.getParameterTypes();
Object clazz = entry.getValue().newInstance();
Object paramObj = paramType[0].newInstance();
Field[] fields = paramObj.getClass().getDeclaredFields();
// 判断请求参数是否在接口方法中入参存在,如存在且转换数据类型。
for(String key : requestParam.keySet()) {
for(Field field : fields) {
if(key.equals(field.getName())) {
field.setAccessible(true);
Class<?> type = field.getType();
if(type == String.class) {
field.set(paramObj, requestParam.get(key).get(0));
continue;
}
if(type == int.class || type == Integer.class) {
field.set(paramObj, Integer.valueOf(requestParam.get(key).get(0)));
continue;
}
if(type == long.class || type == Long.class) {
field.set(paramObj, Long.valueOf(requestParam.get(key).get(0)));
continue;
}
if(type == byte.class || type == Byte.class) {
field.set(paramObj, Byte.valueOf(requestParam.get(key).get(0)));
continue;
}
if(type == boolean.class || type == Boolean.class) {
field.set(paramObj, Boolean.valueOf(requestParam.get(key).get(0)));
continue;
}
}
}
}
// 调用接口方法并传递请求参数。
Object retData = method.invoke(clazz, paramObj);
response.write(retData);
}
}else{
response.write("404");
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}
HttpRequestServlet
package com.zhk.study.tomcat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import java.util.*;
public class HttpRequestServlet {
private ChannelHandlerContext ctx;
private HttpRequest request;
public HttpRequestServlet(ChannelHandlerContext ctx, HttpRequest request){
this.ctx=ctx;
this.request=request;
}
public String getUri(){
String uri = request.uri();
String[] s = uri.split("\\?");
if(s.length < 2) {
return uri;
}
return s[0];
}
public String getMethod(){
return request.method().name();
}
public Map<String, List<String>> getParameters(){
QueryStringDecoder decoder=new QueryStringDecoder(request.uri());
return decoder.parameters();
}
public String getParameter(String name){
Map<String,List<String>> params=getParameters();
List<String> param=params.get(name);
if(param==null)return null;
else return param.get(0);
}
}
HttpResponseServlet
package com.zhk.study.tomcat;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
public class HttpResponseServlet {
private ChannelHandlerContext ctx;
private HttpRequest request;
private String code = "UTF-8";
public HttpResponseServlet(ChannelHandlerContext ctx, HttpRequest request) {
this.ctx = ctx;
this.request = request;
}
public void write(Object out) throws Exception {
try {
//设置HTTP及请求头信息
FullHttpResponse response = null;
response = new DefaultFullHttpResponse(
//设置版本
HttpVersion.HTTP_1_1,
//设置响应状态码
HttpResponseStatus.OK,
//设置输出格式
Unpooled.wrappedBuffer(out == null ? "".getBytes(code) : JSON.toJSONBytes(out)));
response.headers().set("Content-Type", "text/html;");
ctx.write(response);
} finally {
ctx.flush();
ctx.close();
}
}
}
AnnotationScanner
package com.zhk.study.tomcat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class AnnotationScanner {
private static final Logger log = LoggerFactory.getLogger(AnnotationScanner.class);
private Set<Class<?>> controllers;
public Map<String, Map<Method, Class<?>>> getRequestMapping(String pack) {
Map<String, Map<Method, Class<?>>> handler = new HashMap<>();
for (Class<?> cls : getControllers(pack)) {
Method[] methods = cls.getMethods();
for (Method method : methods) {
RequestMapping annotation = method.getAnnotation(RequestMapping.class);
if (annotation != null) {
String urlValue = annotation.value();
if (!urlValue.startsWith("/")) {
urlValue = "/" + urlValue;
}
log.info("loaded servlet:{}", urlValue);
Map<Method, Class<?>> invoke = new HashMap<>();
invoke.put(method, cls);
handler.put(urlValue, invoke);
}
}
}
return handler;
}
public Set<Class<?>> getControllers(String pack) {
if(controllers == null) {
controllers = new HashSet<>();
Set<Class<?>> clszzList = getClasses(pack);
if (clszzList != null && clszzList.size() > 0) {
for (Class<?> cls : clszzList) {
if (cls.getAnnotation(Controller.class) != null) {
controllers.add(cls);
}
}
}
}
return controllers;
}
private Set<Class<?>> getClasses(String pack) {
Set<Class<?>> classes = new HashSet<>();
boolean recursive = true;
String packDirName = pack.replace(".", "/");
Enumeration<URL> dirs;
try {
dirs = Thread.currentThread().getContextClassLoader().getResources(packDirName);
// 循环迭代下去
while (dirs.hasMoreElements()) {
// 获取下一个元素
URL url = dirs.nextElement();
// 得到协议的名称
String protocol = url.getProtocol();
// 如果是以文件的形式保存在服务器上
if ("file".equals(protocol)) {
// 获取包的物理路径
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
// 以文件的方式扫描整个包下的文件 并添加到集合中,以下俩种方法都可以
//网上的第一种方法,
findAndAddClassesInPackageByFile(pack, filePath, recursive, classes);
//网上的第二种方法
//addClass(classes,filePath,packageName);
} else if ("jar".equals(protocol)) {
// 如果是jar包文件
// 定义一个JarFile
JarFile jar;
try {
// 获取jar
jar = ((JarURLConnection) url.openConnection()).getJarFile();
// 从此jar包 得到一个枚举类
Enumeration<JarEntry> entries = jar.entries();
// 同样的进行循环迭代
while (entries.hasMoreElements()) {
// 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
JarEntry entry = entries.nextElement();
String name = entry.getName();
// 如果是以/开头的
if (name.charAt(0) == '/') {
// 获取后面的字符串
name = name.substring(1);
}
// 如果前半部分和定义的包名相同
if (name.startsWith(packDirName)) {
int idx = name.lastIndexOf('/');
// 如果以"/"结尾 是一个包
if (idx != -1) {
// 获取包名 把"/"替换成"."
pack = name.substring(0, idx).replace('/', '.');
}
// 如果可以迭代下去 并且是一个包
if ((idx != -1) || recursive) {
// 如果是一个.class文件 而且不是目录
if (name.endsWith(".class") && !entry.isDirectory()) {
// 去掉后面的".class" 获取真正的类名
String className = name.substring(pack.length() + 1, name.length() - 6);
try {
// 添加到classes
classes.add(Class.forName(pack + '.' + className));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
/**
* 以文件的形式来获取包下的所有Class
*
* @param packageName
* @param packagePath
* @param recursive
* @param classes
*/
public static void findAndAddClassesInPackageByFile(String packageName,
String packagePath, final boolean recursive, Set<Class<?>> classes) {
// 获取此包的目录 建立一个File
File dir = new File(packagePath);
// 如果不存在或者 也不是目录就直接返回
if (!dir.exists() || !dir.isDirectory()) {
// log.warn("用户定义包名 " + packageName + " 下没有任何文件");
return;
}
// 如果存在 就获取包下的所有文件 包括目录
File[] dirfiles = dir.listFiles(new FileFilter() {
// 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
@Override
public boolean accept(File file) {
return (recursive && file.isDirectory())
|| (file.getName().endsWith(".class"));
}
});
// 循环所有文件
for (File file : dirfiles) {
// 如果是目录 则继续扫描
if (file.isDirectory()) {
findAndAddClassesInPackageByFile(packageName + "."
+ file.getName(), file.getAbsolutePath(), recursive,
classes);
} else {
// 如果是java类文件 去掉后面的.class 只留下类名
String className = file.getName().substring(0,
file.getName().length() - 6);
try {
// 添加到集合中去
//classes.add(Class.forName(packageName + '.' + className));
classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));
} catch (ClassNotFoundException e) {
// log.error("添加用户自定义视图类错误 找不到此类的.class文件");
e.printStackTrace();
}
}
}
}
}
HiController
package com.zhk.study.tomcat;
@Controller
public class HiController {
@RequestMapping(value = "hi")
public User hi(User user) {
return user;
}
@RequestMapping(value = "say")
public User say(User user) {
return user;
}
}
user
package com.zhk.study.tomcat;
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private Long id;
private String name;
private int age;
private boolean status;
public User() {
}
public User(Long id, String name, int age, boolean status) {
this.id = id;
this.name = name;
this.age = age;
this.status = status;
}
}
Controller
package com.zhk.study.tomcat;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Controller {
}
RequestMapping
package com.zhk.study.tomcat;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
String value() default "";
}