ServiceFlowService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package com.yihu.hos.arbiter.services;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.hos.core.http.HTTPResponse;
  4. import com.yihu.hos.core.http.HttpClientKit;
  5. import com.yihu.hos.core.log.Logger;
  6. import com.yihu.hos.core.log.LoggerFactory;
  7. import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
  8. import com.yihu.hos.web.framework.model.bo.BrokerServer;
  9. import com.yihu.hos.web.framework.model.bo.ServiceFlow;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.data.mongodb.core.MongoOperations;
  12. import org.springframework.data.mongodb.core.query.Criteria;
  13. import org.springframework.data.mongodb.core.query.Query;
  14. import org.springframework.data.mongodb.core.query.Update;
  15. import org.springframework.stereotype.Service;
  16. import java.io.IOException;
  17. import java.util.List;
  18. /**
  19. * Broker原则上具有等同性,这样Arbiter无论选择了哪个Broker能提供的服务都是一样的。
  20. * 但是因为Broker上还是会运行一些定时的采集任务,这些采集任务如果是多台Broker运行的话,可能会引起数据重复问题。
  21. * 所以在事件触发时需要做一些策略的调整:
  22. * 1.实时任务,通知所有的Broker进行更新路由
  23. * 2.采集任务,只通知其中的一台进行更新路由
  24. *
  25. * @created Airhead 2016/8/16.
  26. */
  27. @Service("serviceFlowService")
  28. public class ServiceFlowService {
  29. private static final Logger logger = LoggerFactory.getLogger(BrokerServerService.class);
  30. @Autowired
  31. private MongoOperations mongoOperations;
  32. @Autowired
  33. private ObjectMapper objectMapper;
  34. @Autowired
  35. private BrokerServerService brokerServerService;
  36. public ServiceFlow save(ServiceFlow serviceFlow) {
  37. if (serviceFlow == null) {
  38. logger.error("ServiceFlow is null");
  39. return null;
  40. }
  41. Query query = new Query();
  42. query.addCriteria(Criteria.where("routeCode").is(serviceFlow.getRouteCode()));
  43. Update update = new Update();
  44. update.set("routeCode", serviceFlow.getRouteCode());
  45. update.set("updated", serviceFlow.getUpdated());
  46. update.set("flowType", serviceFlow.getFlowType());
  47. update.set("tenant", serviceFlow.getTenant());
  48. //删除旧记录
  49. update.set("handleFiles", serviceFlow.getHandleFiles());
  50. // update1.set("handleFiles.$.className", handleFile.getClassName());
  51. // Query query1 = Query.query(new Criteria().andOperator(Criteria.where("className").is( handleFile.getClassName()),Criteria.where("handleFiles").elemMatch(Criteria.where("className").is( handleFile.getClassName()))));
  52. // mongoOperations.updateFirst(query1, update1, ServiceFlow.class);
  53. mongoOperations.upsert(query, update, ServiceFlow.class);
  54. return mongoOperations.findOne(query, ServiceFlow.class);
  55. }
  56. public String get(String serviceName) {
  57. return null;
  58. }
  59. public void delete(ServiceFlow serviceFlow) {
  60. mongoOperations.remove(serviceFlow);
  61. }
  62. public List<ServiceFlow> getAll() {
  63. return mongoOperations.findAll(ServiceFlow.class);
  64. }
  65. /**
  66. * admin发过来的服务流程启动事件处理。
  67. *
  68. * @param msg serviceFlow
  69. */
  70. public void serviceFlowStarted(String msg) {
  71. try {
  72. ServiceFlow serviceFlow = getServiceFlow(msg);
  73. serviceFlow = this.save(serviceFlow);
  74. boolean one = ServiceFlowConstant.JAVA.equals(serviceFlow.getFlowType());//java类型为采集任务
  75. if (one && isStarted(serviceFlow)) {
  76. return;
  77. }
  78. List<BrokerServer> brokerServerList;
  79. brokerServerList = brokerServerService.get(one);
  80. for (BrokerServer broker : brokerServerList) {
  81. boolean result = sendMessage(broker, "post", "/esb/serviceFlow/serverServiceFlow", msg);
  82. if (!result) {
  83. logger.error("sendMessage to broker start failed, broker:" + broker.getURL() + ", msg:" + msg);
  84. continue;
  85. }
  86. brokerServerService.addServiceFlow(broker, serviceFlow);
  87. }
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. logger.error(e.getMessage());
  91. }
  92. }
  93. public void serviceFlowStopped(String msg) {
  94. try {
  95. ServiceFlow serviceFlow = getServiceFlow(msg);
  96. this.delete(serviceFlow);
  97. List<BrokerServer> brokerServerList;
  98. brokerServerList = brokerServerService.get(false);
  99. for (BrokerServer broker : brokerServerList) {
  100. HTTPResponse response = HttpClientKit.post(broker.getURL() + "", msg);
  101. if (response.getStatusCode() == 200) {
  102. String body = response.getBody();
  103. logger.debug(body);
  104. }
  105. boolean result = sendMessage(broker, "post", "/esb/serviceFlow/stop", msg);
  106. if (!result) {
  107. logger.error("sendMessage to broker start failed, broker:" + broker.getURL() + ", msg:" + msg);
  108. continue;
  109. }
  110. brokerServerService.removeServiceFlow(broker, serviceFlow);
  111. }
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. logger.error(e.getMessage());
  115. }
  116. }
  117. public void serviceFlowAdd(String msg) {
  118. try {
  119. ServiceFlow serviceFlow = getServiceFlow(msg);
  120. serviceFlow = this.save(serviceFlow);
  121. System.out.println("流程添加serviceFlowAdd开始!");
  122. boolean one = ServiceFlowConstant.JAVA.equals(serviceFlow.getFlowType());//java类型为采集任务
  123. if (one) {
  124. BrokerServer brokerServer = brokerServerService.get();
  125. boolean result = sendMessage(brokerServer, "post", "/esb/serviceFlow", msg);
  126. if (!result) {
  127. logger.error("sendMessage to broker start failed, broker:" + brokerServer.getURL() + ", msg:" + msg);
  128. return;
  129. }
  130. brokerServerService.addServiceFlow(brokerServer, serviceFlow);
  131. return;
  132. }
  133. List<BrokerServer> brokerServerList;
  134. brokerServerList = brokerServerService.get(one);
  135. for (BrokerServer broker : brokerServerList) {
  136. boolean result = sendMessage(broker, "post", "/esb/serviceFlow", msg);
  137. if (!result) {
  138. logger.error("sendMessage to broker start failed, broker:" + broker.getURL() + ", msg:" + msg);
  139. continue;
  140. }
  141. brokerServerService.addServiceFlow(broker, serviceFlow);
  142. }
  143. } catch (IOException e) {
  144. e.printStackTrace();
  145. logger.error(e.getMessage());
  146. }
  147. }
  148. public void serviceFlowModifyAdd(String msg) {
  149. try {
  150. System.out.println("arbiter'route serviceFlowModifyAdd ================ 1 ");
  151. ServiceFlow serviceFlow = getServiceFlow(msg);
  152. serviceFlow = this.save(serviceFlow);
  153. boolean one = ServiceFlowConstant.JAVA.equals(serviceFlow.getFlowType());//java类型为采集任务
  154. if (one) {
  155. System.out.println("arbiter'route is java ================ 2, cdoe: " + serviceFlow.getRouteCode());
  156. List<BrokerServer> brokerList = brokerServerService.getBrokerList(serviceFlow.getRouteCode());
  157. if (brokerList == null || brokerList.size() == 0) {
  158. logger.error("service flow stopped unexpected.");
  159. return;
  160. }
  161. boolean result = sendMessage(brokerList.get(0), "post", "/esb/serviceFlow/add", msg);
  162. if (!result) {
  163. logger.error("serviceFlowModifyAdd11 sendMessage to broker start failed, broker:" + brokerList.get(0).getURL() + ", msg:" + msg);
  164. return;
  165. }
  166. brokerServerService.addServiceFlow(brokerList.get(0), serviceFlow);
  167. return;
  168. }
  169. List<BrokerServer> brokerServerList;
  170. brokerServerList = brokerServerService.get(one);
  171. for (BrokerServer broker : brokerServerList) {
  172. boolean result = sendMessage(broker, "post", "/esb/serviceFlow/add", msg);
  173. if (!result) {
  174. logger.error("serviceFlowModifyAdd22 sendMessage to broker start failed, broker:" + broker.getURL() + ", msg:" + msg);
  175. continue;
  176. } else {
  177. System.out.println("arbiter'route susscess ================ 3");
  178. }
  179. brokerServerService.addServiceFlow(broker, serviceFlow);
  180. }
  181. } catch (IOException e) {
  182. e.printStackTrace();
  183. logger.error(e.getMessage());
  184. }
  185. }
  186. public void serviceFlowModifyReduce(String msg) {
  187. try {
  188. ServiceFlow serviceFlow = getServiceFlow(msg);
  189. serviceFlow = this.save(serviceFlow);
  190. boolean one = ServiceFlowConstant.JAVA.equals(serviceFlow.getFlowType());//java类型为采集任务
  191. if (one) {
  192. List<BrokerServer> brokerList = brokerServerService.getBrokerList(serviceFlow.getRouteCode());
  193. if (brokerList == null || brokerList.size() == 0) {
  194. logger.error("service flow stopped unexpected.");
  195. return;
  196. }
  197. boolean result = sendMessage(brokerList.get(0), "put", "/esb/serviceFlow/reduce", msg);
  198. if (!result) {
  199. logger.error("sendMessage to broker start failed, broker:" + brokerList.get(0).getURL() + ", msg:" + msg);
  200. return;
  201. }
  202. brokerServerService.addServiceFlow(brokerList.get(0), serviceFlow);
  203. return;
  204. }
  205. List<BrokerServer> brokerServerList;
  206. brokerServerList = brokerServerService.get(one);
  207. for (BrokerServer broker : brokerServerList) {
  208. boolean result = sendMessage(broker, "put", "/esb/serviceFlow/reduce", msg);
  209. if (!result) {
  210. logger.error("sendMessage to broker start failed, broker:" + broker.getURL() + ", msg:" + msg);
  211. continue;
  212. }
  213. brokerServerService.addServiceFlow(broker, serviceFlow);
  214. }
  215. } catch (IOException e) {
  216. e.printStackTrace();
  217. logger.error(e.getMessage());
  218. }
  219. }
  220. public void serviceFlowDelete(String msg) {
  221. try {
  222. ServiceFlow serviceFlow = getServiceFlow(msg);
  223. this.delete(serviceFlow);
  224. boolean one = ServiceFlowConstant.JAVA.equals(serviceFlow.getFlowType());//java类型为采集任务
  225. if (one) {
  226. List<BrokerServer> brokerList = brokerServerService.getBrokerList(serviceFlow.getRouteCode());
  227. if (brokerList == null || brokerList.size() == 0) {
  228. logger.error("service flow stopped unexpected.");
  229. return;
  230. }
  231. boolean result = sendMessage(brokerList.get(0), "delete", "/esb/serviceFlow", msg);
  232. if (!result) {
  233. logger.error("sendMessage to broker start failed, broker:" + brokerList.get(0).getURL() + ", msg:" + msg);
  234. return;
  235. }
  236. brokerServerService.removeServiceFlow(brokerList.get(0), serviceFlow);
  237. return;
  238. }
  239. List<BrokerServer> brokerServerList;
  240. brokerServerList = brokerServerService.get(one);
  241. for (BrokerServer broker : brokerServerList) {
  242. boolean result = sendMessage(broker, "delete", "/esb/serviceFlow", msg);
  243. if (!result) {
  244. logger.error("sendMessage to broker start failed, broker:" + broker.getURL() + ", msg:" + msg);
  245. continue;
  246. }
  247. brokerServerService.removeServiceFlow(broker, serviceFlow);
  248. }
  249. } catch (IOException e) {
  250. e.printStackTrace();
  251. logger.error(e.getMessage());
  252. }
  253. }
  254. public void brokerServerOn(String msg) {
  255. List<ServiceFlow> serviceFlowList = getAll();
  256. serviceFlowList.forEach(serviceFlow -> {
  257. try {
  258. serviceFlow = this.save(serviceFlow);
  259. boolean one = ServiceFlowConstant.JAVA.equals(serviceFlow.getFlowType());//java类型为采集任务
  260. if (one && isStarted(serviceFlow)) {
  261. return;
  262. }
  263. BrokerServer brokerServer = objectMapper.readValue(msg, BrokerServer.class);
  264. String serviceFlowMsg = objectMapper.writeValueAsString(serviceFlow);
  265. boolean result = sendMessage(brokerServer, "post", "/esb/serviceFlow/start", serviceFlowMsg);
  266. if (!result) {
  267. logger.error("sendMessage to broker start failed, broker:" + brokerServer.getURL() + ", msg:" + serviceFlowMsg);
  268. return;
  269. }
  270. brokerServerService.addServiceFlow(brokerServer, serviceFlow);
  271. } catch (IOException e) {
  272. e.printStackTrace();
  273. logger.error(e.getMessage());
  274. }
  275. });
  276. }
  277. public void brokerServerOff(String msg) {
  278. //可以不用处理。
  279. }
  280. private boolean sendMessage(BrokerServer brokerServer, String method, String path, String msg) {
  281. if (brokerServer == null) {
  282. return false;
  283. }
  284. switch (method) {
  285. case "post": {
  286. HTTPResponse response = HttpClientKit.post(brokerServer.getURL() + path, msg);
  287. if (response.getStatusCode() == 200) {
  288. String body = response.getBody();
  289. logger.debug(body);
  290. return true;
  291. }
  292. logger.error("post error,url: " + brokerServer.getURL() + path);
  293. return false;
  294. }
  295. case "put": {
  296. HTTPResponse response = HttpClientKit.put(brokerServer.getURL() + path, msg);
  297. if (response.getStatusCode() == 200) {
  298. String body = response.getBody();
  299. logger.debug(body);
  300. return true;
  301. }
  302. logger.error("put error,url: " + brokerServer.getURL() + path);
  303. return false;
  304. }
  305. case "delete": {
  306. HTTPResponse response = HttpClientKit.delete(brokerServer.getURL() + path, msg);
  307. if (response.getStatusCode() == 200) {
  308. String body = response.getBody();
  309. logger.debug(body);
  310. return true;
  311. }
  312. logger.error("delete error,url: " + brokerServer.getURL() + path);
  313. return false;
  314. }
  315. default:
  316. break;
  317. }
  318. return false;
  319. }
  320. private ServiceFlow getServiceFlow(String msg) throws IOException {
  321. return objectMapper.readValue(msg, ServiceFlow.class);
  322. }
  323. private boolean isStarted(ServiceFlow serviceFlow) {
  324. List<BrokerServer> brokerList = brokerServerService.getBrokerList(serviceFlow.getRouteCode());
  325. if (brokerList != null && brokerList.size() != 0) {
  326. logger.debug("service flow is already started on the broker");
  327. return true;
  328. }
  329. return false;
  330. }
  331. }