通过此介绍:http://www.oschina.net/p/cat-dianping
得知,运行cat-home项目里的‘com.dianping.cat.TestServer’可以启动CAT服务。so,我们就通过这个类来分析一下cat服务端的启动流程。
准备工作:需要把cat的代码导入IDE(我这里用的是eclipse),下载依赖(吐槽一下,太慢了),编译成功;
好,接着我们断点TestServer类跟踪整个启动流程。
----------------------------------------------分割线------------------------------------------------------
直接上TsetServer代码:
- /**
- * Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,
- * 而且写test case 的类不需要继承TestCase,只需要在所要做test case的方法前加@Test 注解即可。
- * @author admin
- */
- @RunWith(JUnit4.class)//测试运行于JUnit4测试环境
- public class TestServer extends JettyServer {
- public static void main(String[] args) throws Exception {
- TestServer server = new TestServer();
- System.setProperty("devMode", "true");//设置指定键对值的系统属性:开发模式(true)
- server.startServer();//开启一个jetty服务
- server.startWebApp();//在默认浏览器中打开一个页面
- server.stopServer();//停止一个jetty服务
- }
- /**
- * @Before, @After注解过的方法将在测试方法之前/之后执行。
- * @throws Exception
- */
- @Before
- public void before() throws Exception {
- System.setProperty("devMode", "true");
- super.startServer();
- }
- @Override
- protected String getContextPath() {
- return "/cat";
- }
- /**
- * 得到服务器端口
- */
- @Override
- protected int getServerPort() {
- return 2281;
- }
- @Override
- protected void postConfigure(WebAppContext context) {
- context.addFilter(GzipFilter.class, "/*", Handler.ALL);
- }
- @Test
- public void startWebApp() throws Exception {
- // open the page in the default browser
- // 在系统默认浏览器中打开一个页面,这个地方要注意一下,因为调用的是默认浏览器,而cat的对于别的浏览器页面不兼容,建议把默认浏览器设置为chrome
- display("/cat/r");
- waitForAnyKey();
- }
- }
从上面的代码可以看出重点在main方法,从中基本可以看出整个服务器的启动过程如下:
1,因为cat项目在开发模式下集成了jetty,所以第一步就是启动一个jetty服务(server.startServer();//开启一个jetty服务);
2,调用本地操作系统的默认浏览器打开一个页面(server.startWebApp();//在默认浏览器中打开一个页面);
3,停止服务(server.stopServer();//停止一个jetty服务);
所以,cat服务的启动过程可以通过以上三个过程步骤进行分析,接下来我们看看cat在startServer的时候都干了一些什么,而startServer整个过程又分为如下步骤:
JettyServer类
此类的代码点评封装在了一个jar里面(test-framework-2.2.0.jar)
1,安装Plexus IOC容器setupContainer()(没有接触过plexus ioc的请查看:http://blog.csdn.net/songhuiqiao/article/details/49908165)
Plexus提供完整的软件栈,用于