所有文章
https://www.cnblogs.com/lay2017/p/12485081.html
正文
在前面的文章中,我們大體地閱讀了一下客戶端的代碼。本文作為seata-server部分的第一篇文章,將開始閱讀Server端的內容。
main方法作為Server的的啟動代碼的開始部分,所以我們將從main方法開始。
Server端的main方法主體邏輯
public static void main(String[] args) throws IOException { // 構造參數解析器 ParameterParser parameterParser = new ParameterParser(args); // 初始化指標管理 MetricsManager.get().init(); // 設置存儲模式系統變量 System.setProperty(ConfigurationKeys.STORE_MODE, parameterParser.getStoreMode()); // 構造並初始化一個netty的Server端 RpcServer rpcServer = new RpcServer(WORKING_THREADS); rpcServer.setListenPort(parameterParser.getPort()); UUIDGenerator.init(parameterParser.getServerNode()); SessionHolder.init(parameterParser.getStoreMode()); // 構造並初始化一個分布式事務Coordinator DefaultCoordinator coordinator = new DefaultCoordinator(rpcServer); coordinator.init(); // 設置為rpc處理器 rpcServer.setHandler(coordinator); // 注冊一個銷毀的勾子 ShutdownHook.getInstance().addDisposable(coordinator); // 設置全局IP地址 if (NetUtil.isValidIp(parameterParser.getHost(), false)) { XID.setIpAddress(parameterParser.getHost()); } else { XID.setIpAddress(NetUtil.getLocalIp()); } // 設置全局端口 XID.setPort(rpcServer.getListenPort()); try { // 初始化RPCServer rpcServer.init(); } catch (Throwable e) { // 異常退出JVM System.exit(-1); } // 正常退出JVM System.exit(0); }
seata-server先是初始化了一個指標管理器,該指標管理器采用SPI機制選擇實現。根據官方說法,不采用spring機制就是為了減少更多的依賴沖突的可能。
而后,采用netty做網絡服務構造RpcServer
再接着是關於分布式事務模型中非常重要的Coordinator協調者的構造,並設置為RpcServer的handler,接入網絡服務。
最后,執行RpcServer的init方法,整個main方法就結束了。