Skip to content

Commit

Permalink
[ISSUE #5361] check isUseGrpcFeatures() when subscribe service using …
Browse files Browse the repository at this point in the history
…GRPC (#5411)

* [ISSUE #5361] check isUseGrpcFeatures() when subscribe service using GRPC

* [ISSUE #5361] add a GrpcRequestFilter to check isUseGrpcFeatures() when using GRPC protocol

* [ISSUE #5361] add a judgement whether request instance of AbstractNamingRequest

* [ISSUE #5361] reformat code using nacos-code-style-for-idea.xml
  • Loading branch information
MajorHe1 authored Apr 22, 2021
1 parent a95236a commit 0a4f7f5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.remote.rpc.filter;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest;
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;

/**
* Grpc request filter.
*
* @author majorhe
*/
@Component
public class GrpcRequestFilter extends AbstractRequestFilter {

@Autowired
private UpgradeJudgement upgradeJudgement;

@Override
protected Response filter(Request request, RequestMeta meta, Class handlerClazz) throws NacosException {
if (request instanceof AbstractNamingRequest && !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

0 comments on commit 0a4f7f5

Please sign in to comment.