preface

As the saying goes “Don’t duplicate the wheel”, it is not necessary to leave the scope of this discussion.

The main purpose of this project is to improve myself, see the gap with well-known open source projects and learn good open source methods.

All right, let’s focus on the core functions of cicADA.

I define it as a fast, lightweight WEB framework; There are no excessive dependencies, and the core JAR package is only 30KB.

It only takes one line of code to start an HTTP service.


features

Now let’s talk about a few important features.

The current version mainly implements basic request, response, custom parameters, and interceptor functions.

Functions are few, but the five organs are complete.

In the future iteration process will gradually improve the above function, have good ideas also welcome to github.com/crossoverJi… .

Quick start

Let’s look at how to quickly start an HTTP service.

Just create a Maven project and introduce the core package.

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

Just configure another startup class, as shown in the figure above.

public class MainStart {

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

Configuring Service Actions

Of course we also need a place to implement the business logic. Cicada provides an interface that only needs to be implemented to implement concrete logic.

Create a business Action to achieve 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) ;
        returnres; }}Copy the code

You also need to add the @cicadaAction annotation to the custom class and specify a value, which is mainly to find the business class when requesting the route.

This starts the application and accesses it

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

You can execute the business logic and get a return from the server.

Json responses are currently supported by default, and template parsing will be added later.

Related logs are also printed in the service.

Flexible parameter configuration

All of the request parameters are encapsulated in Param, and the various apis are available to retrieve the request data.

It’s flexible: we can even ask:

http://127.0.0.1:7317/cicada-example/demoAction?jsonData="info": {
    "age": 22."name": "zhangsan"
  }
Copy the code

This allows you to pass data of any structure, as long as it is parsed during business processing.

Custom interceptors

Interceptors are a basic feature of a framework that can be used to perform common tasks such as logging, transaction committing, and so on.

Cicada provides an interface for this: top crossoverjie. Cicada. Server intercept. CicadaInterceptor.

We only need to implement this interface to write interception:

@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); }}Copy the code

This is an example of logging the execution time of all actions.

Currently only action interception is implemented by default, and custom interceptors will be added later.

Interceptor adapter

Although before/after methods are provided in interceptors, not all of them need to be implemented.

So CICADA provides an adapter:

top.crossoverjie.cicada.server.intercept.AbstractCicadaInterceptorAdapter

We need to inherit it to implement one of these methods on demand, as follows:

@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()); }}Copy the code

The performance test

Since it is an HTTP service framework, performance must be guaranteed.

In the test conditions: 300 concurrent continuous pressure test two rounds; 1 gb memory, single CPU, 1Mbps. Pressure measurement with Jmeter is as follows:

The same server is pressed with Tomcat to see the results.

Tomcat thread pool configuration:

<Executor name="tomcatThreadPool" namePrefix="consumer-exec-"
        maxThreads="510" minSpareThreads="10"/>
Copy the code

I requested a Doc directory for Tomcat, although it turns out cicada is better than Tomcat.

However, the variables in this comparison process are not completely controlled. Tomcat returns HTML, while CICada only returns JSON. Of course, there are more problems.

But cicada’s current performance is still good.

conclusion

This article does not discuss cicADA implementation principle too much, interested can look at the source code, are relatively simple.

We’ll explore this in more detail in a future update.

Not surprisingly, cicada will continue to be updated and more useful features will be added in the future.

I will even apply it to my production projects at the right time, and I hope more friends can join me to make this “wheel” better.

Project address: github.com/crossoverJi…

Your likes and retweets are the biggest support.