RedAnt is a Netty-based lightweight web container
characteristic:
Redant is a Netty-based web container, similar to Tomcat and WebLogic.
You only need to start a Server and the default implementation class is NettyHttpServer to quickly start a web container, as shown below:
public final class ServerBootstrap {
public static void main ( String [] args ) {
Server nettyServer = new NettyHttpServer ();
// 各种初始化工作
nettyServer . preStart ();
// 启动服务器
nettyServer . start ();
}
}So far, I have described all single-node mode. If the performance of a single-node cannot be met on one day, then I need to use a cluster, so I have also implemented the cluster mode.
The cluster mode is composed of a master node and several slave nodes. After receiving the request, the master node forwards the request to the slave node for processing. The slave node returns the processed result to the master node, and the master node responds to the result to the request.
To implement the cluster model, we need to have a service registration and discovery function. Currently, we use Zk to do service registration and discovery.
Because the master node needs to forward the request to the slave node, the master node needs to know which slave nodes are currently there. I use ZooKeeper to implement service registration and discovery.
If you do not have a Zk server available, you can start a ZooKeeper server by running the following Main method:
public final class ZkBootstrap {
private static final Logger LOGGER = LoggerFactory . getLogger ( ZkBootstrap . class );
public static void main ( String [] args ) {
try {
ZkServer zkServer = new ZkServer ();
zkServer . startStandalone ( ZkConfig . DEFAULT );
} catch ( Exception e ){
LOGGER . error ( "ZkBootstrap start failed,cause:" , e );
System . exit ( 1 );
}
}
}This way you can use this Zk when starting the master and slave node later. But this is not necessary. If you already have a running Zk server, you can use it directly when starting the master and slave node, by specifying the address of Zk in the main method parameters.
Just run the following code to start a master node:
public class MasterServerBootstrap {
public static void main ( String [] args ) {
String zkAddress = ZkServer . getZkAddressArgs ( args , ZkConfig . DEFAULT );
// 启动MasterServer
Server masterServer = new MasterServer ( zkAddress );
masterServer . preStart ();
masterServer . start ();
}
}If the Zk address is specified in the main method parameters, the service discovery will be performed through this address, otherwise the default Zk address will be used.
Just run the following code to start a slave node:
public class SlaveServerBootstrap {
public static void main ( String [] args ) {
String zkAddress = ZkServer . getZkAddressArgs ( args , ZkConfig . DEFAULT );
Node node = Node . getNodeWithArgs ( args );
// 启动SlaveServer
Server slaveServer = new SlaveServer ( zkAddress , node );
slaveServer . preStart ();
slaveServer . start ();
}
}If the Zk address is specified in the main method parameters, the service registration will be carried out through this address, otherwise the default Zk address will be used.
You can try it by running the example provided in the redant-example module, which has two controllers built into the example module.
After starting, you can visit http://127.0.0.1:8888 in your browser to view the specific effects (the default port can be modified in redant.properties)
If you see a message like this: "Welcome to redant!", it means that you have started successfully.
In the redant-example module, the following default routes are built in:
| Method Type | URL | Response Type |
|---|---|---|
| GET | / | HTML |
| * | * | HTML |
| GET | /user/count | JSON |
| GET | /user/list | JSON |
| GET | /user/info | JSON |
Like Spring, you can manage all objects through @Bean annotation and automatically inject them through @Autowired.
Tips: For more information, please check wiki: Bean
Like Spring, you can customize a Controller through @Controller.
@Mapping annotation is used at the method level, @Controller + @Mapping only defines an http request.
@Param annotation is used on the parameters of the method. This annotation allows you to automatically convert the basic type into a POJO object.
Tips: For more information, please check wiki: Router
The Cookie Manager can manage user-defined cookies.
Tips: For more information, please check wiki: Cookies
wh_all4you#hotmail.com
