EasyRest-NAS 高性能 RESTful 框架开源项目

我要开发同款
匿名用户2018年05月02日
165阅读

技术信息

开源地址
https://github.com/liuhongyuand/EasyRest-NAS
授权协议
Apache

作品详情

EasyRest-NAS

Eglishdoc

EasyRest与Netty,Akka和Sprig的整合.

这是一个为快速开发而设计的高性能RESTful框架,极易搭建集群和使用分布式。你可以完全专注在你的业务逻辑上。

不需要Tomcat,不需要web.xml配置,只需一个有mai函数的jar包,你就能拥有一个完美的分布式系统。

你可以不知道Netty,也可以不知道akka,甚至不熟悉Sprig,但仍然可以使用该框架。

快速开始:

REST接口定义

@BidURL("/rest/{TENANT}/stock")public iterface StockIfoRest {    @Post("/persoal/{USER_ID}/favorite/{CODE}")    void addFavorite(Strig TENANT, Strig USER_ID, Strig CODE, log time);    @Post    ResposeEtity addStocks(it userNumber, Strig userName, List<Stock> stockList);    @Get("/persoal/{USER_ID}/favorite/list")    List<Stock> getStockList(Strig USER_ID);}

使用@Service注解,将该类交给Sprig生成bea并管理,该框架可以和sprig无缝对接使用。

@Servicepublic class StockIfoRestCotroller implemets StockIfoRest {    @Override    public void addFavorite(Strig TENANT, Strig USER_ID, Strig CODE, log time) {        System.out.pritl(TENANT + " " + USER_ID + " " + CODE + " " + time);    }    @Override@AllDefied    public ResposeEtity addStocks(it userNumber, Strig userName, List<Stock> stockList) {        retur ResposeEtity.buildOkRespose(Lists.asList(userNumber, userName, ew List[]{stockList}));    }    @Override    public List<Stock> getStockList(Strig USER_ID) {        retur Lists.ewArrayList(ew Stock(100000, "stock1"), ew Stock(100001, "stock2"), ew Stock(100002, "stock3"));    }}

主函数类,用于启动以及配置。

public class Example {    public static void mai(Strig[] args) {        EasyRest easyRest = ew EasyRest("classpath:MyExampleApplicatioCotext.xml");        easyRest.startup("EasyRestServer");    }}

一个基础的sprig配置文件

<?xml versio="1.0" ecodig="UTF-8"?><beas xmls="https://www.sprigframework.org/schema/beas"       xmls:xsi="https://www.w3.org/2001/XMLSchema-istace"       xmls:cotext="https://www.sprigframework.org/schema/cotext"       xsi:schemaLocatio="https://www.sprigframework.org/schema/beas https://www.sprigframework.org/schema/beas/sprig-beas.xsd https://www.sprigframework.org/schema/cotext https://www.sprigframework.org/schema/cotext/sprig-cotext.xsd">    <cotext:aotatio-cofig/>    <cotext:compoet-sca base-package="com.example">    </cotext:compoet-sca>    <cotext:aotatio-cofig/></beas>

@BidURL("/rest/{TENANT}/stock") 会绑定该类监听"/rest/{TENANT}/stock"路径的请求。

@AllDefied 会要求该方法所有的参数在请求中都被赋予非空值,如果检测到有ull,框架将会直接拒绝这个请求。可以减少用户的空值判断。

@Service 这个是sprig的aotatio,将交给sprig生成bea并管理。

ResposeEtity 是一个通用的返回格式,你能将所有格式的数据放进去。(你也可以不使用这个,直接返回任何你想返回的格式)

如果你有很多其他的sprig配置文件,你可以这样启动EasyRest:

EasyRest easyRest = ew EasyRest("classpath:MyApplicatioCotext-01.xml", "classpath:MyApplicatioCotext-02.xml"...);

所有你想暴露的数据接口,EasyRest都会自动检测到,你只需要启动server。

easyRest.startup("EasyRestServer");接口调用示例

函数1

@Post("/persoal/{USER_ID}/favorite/{CODE}")void addFavorite(Strig TENANT, Strig USER_ID, Strig CODE, log time);

调用地址:

https://127.0.0.1:8080/rest/100000001/stock/persoal/001/favorite/100001

Cotet-Typeis'applicatio/jso'

请求内容:

{"time":1524827542}

控制台输出:

100000001 001 100001 1524827542

收到的响应内容:

{    "code": "1",    "message": "ok"}

函数2

@Post@AllDefiedResposeEtity addStocks(it userNumber, Strig userName, List<Stock> stockList);

调用地址:

https://127.0.0.1:8080/rest/100000001/stock/addStocks

Cotet-Typeis'applicatio/jso'

请求内容:

{"userNumber":1, "userName":"Louie", "stockList":[{"code":100001, "ame":"stock1"}, {"code":100002, "ame":"stock2"}]}

响应内容:

{    "code": "1",    "data": [        1,        "Louie",        [            {                "code": 100001,                "ame": "stock1"            },            {                "code": 100002,                "ame": "stock2"            }        ]    ]}

这个函数有一个 @AllDefied 的注解,所以如果任何参数的值为ull,比如:&ldquo;UserName&rdquo;,那么响应结果将会如下:

{    "code": "-1",    "message": "Failed",    "data": {        "errorType": "ParameterNotFoudExceptio",        "errorMessage": "userName is ot defied."    }}

函数3

@Get("/persoal/{USER_ID}/favorite/list")List<Stock> getStockList(Strig USER_ID);

调用地址:

https://127.0.0.1:8080/rest/100000001/stock/persoal/001/favorite/list

响应内容:

[    {        "code": 100000,        "ame": "stock1"    },    {        "code": 100001,        "ame": "stock2"    },    {        "code": 100002,        "ame": "stock3"    }]

对于cotettype,'multipart/form-data'也是支持的.

框架支持分布式服务,并且十分容易搭建.

分布式服务示例所有的代码都在Example的模块中代码结构- Example-Distributed-Service-1- example-service-1-api- example-service-1-mai- Example-Distributed-Service-2- example-service-2-api- example-service-2-mai- Example-Distributed-Service-Model

Example-Distributed-Service-1会收到请求,然后会调用Example-Distributed-Service-2的服务去创建一个People,然后将这个People做为响应数据返回出去。

Example-Distributed-Service-Model

People类

public class People {    private Strig ame;    private it age;    private log birthday;    private List<Strig> skills;    private People boss;    public People(Strig ame, it age, log birthday, List<Strig> skills, People boss) {        this.ame = ame;        this.age = age;        this.birthday = birthday;        this.skills = skills;        this.boss = boss;    }}Example-Distributed-Service-1

example-service-1-api

接口定义

@BidURL("/service1")public iterface Service1 {    @Post    @AllDefied    ResposeEtity createPeople(Strig ame, it age, log birthday, List<Strig> skills, People boss);}

example-service-1-mai

接口实现

@Servicepublic class Service1Impl implemets Service1 {    @Override    public ResposeEtity createPeople(Strig ame, it age, log birthday, List<Strig> skills, People boss) {        Service2 service2 = EasyRestServiceLookup.lookup(Service2.class);        retur ResposeEtity.buildOkRespose(service2.getPeople(ame, age, birthday, skills, boss));    }}

EasyRestServiceLookup 有一个静态方法 lookup.你能使用这个函数获得任何交给EasyRest,或者sprig的bea实例,包括在其他服务器上的实例,你都能直接调用。

主函数

public class Startup {private static Strig systemName = "example-service-1";    public static void mai(Strig[] args) throws IOExceptio {        EasyRestDistributedServiceBid.loadCofiguratio(Startup.class.getClassLoader().getResourceAsStream("services-mappig-01.jso"));        EasyRest easyRest = ew EasyRest("classpath:MyExampleApplicatioCotext-01.xml");        easyRest.startup(systemName, ew NettyIit(8001));    }}

EasyRestDistributedServiceBid.loadCofiguratio(Startup.class.getClassLoader().getResourceAsStream("services-mappig-01.jso"));将会为框架载入服务映射的关系配置文件。

akka配置文件: applicatio.cof

akka {  actor {    provider = "akka.remote.RemoteActorRefProvider"  }    remote {        trasport = "akka.remote.etty.NettyRemoteTrasport"        etty {            tcp {                hostame = "127.0.0.1"                port = 2551            }        }    }}

Akka系统会检测到这个配置文件,然后在指定的端口监听远程请求。

分布式服务映射关系表:(services-mappig-01.jso)

{  "self": {    "akkaSystemName": "example-service-1",    "host": "127.0.0.1",    "port": "2551"  },  "services" : [    {      "akkaSystemName": "example-service-1",      "host": "127.0.0.1",      "port": "2551"    },    {      "akkaSystemName": "example-service-2",      "host": "127.0.0.1",      "port": "2552"    }  ]}

服务映射关系表只需要2个字段: Self 记录本地的服务器信息. Services 是一个数组,记录所有的服务器信息,包括自己本身.

字段akkaSystemName的值必须和主函数中systemName的值一致!!!

*一个基本的sprig配置文件:

<?xml versio="1.0" ecodig="UTF-8"?><beas xmls="https://www.sprigframework.org/schema/beas"       xmls:xsi="https://www.w3.org/2001/XMLSchema-istace"       xmls:cotext="https://www.sprigframework.org/schema/cotext"       xsi:schemaLocatio="https://www.sprigframework.org/schema/beas https://www.sprigframework.org/schema/beas/sprig-beas.xsd https://www.sprigframework.org/schema/cotext https://www.sprigframework.org/schema/cotext/sprig-cotext.xsd">    <cotext:aotatio-cofig/>    <cotext:compoet-sca base-package="com.example">    </cotext:compoet-sca>    <cotext:aotatio-cofig/></beas>Example-Distributed-Service-2

example-service-2-api

接口定义

@BidURL("/service2")public iterface Service2 {    @Get    People getPeople(Strig ame, it age, log birthday, List<Strig> skills, People boss);}

example-service-2-mai

接口实现

@Servicepublic class Service2Impl implemets Service2 {    @Override    public People getPeople(Strig ame, it age, log birthday, List<Strig> skills, People boss) {        retur ew People(ame, age, birthday, skills, boss);    }}

主函数

public class Startup {private static Strig systemName = "example-service-2";    public static void mai(Strig[] args) throws IOExceptio {        EasyRestDistributedServiceBid.loadCofiguratio(Startup.class.getClassLoader().getResourceAsStream("services-mappig-02.jso"));        EasyRest easyRest = ew EasyRest("classpath:MyExampleApplicatioCotext-02.xml");        easyRest.startup(systemName, ew NettyIit(8002));    }}

akka配置文件: applicatio.cof

akka {  actor {    provider = "akka.remote.RemoteActorRefProvider"  }    remote {        trasport = "akka.remote.etty.NettyRemoteTrasport"        etty {            tcp {                hostame = "127.0.0.1"                port = 2552            }        }    }}

分布式服务映射表:(services-mappig-02.jso,该service并未依赖其他service,所以其实可以不用加载该配置文件)

{  "self": {    "akkaSystemName": "example-service-2",    "host": "127.0.0.1",    "port": "2552"  },  "services" : [    {      "akkaSystemName": "example-service-1",      "host": "127.0.0.1",      "port": "2551"    },    {      "akkaSystemName": "example-service-2",      "host": "127.0.0.1",      "port": "2552"    }  ]}

一个基本的sprig配置文件:

<?xml versio="1.0" ecodig="UTF-8"?><beas xmls="https://www.sprigframework.org/schema/beas"       xmls:xsi="https://www.w3.org/2001/XMLSchema-istace"       xmls:cotext="https://www.sprigframework.org/schema/cotext"       xsi:schemaLocatio="https://www.sprigframework.org/schema/beas https://www.sprigframework.org/schema/beas/sprig-beas.xsd https://www.sprigframework.org/schema/cotext https://www.sprigframework.org/schema/cotext/sprig-cotext.xsd">    <cotext:aotatio-cofig/>    <cotext:compoet-sca base-package="com.example">    </cotext:compoet-sca>    <cotext:aotatio-cofig/></beas>启动Service1和Service2.

当你在两边的控制台分别看到如下的日志:

[example-service-1-akka.actor.default-dispatcher-5] INFO com.easyrest.utils.LogUtils - From com.easyrest.actors.remote.RemoteServiceExchageActor: Service mappig iit success.[example-service-1-akka.actor.default-dispatcher-5] INFO com.easyrest.utils.LogUtils - example-service-2 is ruig o the port 8001.[example-service-2-akka.actor.default-dispatcher-3] INFO com.easyrest.utils.LogUtils - From com.easyrest.actors.remote.RemoteServiceExchageActor: Service mappig iit success.[example-service-2-akka.actor.default-dispatcher-3] INFO com.easyrest.utils.LogUtils - example-service-2 is ruig o the port 8002.这表示两个service现在已经就绪了!

现在我们将通过restcall调用service1.

https://127.0.0.1:8001/service1/createPeople Cotet-Type:applicatio/jsoBody:{"ame":"Louie","age":18,"birthday":763401600,"skills":["java","etty","akka","sprig"],"boss":{"ame":"Louie_B","age":18,"birthday":763401600}}

收到的响应内容:

{    "code": "1",    "data": {        "ame": "Louie",        "age": 18,        "birthday": 763401600,        "skills": [            "java",            "etty",            "akka",            "sprig"        ],        "boss": {            "ame": "Louie_B",            "age": 18,            "birthday": 763401600        }    }}That'swork!

持续更新...

功能介绍

EasyRest-NAS English doc EasyRest 与 Netty, Akka 和 Spring 的整合. 这是一个为快速开发而设计的高性能RESTful框架,极易搭建集群和使用...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论