use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.
the class RaftController method onPublish.
/**
* Commit publish datum.
*
* @param request http request
* @param response http response
* @return 'ok' if success
* @throws Exception exception
*/
@PostMapping("/datum/commit")
public String onPublish(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (versionJudgement.allMemberIsNewVersion()) {
throw new IllegalStateException("old raft protocol already stop");
}
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Encode", "gzip");
String entity = IoUtils.toString(request.getInputStream(), "UTF-8");
String value = URLDecoder.decode(entity, "UTF-8");
JsonNode jsonObject = JacksonUtils.toObj(value);
String key = "key";
RaftPeer source = JacksonUtils.toObj(jsonObject.get("source").toString(), RaftPeer.class);
JsonNode datumJson = jsonObject.get("datum");
Datum datum = null;
if (KeyBuilder.matchInstanceListKey(datumJson.get(key).asText())) {
datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Instances>>() {
});
} else if (KeyBuilder.matchSwitchKey(datumJson.get(key).asText())) {
datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<SwitchDomain>>() {
});
} else if (KeyBuilder.matchServiceMetaKey(datumJson.get(key).asText())) {
datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Service>>() {
});
}
raftConsistencyService.onPut(datum, source);
return "ok";
}
use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.
the class BaseTest method mockRaft.
protected void mockRaft() {
RaftPeer peer = new RaftPeer();
peer.ip = NetUtils.localServer();
raftCore.setPeerSet(peerSet);
Mockito.when(peerSet.local()).thenReturn(peer);
Mockito.when(peerSet.getLeader()).thenReturn(peer);
Mockito.when(peerSet.isLeader(NetUtils.localServer())).thenReturn(true);
}
use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.
the class RaftController method beat.
/**
* Beat api.
*
* @param request http request
* @param response http response
* @return peer information
* @throws Exception exception
*/
@PostMapping("/beat")
public JsonNode beat(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (versionJudgement.allMemberIsNewVersion()) {
throw new IllegalStateException("old raft protocol already stop");
}
String entity = new String(IoUtils.tryDecompress(request.getInputStream()), StandardCharsets.UTF_8);
String value = URLDecoder.decode(entity, "UTF-8");
value = URLDecoder.decode(value, "UTF-8");
JsonNode json = JacksonUtils.toObj(value);
RaftPeer peer = raftCore.receivedBeat(JacksonUtils.toObj(json.get("beat").asText()));
return JacksonUtils.transferToJsonNode(peer);
}
use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.
the class RaftController method getPeer.
/**
* Get peer information.
*
* @param request http request
* @param response http response
* @return peer information
*/
@GetMapping("/peer")
public JsonNode getPeer(HttpServletRequest request, HttpServletResponse response) {
if (versionJudgement.allMemberIsNewVersion()) {
throw new IllegalStateException("old raft protocol already stop");
}
List<RaftPeer> peers = raftCore.getPeers();
RaftPeer peer = null;
for (RaftPeer peer1 : peers) {
if (StringUtils.equals(peer1.ip, NetUtils.localServer())) {
peer = peer1;
}
}
if (peer == null) {
peer = new RaftPeer();
peer.ip = NetUtils.localServer();
}
return JacksonUtils.transferToJsonNode(peer);
}
use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.
the class RaftController method onDelete.
/**
* Commit delete datum.
*
* @param request http request
* @param response http response
* @return 'ok' if success
* @throws Exception exception
*/
@DeleteMapping("/datum/commit")
public String onDelete(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (versionJudgement.allMemberIsNewVersion()) {
throw new IllegalStateException("old raft protocol already stop");
}
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Encode", "gzip");
String entity = IoUtils.toString(request.getInputStream(), "UTF-8");
String value = URLDecoder.decode(entity, "UTF-8");
value = URLDecoder.decode(value, "UTF-8");
JsonNode jsonObject = JacksonUtils.toObj(value);
Datum datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), Datum.class);
RaftPeer source = JacksonUtils.toObj(jsonObject.get("source").toString(), RaftPeer.class);
raftConsistencyService.onRemove(datum, source);
return "ok";
}
Aggregations