Objective
As the saying goes, "Don't reinvent the wheel," about whether it is necessary to stop this discussion.
The main purpose of creating this project is to improve yourself, look at the gap between well-known open source projects and learn the best way to open source.
Well, now focus on the core features of the Cicada project.
I define him as a fast, lightweight WEB framework, and without too much reliance, the core jar package is only 30KB.
You can start a service with just one line of code HTTP
.
Characteristics
Now let's talk about some important features.
The current version primarily implements basic requests, responses, custom parameters, and interceptor functionality.
Although the function is small, but perfectly formed.
In the future iterative process will gradually improve the function, there are good ideas and welcome to mention Github.com/crossoverjie/cicada/issues.
Quick Start
Let's look at how to start an HTTP service quickly.
You just need to create a Maven project and introduce a core package.
<dependency> <groupId>top.crossoverjie.opensource</groupId> <artifactId>cicada-core</artifactId> <version>1.0.0</version></dependency>
As shown, configure a startup class.
public class MainStart { public static void main(String[] args) throws InterruptedException { CicadaServer.start(MainStart.class,"/cicada-example") ; }}
Configure Business Action
Of course we also need a place to implement business logic. cicada
provides an interface that implements specific logic only if the interface is implemented.
Create a business Action implementation 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; }}
You also need to add annotations to the custom class, @CicadaAction
and you need to specify a value
value that is primarily intended to find the business class when the route is requested.
This launches the app and accesses
http://127.0.0.1:7317/cicada-example/demoAction?name=12345&id=10
Can execute the business logic and get the return of the server.
The response is currently supported by default json
, and template parsing is also added later.
Related logs are also printed in the service.
Flexible configuration of parameters
All of the request parameters are encapsulated here Param
, and the various APIs can be used to get the request data.
The reason is flexible: we can even request this:
http://127.0.0.1:7317/cicada-example/demoAction?jsonData="info": { "age": 22, "name": "zhangsan" }
This allows you to pass data of any structure, as long as the business process is resolved.
Custom Interceptors
Interceptors are the basic functions of a framework and can be used to implement common tasks such as logging and transaction submission.
cicada
provide an interface for this: top.crossoverjie.cicada.server.intercept.CicadaInterceptor
.
We only need to implement this interface to write interception capabilities:
@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); }}
This is a demonstration of the execution time of all actions.
Currently, only action interception is implemented by default, and custom interceptors are added later.
Intercept Adapter
Although there are two methods available in interceptors before/after
, not all methods need to be implemented.
Therefore cicada
, an adapter is provided:
top.crossoverjie.cicada.server.intercept.AbstractCicadaInterceptorAdapter
We need to inherit from him to implement one of these methods as needed, 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()); }}
Performance testing
Since it is an HTTP service framework, the performance will naturally be guaranteed.
In the test conditions are: 300 并发连续压测两轮;1G 内存、单核 CPU、1Mbps。
with Jmeter pressure measurement situation as follows:
The same server uses TOMCAT to test the results.
Tomcat's thread pool configuration:
<Executor name="tomcatThreadPool" namePrefix="consumer-exec-" maxThreads="510" minSpareThreads="10"/>
What I'm requesting here is a doc directory for Tomcat, although the results seem cicada
to be more powerful than Tomcat.
But in fact the variables in this contrast process are not fully controlled, and Tomcat returns the HTML, but cicada
only the JSON, of course, the problem is more than that.
But still can explain cicada
the current performance is good.
Summarize
This article did not discuss the cicada
principle of implementation, interested can look at the source code, are relatively simple.
This piece of content is carefully explored in subsequent updates.
At the same time cicada
, no accident will continue to update, the future will add more practical features.
Even I will apply it to my production projects at the right time, and I hope more friends can get involved and make this "wheel" better.
Project Address: Github.com/crossoverjie/cicada
Your point of praise and forwarding is the biggest support.