dushitaoyuan / cicada

πŸš€ Fast lightweight HTTP service framework.

Home Page:https://crossoverjie.top/categories/cicada/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


Build Status QQηΎ€

πŸ“˜Features |🌁Quick Start | πŸ–Performance Test | 🌈ChangeLog | πŸ’‘ Contact Author|πŸ‡¨πŸ‡³δΈ­ζ–‡ζ–‡ζ‘£



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.2</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 configuration

By default, the configuration file under the classpath is read.

You can also customize the configuration file.

Just need to extends top.crossoverjie.cicada.server.configuration.AbstractCicadaConfiguration class.

Write the name of the configuration file at the same time.

Like this:

public class RedisConfiguration extends AbstractCicadaConfiguration {


    public RedisConfiguration() {
        super.setPropertiesName("redis.properties");
    }

}

public class KafkaConfiguration extends AbstractCicadaConfiguration {

    public KafkaConfiguration() {
        super.setPropertiesName("kafka.properties");
    }


}

Get configuration information

Get the configuration infomation, follow this:

KafkaConfiguration configuration = (KafkaConfiguration) getConfiguration(KafkaConfiguration.class);
RedisConfiguration redisConfiguration = (RedisConfiguration) ConfigurationHolder.getConfiguration(RedisConfiguration.class);
ApplicationConfiguration applicationConfiguration = (ApplicationConfiguration) ConfigurationHolder.getConfiguration(ApplicationConfiguration.class);

String brokerList = configuration.get("kafka.broker.list");
String redisHost = redisConfiguration.get("redis.host");
String port = applicationConfiguration.get("cicada.port");

LOGGER.info("Configuration brokerList=[{}],redisHost=[{}] port=[{}]",brokerList,redisHost,port);

External configuration file

Configuration files can also be read in multiple environments, just add VM parameters, also ensure that the parameter name and file name are consistent.

-Dapplication.properties=/xx/application.properties
-Dkafka.properties=/xx/kakfa.properties
-Dredis.properties=/xx/redis.properties

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: 100 threads and 100 connections ;1G RAM/4 CPU

Nearly 10W requests per second.

ChangeLog

v1.0.2

  • fixed #6
  • Customize the configuration file.
  • Using flexible.
  • Refactor the code.

Contact author

crossoverJie#gmail.com

About

πŸš€ Fast lightweight HTTP service framework.

https://crossoverjie.top/categories/cicada/

License:Apache License 2.0


Languages

Language:Java 99.3%Language:Shell 0.7%