Add SPI for ClientBeatExtensionHandler (#6250)

This commit is contained in:
杨翊 SionYang 2021-07-02 17:17:24 +08:00 committed by GitHub
parent 4f0900b0aa
commit 5c375afe81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 18 deletions

View File

@ -22,6 +22,7 @@ import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.common.ActionTypes;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.core.Instance;
@ -31,6 +32,7 @@ import com.alibaba.nacos.naming.core.InstanceOperatorServiceImpl;
import com.alibaba.nacos.naming.core.InstancePatchObject;
import com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement;
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatExtensionHandler;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.SwitchEntry;
@ -56,6 +58,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -83,6 +86,10 @@ public class InstanceController {
@Autowired
private UpgradeJudgement upgradeJudgement;
public InstanceController() {
NacosServiceLoader.load(ClientBeatExtensionHandler.class);
}
/**
* Register new instance.
*
@ -379,9 +386,15 @@ public class InstanceController {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
NamingUtils.checkServiceNameFormat(serviceName);
Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}", clientBeat, serviceName);
int resultCode = getInstanceOperator().handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat);
Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat,
serviceName, namespaceId);
Collection<ClientBeatExtensionHandler> extensionHandlers = NacosServiceLoader
.newServiceInstances(ClientBeatExtensionHandler.class);
for (ClientBeatExtensionHandler each : extensionHandlers) {
each.configExtensionInfoFromRequest(request);
}
int resultCode = getInstanceOperator()
.handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, extensionHandlers);
result.put(CommonParams.CODE, resultCode);
result.put(SwitchEntry.CLIENT_BEAT_INTERVAL,
getInstanceOperator().getHeartBeatInterval(namespaceId, serviceName, ip, port, clusterName));

View File

@ -20,9 +20,11 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatExtensionHandler;
import com.alibaba.nacos.naming.pojo.InstanceOperationInfo;
import com.alibaba.nacos.naming.pojo.Subscriber;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -110,17 +112,18 @@ public interface InstanceOperator {
/**
* Handle beat request.
*
* @param namespaceId namespace
* @param serviceName grouped service name group@@service
* @param ip ip of instance
* @param port port of instance
* @param cluster cluster of instance
* @param clientBeat client beat info
* @param namespaceId namespace
* @param serviceName grouped service name group@@service
* @param ip ip of instance
* @param port port of instance
* @param cluster cluster of instance
* @param clientBeat client beat info
* @param extensionHandlers client beat extension handlers
* @return result code
* @throws NacosException nacos exception when service non-exist and client beat info is null
*/
int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster, RsInfo clientBeat)
throws NacosException;
int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster, RsInfo clientBeat,
Collection<ClientBeatExtensionHandler> extensionHandlers) throws NacosException;
/**
* Get heart beat interval for specified instance.
@ -147,9 +150,9 @@ public interface InstanceOperator {
/**
* Batch update metadata of instances.
*
* @param namespaceId namespace Id of instances
* @param namespaceId namespace Id of instances
* @param instanceOperationInfo instance operation info
* @param metadata updated metadata
* @param metadata updated metadata
* @return updated instance
* @throws NacosException nacos exception during update
*/
@ -159,9 +162,9 @@ public interface InstanceOperator {
/**
* Batch delete metadata of instances.
*
* @param namespaceId namespace Id of instances
* @param namespaceId namespace Id of instances
* @param instanceOperationInfo instance operation info
* @param metadata delete metadata
* @param metadata delete metadata
* @return updated instance
* @throws NacosException nacos exception during update
*/

View File

@ -41,6 +41,7 @@ import com.alibaba.nacos.naming.core.v2.service.ClientOperationService;
import com.alibaba.nacos.naming.core.v2.service.ClientOperationServiceProxy;
import com.alibaba.nacos.naming.healthcheck.HealthCheckReactor;
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatExtensionHandler;
import com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatProcessorV2;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
@ -50,6 +51,7 @@ import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.push.UdpPushService;
import com.alibaba.nacos.naming.utils.ServiceUtil;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@ -211,7 +213,7 @@ public class InstanceOperatorClientImpl implements InstanceOperator {
@Override
public int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster,
RsInfo clientBeat) throws NacosException {
RsInfo clientBeat, Collection<ClientBeatExtensionHandler> extensionHandlers) 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);
@ -228,6 +230,9 @@ public class InstanceOperatorClientImpl implements InstanceOperator {
instance.setServiceName(serviceName);
instance.setInstanceId(instance.getInstanceId());
instance.setEphemeral(clientBeat.isEphemeral());
for (ClientBeatExtensionHandler each : extensionHandlers) {
each.handleExtensionInfo(instance);
}
registerInstance(namespaceId, serviceName, instance);
client = (IpPortBasedClient) clientManager.getClient(clientId);
}

View File

@ -23,6 +23,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatExtensionHandler;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.pojo.InstanceOperationContext;
@ -274,7 +275,7 @@ public class InstanceOperatorServiceImpl implements InstanceOperator {
@Override
public int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster,
RsInfo clientBeat) throws NacosException {
RsInfo clientBeat, Collection<ClientBeatExtensionHandler> extensionHandlers) throws NacosException {
com.alibaba.nacos.naming.core.Instance instance = serviceManager
.getInstance(namespaceId, serviceName, cluster, ip, port);
@ -295,7 +296,9 @@ public class InstanceOperatorServiceImpl implements InstanceOperator {
instance.setServiceName(serviceName);
instance.setInstanceId(instance.getInstanceId());
instance.setEphemeral(clientBeat.isEphemeral());
for (ClientBeatExtensionHandler each : extensionHandlers) {
each.handleExtensionInfo(instance);
}
serviceManager.registerInstance(namespaceId, serviceName, instance);
}

View File

@ -0,0 +1,45 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.healthcheck.heartbeat;
import com.alibaba.nacos.api.naming.pojo.Instance;
import javax.servlet.http.HttpServletRequest;
/**
* Client beat extension handler.
*
* <p>An extension handler for client beat, which is to handle some specified beat request for 1.x client.
*
* @author xiweng.yy
*/
public interface ClientBeatExtensionHandler {
/**
* Config extension info from http request.
*
* @param request http request
*/
void configExtensionInfoFromRequest(HttpServletRequest request);
/**
* Do handle for instance.
*
* @param needHandleInstance instance needed to be handled.
*/
void handleExtensionInfo(Instance needHandleInstance);
}