use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.
the class InstanceControllerV2 method beat.
/**
* Create a beat for instance.
*
* @param namespaceId service namespaceId
* @param serviceName service serviceName
* @param ip instance ip
* @param clusterName service clusterName
* @param port instance port
* @param beat instance beat info
* @return detail information of instance
* @throws Exception any error during handle
*/
@CanDistro
@PutMapping("/beat")
@Secured(action = ActionTypes.WRITE)
public ObjectNode beat(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = StringUtils.EMPTY) String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String clusterName, @RequestParam(defaultValue = "0") Integer port, @RequestParam(defaultValue = StringUtils.EMPTY) String beat) throws Exception {
ObjectNode result = JacksonUtils.createEmptyJsonNode();
result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
RsInfo clientBeat = null;
if (StringUtils.isNotBlank(beat)) {
clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
}
if (clientBeat != null) {
if (StringUtils.isNotBlank(clientBeat.getCluster())) {
clusterName = clientBeat.getCluster();
} else {
// fix #2533
clientBeat.setCluster(clusterName);
}
ip = clientBeat.getIp();
port = clientBeat.getPort();
}
NamingUtils.checkServiceNameFormat(serviceName);
Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat, serviceName, namespaceId);
BeatInfoInstanceBuilder builder = BeatInfoInstanceBuilder.newBuilder();
int resultCode = instanceServiceV2.handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, builder);
result.put(CommonParams.CODE, resultCode);
result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, instanceServiceV2.getHeartBeatInterval(namespaceId, serviceName, ip, port, clusterName));
result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled());
return result;
}
use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.
the class InstanceOperatorServiceImpl method handleBeat.
@Override
public int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster, RsInfo clientBeat, BeatInfoInstanceBuilder builder) throws NacosException {
com.alibaba.nacos.naming.core.Instance instance = serviceManager.getInstance(namespaceId, serviceName, cluster, ip, port);
if (instance == null) {
if (clientBeat == null) {
return NamingResponseCode.RESOURCE_NOT_FOUND;
}
Loggers.SRV_LOG.warn("[CLIENT-BEAT] The instance has been removed for health mechanism, " + "perform data compensation operations, beat: {}, serviceName: {}", clientBeat, serviceName);
instance = parseInstance(builder.setBeatInfo(clientBeat).setServiceName(serviceName).build());
serviceManager.registerInstance(namespaceId, serviceName, instance);
}
Service service = serviceManager.getService(namespaceId, serviceName);
serviceManager.checkServiceIsNull(service, namespaceId, serviceName);
if (clientBeat == null) {
clientBeat = new RsInfo();
clientBeat.setIp(ip);
clientBeat.setPort(port);
clientBeat.setCluster(cluster);
}
service.processClientBeat(clientBeat);
return NamingResponseCode.OK;
}
use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.
the class InstanceOperatorClientImpl method handleBeat.
@Override
public int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster, RsInfo clientBeat, BeatInfoInstanceBuilder builder) throws NacosException {
Service service = getService(namespaceId, serviceName, true);
String clientId = IpPortBasedClient.getClientId(ip + InternetAddressUtil.IP_PORT_SPLITER + port, true);
IpPortBasedClient client = (IpPortBasedClient) clientManager.getClient(clientId);
if (null == client || !client.getAllPublishedService().contains(service)) {
if (null == clientBeat) {
return NamingResponseCode.RESOURCE_NOT_FOUND;
}
Instance instance = builder.setBeatInfo(clientBeat).setServiceName(serviceName).build();
registerInstance(namespaceId, serviceName, instance);
client = (IpPortBasedClient) clientManager.getClient(clientId);
}
if (!ServiceManager.getInstance().containSingleton(service)) {
throw new NacosException(NacosException.SERVER_ERROR, "service not found: " + serviceName + "@" + namespaceId);
}
if (null == clientBeat) {
clientBeat = new RsInfo();
clientBeat.setIp(ip);
clientBeat.setPort(port);
clientBeat.setCluster(cluster);
clientBeat.setServiceName(serviceName);
}
ClientBeatProcessorV2 beatProcessor = new ClientBeatProcessorV2(namespaceId, clientBeat, client);
HealthCheckReactor.scheduleNow(beatProcessor);
client.setLastUpdatedTime();
return NamingResponseCode.OK;
}
use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.
the class DistroIpPortTagGenerator method getResponsibleTag.
@Override
public String getResponsibleTag(ReuseHttpServletRequest request) {
String ip = request.getParameter(PARAMETER_IP);
String port = request.getParameter(PARAMETER_PORT);
if (StringUtils.isBlank(ip)) {
// some old version clients using beat parameter
String beatStr = request.getParameter(PARAMETER_BEAT);
if (StringUtils.isNotBlank(beatStr)) {
try {
RsInfo rsInfo = JacksonUtils.toObj(beatStr, RsInfo.class);
ip = rsInfo.getIp();
port = String.valueOf(rsInfo.getPort());
} catch (NacosDeserializationException ignored) {
}
}
}
if (StringUtils.isNotBlank(ip)) {
ip = ip.trim();
}
port = StringUtils.isBlank(port) ? "0" : port.trim();
return ip + InternetAddressUtil.IP_PORT_SPLITER + port;
}
use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.
the class BeatInfoInstanceBuilderTest method setUp.
@Before
public void setUp() throws Exception {
builder = BeatInfoInstanceBuilder.newBuilder();
builder.setRequest(request);
beatInfo = new RsInfo();
beatInfo.setServiceName("g@@s");
beatInfo.setCluster("c");
beatInfo.setIp("1.1.1.1");
beatInfo.setPort(8848);
beatInfo.setWeight(10);
beatInfo.setMetadata(new HashMap<>());
}
Aggregations