Skip to content

Commit

Permalink
[ISSUE alibaba#5361] add a GrpcRequestFilter to check isUseGrpcFeatur…
Browse files Browse the repository at this point in the history
…es() when using GRPC protocol
  • Loading branch information
MajorHe1 committed Apr 20, 2021
1 parent 52825cb commit 4e766ce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.nacos.naming.remote.rpc.filter;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.core.remote.AbstractRequestFilter;
import com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GrpcRequestFilter extends AbstractRequestFilter {

@Autowired
private UpgradeJudgement upgradeJudgement;

@Override
protected Response filter(Request request, RequestMeta meta, Class handlerClazz) throws NacosException {
if (!upgradeJudgement.isUseGrpcFeatures()) {
Response response = getDefaultResponseInstance(handlerClazz);
response.setErrorInfo(NacosException.SERVER_ERROR, "Nacos cluster is running with 1.X mode, can't accept gRPC request temporarily. Please check the server status or close Double write to force open 2.0 mode. Detail https://nacos.io/en-us/docs/2.0.0-upgrading.html.");
return response;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import com.alibaba.nacos.core.remote.RequestHandler;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.core.v2.service.impl.EphemeralClientOperationServiceImpl;
import com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement;
import com.alibaba.nacos.naming.web.NamingResourceParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
Expand All @@ -39,9 +37,6 @@
@Component
public class InstanceRequestHandler extends RequestHandler<InstanceRequest, InstanceResponse> {

@Autowired
private UpgradeJudgement upgradeJudgement;

private final EphemeralClientOperationServiceImpl clientOperationService;

public InstanceRequestHandler(EphemeralClientOperationServiceImpl clientOperationService) {
Expand All @@ -51,10 +46,6 @@ public InstanceRequestHandler(EphemeralClientOperationServiceImpl clientOperatio
@Override
@Secured(action = ActionTypes.WRITE, parser = NamingResourceParser.class)
public InstanceResponse handle(InstanceRequest request, RequestMeta meta) throws NacosException {
if (!upgradeJudgement.isUseGrpcFeatures()) {
throw new NacosException(NacosException.SERVER_ERROR,
"Nacos cluster is running with 1.X mode, can't accept gRPC request temporarily. Please check the server status or close Double write to force open 2.0 mode. Detail https://nacos.io/en-us/docs/2.0.0-upgrading.html.");
}
Service service = Service
.newService(request.getNamespace(), request.getGroupName(), request.getServiceName(), true);
switch (request.getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.core.v2.service.impl.EphemeralClientOperationServiceImpl;
import com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.utils.ServiceUtil;
import com.alibaba.nacos.naming.web.NamingResourceParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
Expand All @@ -48,9 +46,6 @@
@Component
public class SubscribeServiceRequestHandler extends RequestHandler<SubscribeServiceRequest, SubscribeServiceResponse> {

@Autowired
private UpgradeJudgement upgradeJudgement;

private final ServiceStorage serviceStorage;

private final NamingMetadataManager metadataManager;
Expand All @@ -68,10 +63,6 @@ public SubscribeServiceRequestHandler(ServiceStorage serviceStorage,
@Override
@Secured(action = ActionTypes.READ, parser = NamingResourceParser.class)
public SubscribeServiceResponse handle(SubscribeServiceRequest request, RequestMeta meta) throws NacosException {
if (!upgradeJudgement.isUseGrpcFeatures()) {
throw new NacosException(NacosException.SERVER_ERROR,
"Nacos cluster is running with 1.X mode, can't accept gRPC request temporarily. Please check the server status or close Double write to force open 2.0 mode. Detail https://nacos.io/en-us/docs/2.0.0-upgrading.html.");
}
String namespaceId = request.getNamespace();
String serviceName = request.getServiceName();
String groupName = request.getGroupName();
Expand Down

0 comments on commit 4e766ce

Please sign in to comment.