Giter Site home page Giter Site logo

ligzy / sardine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lbsha/sardine

0.0 1.0 0.0 91 KB

lightweight micro service framework | 高性能微服务框架 --- 沙丁鱼,小而迅速,多而勇猛。

Home Page: http://sardine.ibruce.info

License: MIT License

Java 99.80% HTML 0.20%

sardine's Introduction

Sardine

中文文档

整个项目未完成,尚处于开发中,敬请期待。。。

Lightweight framework for creating small standalone Java EE applications in a micro service way.

轻量级高性能微服务框架,本项目是 Netty5+Java8 的 Sinatra 实现。

Sardine

沙丁鱼,世界重要海洋经济鱼类。硬骨鱼纲鲱形目鲱科,形态延长,侧扁,腹部具棱鳞。小者二寸,大者尺许,密集群息,沿岸洄游。

Introduction

Lightweight micro service framework with Java8

  • Java8: functional、Lambda、Stream
  • Netty5: 事件驱动、异步高性能、高可靠性
  • Tiny: 全部源码只有100k+,仅依赖Netty

Quick Start

a line code, a http server。

public static void main(String[] args) {
    get("/", () -> "hello sardine.");
}

DONE! Now, Run and View:

http://localhost:9527/

Tutorials

Dependency

<dependency>
    <groupId>sardine</groupId>
    <artifactId>sardine-all</artifactId>
    <version>${sardine-version}</version>
</dependency>

启动

import static sardine.Sardine.*;

public class SardineServerTest {
    public static void main(String[] args) {
        port(9527);
        get("/", () -> "hello sardine.");
    }
}

Route

Route 由三部分组成:

  • 动作:http 动作的一种,如:GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH
  • 路径:http 请求路径,如:/home, /books/:id, /books/:author
  • 函数:消费http request 产生 http response,如:() -> {}, request, response) -> {}

此外,还可以有:

  • 条件函数:条件函数,如:(request) -> {}, request -> "127.0.0.1".equals(request.ip())
  • 接收类型:即 http accept type,如:Accept: text/html,*/*, Accept: application/json

具体的示例:

get("/books/:author", (request, response) -> {
    return "query";
});

post("/book", (request, response) -> {
    return "created";
});

put("/books/:id", (request, response) -> {
    return "updated";
});

delete("/books/:id", (request, response) -> {
    return "deleted";
});

// TODO restful verb 介绍

Parameters

Named parameters

Wildcards parameters

Query parameters

条件Condition

条件函数

get("/hello", request -> "127.0.0.1".equals(request.ip()), (request, response) -> {
    return "hello sardine.";
});

Request

request.host();
request.port();
request.method();
request.path();
request.scheme();
request.contentType();
request.accept();
request.ajax();

request.headers();
request.headers("foo");

request.cookies();
request.cookies("name");

request.params();
request.params("foo");
request.paramsOptional("foo")
request.paramsOrElse("foo")

request.splats()
request.splatsAsList()
request.splatsFirst()
request.splatsLast()

request.queryParams();
request.queryParams("foo");
request.multiQueryParams("foo");

request.body();
request.bodyAsByte();
request.bodyLength();

Response

response.status(404);
response.header("foo", "bar");
response.cookie("foo", "bar");
response.contentType("application/json");

response.body();
response.body("are you ok ?");

response.redirect("/login");
response.file("/hello.html");

Cookie

Halt

halt();
halt(403);
halt("a u ok?");
halt(503, "i got u!");

Filter

Redirect

Rewrite

Exception

Static assets

staticFileLocation("/resources");

externalStaticFileLocation("/var/www/resources");

ResponseTransformer

json

MVC

Gzip

日志

监控

示例


1、HelloSardine

最简版:

import static sardine.Sardine.*;

public class HelloSardine {
    public static void main(String[] args) {
        get("/", () -> "hello sardine.");
    }
}

Run:http://localhost:9527/

Java8 语法糖版:

import static sardine.Sardine.*;

public class HelloSardine {
    public static void main(String[] args) {
        get("/", new HelloSardine()::hello);
    }
    public String hello() { return "hello sardine"; }
}

Run:http://localhost:9527/

import static sardine.Sardine.*;

public class HelloSardine {
    public static void main(String[] args) {
        get("/", HelloSardine::hello);
    }
    public static String hello() { return "hello sardine"; }
}

Run:http://localhost:9527/

实例

Demo工程

真实案例

不蒜子:http://busuanzi.ibruce.info

压力测试

ab

参考资料

sardine's People

Contributors

lbsha avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.