31

GitHub - TogetherOS/cicada: ? Fast lightweight HTTP service framework.

 5 years ago
source link: https://github.com/TogetherOS/cicada
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

68747470733a2f2f7773332e73696e61696d672e636e2f6c617267652f303036744e6252776c7931667576667862633779316a3330676f3065396161792e6a7067

Build Status QQ群


中文文档


Introduction

Fast, lightweight Web framework based on Netty; without too much dependency, and the core jar package is only 30KB.

If you are interested, please click Star.

Features

  • Clean code, without too much dependency.
  • One line of code to start the HTTP service.
  • Custom interceptor.
  • Flexible parameters way.
  • Response json.
  • Start with jar.
  • Custom configuration.
  • Multiple routing ways.
  • Support HTTPS.
  • Support Cookie.
  • File Upload.

Quick Start

Create a project with Maven, import core dependency.

<dependency>
    <groupId>top.crossoverjie.opensource</groupId>
    <artifactId>cicada-core</artifactId>
    <version>1.0.0</version>
</dependency>

start class:

public class MainStart {

    public static void main(String[] args) throws InterruptedException {
        CicadaServer.start(MainStart.class,"/cicada-example") ;
    }
}

Configuring business Action

Create business Action implement top.crossoverjie.cicada.server.action.WorkAction interface:

@CicadaAction(value = "demoAction")
public class DemoAction implements WorkAction {


    private static final Logger LOGGER = LoggerBuilder.getLogger(DemoAction.class) ;

    private static AtomicLong index = new AtomicLong() ;

    @Override
    public WorkRes<DemoResVO> execute(Param paramMap) throws Exception {
        String name = paramMap.getString("name");
        Integer id = paramMap.getInteger("id");
        LOGGER.info("name=[{}],id=[{}]" , name,id);

        DemoResVO demoResVO = new DemoResVO() ;
        demoResVO.setIndex(index.incrementAndGet());
        WorkRes<DemoResVO> res = new WorkRes();
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMessage(StatusEnum.SUCCESS.getMessage());
        res.setDataBody(demoResVO) ;
        return res;
    }

}

Launch and apply access: http://127.0.0.1:7317/cicada-example/demoAction?name=12345&id=10

{
    "code": "9000",
    "dataBody": {
        "index": 1
    },
    "message": "成功"
}

Custom interceptor

Implement top.crossoverjie.cicada.example.intercept.CicadaInterceptor interface.

@Interceptor(value = "executeTimeInterceptor")
public class ExecuteTimeInterceptor implements CicadaInterceptor {

    private static final Logger LOGGER = LoggerBuilder.getLogger(ExecuteTimeInterceptor.class);

    private Long start;

    private Long end;

    @Override
    public void before(Param param) {
        start = System.currentTimeMillis();
    }

    @Override
    public void after(Param param) {
        end = System.currentTimeMillis();

        LOGGER.info("cast [{}] times", end - start);
    }
}

Interceptor Adapter

If you only want to implement one of the methods ,only extends top.crossoverjie.cicada.server.intercept.AbstractCicadaInterceptorAdapter abstract class.

@Interceptor(value = "loggerInterceptor")
public class LoggerInterceptorAbstract extends AbstractCicadaInterceptorAdapter {

    private static final Logger LOGGER = LoggerBuilder.getLogger(LoggerInterceptorAbstract.class) ;

    @Override
    public void before(Param param) {
        LOGGER.info("logger param=[{}]",param.toString());
    }

}

Performance Test

Test Conditions: 300 concurrency for twice ;1G RAM/one CPU/1Mbps.

Contact author

crossoverJie#gmail.com

68747470733a2f2f7773322e73696e61696d672e636e2f6c617267652f303036744b6654636c793166736130317537726f316a3330677330686f7766712e6a7067


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK