saowu's Blog

注重实践,所以自己造了一个Springboot

注重实践,所以自己造了一个Springboot
2020-05-09 · 5 min read
Repositories Java

(仅仅是一个仿制Springboot的demo,学习实践而已)

👉GitHub

零、技术特点

1.使用Netty作为http服务器
2.支持注解@Controller、@RequestMapping等
3.利用maven项目管理
4.简洁的请求数据提取
6.HikariCP连接池
7.IoC设计(反射实现)
8.ORM封装(需大力重构)
9.AOP注解(测试中)

一、启动Banner

 _   _ _   _         _   _      _   _
| | | | |_| |_ _ __ | \ | | ___| |_| |_ _   _
| |_| | __| __| '_ \|  \| |/ _ \ __| __| | | |
|  _  | |_| |_| |_) | |\  |  __/ |_| |_| |_| |
|_| |_|\__|\__| .__/|_| \_|\___|\__|\__|\__, |
              |_|    Author:saowu       |___/

二、注解机制

  • 类级注解@Controller、@Component、@Service、@Repository、@Table
  • 方法级注解@RequestMapping
  • 字段级注解@Autowired、@Column

三、路由控制器

  • 勉强支持RESTful API规范
@Controller(path = "/index")
public class IndexController {
    @RequestMapping(method = RequestMethod.POST, path = "/test")
    public String test(Map<String, Object> map) {
        return JSONObject.toJSONString(map);
    }
}
Http Netty : 2020-05-09 04:34:34 GET -> /
Http Netty : 2020-05-09 04:34:34 GET -> /static/css
Http Netty : 2020-05-09 04:34:34 GET -> /static/js
Http Netty : 2020-05-09 04:34:34 GET -> /static/img
Http Netty : 2020-05-09 04:35:30 POST -> /index/test

四、请求数据提取

  • 将GET、PUOST、PUT、DELETE请求数据统一解析成Map,无论是application/x-www-form-urlencodedmultipart/form-data还是application/json

表单接收

     @RequestMapping(method = RequestMethod.POST, path = "/test")
     public String test(Map<String, Object> map) {
        return JSONObject.toJSONString(map);
     }

     @RequestMapping(method = RequestMethod.PUT, path = "/test2")
     public String test2(Map<String, Object> map) {
         return JSONObject.toJSONString(map);
     }

URI接收

     @RequestMapping(method = RequestMethod.GET, path = "/test1")
     public String test1(Map<String, Object> map) {
         return JSONObject.toJSONString(map);
     }
     @RequestMapping(method = RequestMethod.DELETE, path = "/test3")
     public String test3(Map<String, Object> map) {
         return JSONObject.toJSONString(map);
     }
 

文件接收

    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    public String uploadfile(Map<String, Object> map) {
        HashMap<String, String> fileInfo = new HashMap<>();
        for (String key : map.keySet()) {
            //获取文件对象并保存
            String path = IOUtils.saveFileUpload(key, map.get(key));
            fileInfo.put(key, path);
        }
        return JSONObject.toJSONString(fileInfo);
     }

五、依赖注入

    @Component
    public class TestService {
        @Autowired
        private TestDao testDao;

        public List<Files> selectAll() {
            List<Files> filesList = testDao.selectAll();
            return filesList;
        }

    }

六、数据库连接池

hikari.properties

jdbcUrl=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driverClassName=com.mysql.jdbc.Driver
dataSource.user=root
dataSource.password=123456
dataSource.connectionTimeout=30000
dataSource.idleTimeout=600000
dataSource.maxLifetime=1800000
dataSource.maximumPoolSize=30

注解 @Table、@Column

@Table
public class Files {
    @Column(name = "id")
    private String id;

    @Column(name = "name")
    private String name;

    @Column(name = "path")
    private String path;

    @Column(name = "type")
    private String type;

  /*Getter、Setter*/
}

七、ORM API

@Repository
public class TestDao {

    @Autowired
    private PoolUtils poolUtils;

    public List<Files> selectFiles(Map<String, Object> map) {
        List<Files> filesList = ORMUtils.executeQuery(poolUtils, Files.class, map);
        return filesList;
    }

    public boolean updateFiles(Files files) {
        int i = ORMUtils.executeUpdate(poolUtils, Files.class, files);
        return i > 0;
    }

    public boolean insertFiles(Files files) {
        int i = ORMUtils.executeInsert(poolUtils, Files.class, files);
        return i > 0;
    }

    public boolean deleteFiles(Map<String, Object> map) {
        int i = ORMUtils.executeDelete(poolUtils, Files.class, map);
        return i > 0;
    }

}

八、项目结构

.
├── README.md
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── org
    │   │       └── saowu
    │   │           ├── Application.java
    │   │           ├── controller
    │   │           │   └── TestController.java
    │   │           ├── core
    │   │           │   ├── annotation
    │   │           │   │   ├── Autowired.java
    │   │           │   │   ├── Column.java
    │   │           │   │   ├── Component.java
    │   │           │   │   ├── Controller.java
    │   │           │   │   ├── Entity.java
    │   │           │   │   ├── Repository.java
    │   │           │   │   ├── RequestMapping.java
    │   │           │   │   └── Service.java
    │   │           │   ├── banner
    │   │           │   ├── config
    │   │           │   │   └── ApplicationContext.java
    │   │           │   ├── pojo
    │   │           │   │   ├── RequestMethod.java
    │   │           │   │   ├── RouteInfo.java
    │   │           │   │   ├── SCSS.java
    │   │           │   │   ├── SIMG.java
    │   │           │   │   ├── SJS.java
    │   │           │   │   └── Template.java
    │   │           │   ├── server
    │   │           │   │   ├── BootServer.java
    │   │           │   │   ├── HttpRequestHandler.java
    │   │           │   │   └── InitCenter.java
    │   │           │   └── utils
    │   │           │       ├── AnnotationUtils.java
    │   │           │       ├── HttpRequestUtils.java
    │   │           │       ├── IOUtils.java
    │   │           │       ├── PoolUtils.java
    │   │           │       ├── ReflexUtils.java
    │   │           │       ├── ResponseUtils.java
    │   │           │       └── ResultSetUtils.java
    │   │           ├── dao
    │   │           │   └── TestDao.java
    │   │           ├── entity
    │   │           │   └── Files.java
    │   │           └── service
    │   │               └── TestService.java
    │   └── resources
    │       ├── hikari.properties
    │       ├── static
    │       │   ├── css
    │       │   │   └── index.css
    │       │   ├── img
    │       │   │   └── picture.png
    │       │   └── js
    │       │       └── index.js
    │       ├── templates
    │       │   └── index.html
    │       └── upload
    └── test
        └── java
            └── org
                └── saowu
                    └── AppTest.java

26 directories, 39 files

九、Browser Console

十、siege压力测试

siege -c 200 -r 100 http://127.0.0.1:9000/test1

{	"transactions":			       19855,
	"availability":			       99.28,
	"elapsed_time":			       41.73,
	"data_transferred":		        0.04,
	"response_time":		        0.31,
	"transaction_rate":		      475.80,
	"throughput":			        0.00,
	"concurrency":			      149.54,
	"successful_transactions":	       19855,
	"failed_transactions":		         145,
	"longest_transaction":		       19.77,
	"shortest_transaction":		        0.00
}

那当然是我太菜了,不是netty的问题

Copyright © 2020 - 2024 saowu. All Right Reserved
Powered by Gridea