|
@ -2,11 +2,8 @@ package com.yihu.hos.arbiter.services;
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.mongodb.WriteResult;
|
|
|
import com.yihu.hos.web.framework.model.bo.BrokerServer;
|
|
|
import com.yihu.hos.core.http.HTTPResponse;
|
|
|
import com.yihu.hos.core.http.HttpClientKit;
|
|
|
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
|
|
|
import com.yihu.hos.web.framework.model.bo.BrokerServer;
|
|
|
import com.yihu.hos.web.framework.model.bo.ServiceFlow;
|
|
|
import org.apache.camel.CamelContext;
|
|
|
import org.apache.camel.ProducerTemplate;
|
|
@ -40,53 +37,6 @@ public class BrokerServerService {
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
public void save(BrokerServer brokerServer) {
|
|
|
Query query = new Query();
|
|
|
query.addCriteria(Criteria.where("hostName").is(brokerServer.getHostName()));
|
|
|
query.addCriteria(Criteria.where("hostAddress").is(brokerServer.getHostAddress()));
|
|
|
query.addCriteria(Criteria.where("port").is(brokerServer.getPort()));
|
|
|
|
|
|
Update update = new Update();
|
|
|
update.set("tenant", brokerServer.getTenant());
|
|
|
update.set("hostName", brokerServer.getHostName());
|
|
|
update.set("hostAddress", brokerServer.getHostAddress());
|
|
|
update.set("port", brokerServer.getPort());
|
|
|
Date updateTime = brokerServer.getUpdateTime() == null ? new Date(): brokerServer.getUpdateTime();
|
|
|
update.set("updateTime", updateTime);
|
|
|
update.set("enable", brokerServer.isEnable());
|
|
|
if (brokerServer.getServiceFlows()!=null) {
|
|
|
for (BrokerServer.ServiceFlow serviceFlow : brokerServer.getServiceFlows()) {
|
|
|
update.addToSet("serviceFlows", serviceFlow);
|
|
|
}
|
|
|
}
|
|
|
WriteResult writeResult = mongoOperations.upsert(query, update, BrokerServer.class);
|
|
|
if (writeResult.isUpdateOfExisting()) {
|
|
|
//避免Broker重启的情况
|
|
|
HTTPResponse response = HttpClientKit.get(brokerServer.getURL() + "/esb/heartbeat");
|
|
|
if (response.getStatusCode() == 200 && brokerServer.isRegistered()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//确保Broker已经启动了流程
|
|
|
brokerServer = mongoOperations.findOne(query, BrokerServer.class);
|
|
|
if (brokerServer.getServiceFlows() != null) {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//没有启动流程
|
|
|
try {
|
|
|
String msg = objectMapper.writeValueAsString(brokerServer);
|
|
|
ProducerTemplate producerTemplate = createProducerTemplate();
|
|
|
Map<String, Object> header = new HashMap<>();
|
|
|
header.put("event", ServiceFlowConstant.BROKER_SERVER_ON);
|
|
|
producerTemplate.sendBodyAndHeaders(ServiceFlowConstant.CAMEL_ENDPOINT, msg, header);
|
|
|
} catch (JsonProcessingException e) {
|
|
|
e.printStackTrace();
|
|
|
logger.error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 暂不提供动态均衡算法,只是随机返回一个。
|
|
|
*
|
|
@ -99,10 +49,6 @@ public class BrokerServerService {
|
|
|
return mongoOperations.findOne(query, BrokerServer.class);
|
|
|
}
|
|
|
|
|
|
public void delete(BrokerServer brokerServer) {
|
|
|
mongoOperations.remove(brokerServer);
|
|
|
}
|
|
|
|
|
|
public List<BrokerServer> get(boolean one) {
|
|
|
if (one) {
|
|
|
BrokerServer brokerServer = get();
|
|
@ -124,10 +70,6 @@ public class BrokerServerService {
|
|
|
return mongoOperations.find(query, BrokerServer.class);
|
|
|
}
|
|
|
|
|
|
public void flowStarted(String msg) {
|
|
|
|
|
|
}
|
|
|
|
|
|
public void addServiceFlow(BrokerServer brokerServer, ServiceFlow serviceFlow) {
|
|
|
BrokerServer.ServiceFlow flow = new BrokerServer.ServiceFlow();
|
|
|
flow.setFlowId(serviceFlow.getId());
|
|
@ -167,6 +109,59 @@ public class BrokerServerService {
|
|
|
mongoOperations.upsert(query, update, BrokerServer.class);
|
|
|
}
|
|
|
|
|
|
public void login(BrokerServer brokerServer) {
|
|
|
Query query = new Query();
|
|
|
query.addCriteria(Criteria.where("hostName").is(brokerServer.getHostName()));
|
|
|
query.addCriteria(Criteria.where("hostAddress").is(brokerServer.getHostAddress()));
|
|
|
query.addCriteria(Criteria.where("port").is(brokerServer.getPort()));
|
|
|
|
|
|
Update update = new Update();
|
|
|
update.set("tenant", brokerServer.getTenant());
|
|
|
update.set("hostName", brokerServer.getHostName());
|
|
|
update.set("hostAddress", brokerServer.getHostAddress());
|
|
|
update.set("port", brokerServer.getPort());
|
|
|
Date updateTime = brokerServer.getUpdateTime() == null ? new Date() : brokerServer.getUpdateTime();
|
|
|
update.set("updateTime", updateTime);
|
|
|
update.set("enable", brokerServer.isEnable());
|
|
|
|
|
|
mongoOperations.upsert(query, update, BrokerServer.class);
|
|
|
|
|
|
//没有启动流程
|
|
|
try {
|
|
|
String msg = objectMapper.writeValueAsString(brokerServer);
|
|
|
ProducerTemplate producerTemplate = createProducerTemplate();
|
|
|
Map<String, Object> header = new HashMap<>();
|
|
|
header.put("event", ServiceFlowConstant.BROKER_SERVER_ON);
|
|
|
producerTemplate.sendBodyAndHeaders(ServiceFlowConstant.CAMEL_ENDPOINT, msg, header);
|
|
|
} catch (JsonProcessingException e) {
|
|
|
e.printStackTrace();
|
|
|
logger.error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void logout(BrokerServer brokerServer) {
|
|
|
Query query = new Query();
|
|
|
query.addCriteria(Criteria.where("hostName").is(brokerServer.getHostName()));
|
|
|
query.addCriteria(Criteria.where("hostAddress").is(brokerServer.getHostAddress()));
|
|
|
query.addCriteria(Criteria.where("port").is(brokerServer.getPort()));
|
|
|
|
|
|
BrokerServer server = mongoOperations.findOne(query, BrokerServer.class);
|
|
|
mongoOperations.remove(server);
|
|
|
}
|
|
|
|
|
|
public void heartbeat(BrokerServer brokerServer) {
|
|
|
Query query = new Query();
|
|
|
query.addCriteria(Criteria.where("hostName").is(brokerServer.getHostName()));
|
|
|
query.addCriteria(Criteria.where("hostAddress").is(brokerServer.getHostAddress()));
|
|
|
query.addCriteria(Criteria.where("port").is(brokerServer.getPort()));
|
|
|
|
|
|
BrokerServer server = mongoOperations.findOne(query, BrokerServer.class);
|
|
|
if (server != null) {
|
|
|
server.setUpdateTime(new Date());
|
|
|
mongoOperations.save(server);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private ProducerTemplate createProducerTemplate() {
|
|
|
if (producerTemplate == null) {
|
|
|
producerTemplate = camelContext.createProducerTemplate();
|