response model optimize (#3357)

* Add gprc support-> add grpc log to distribution logback

* Add gprc support-> response model optimize
This commit is contained in:
nov.lzf 2020-07-17 12:03:30 +08:00 committed by GitHub
parent b10a24885e
commit 3f06a7a0f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 213 additions and 98 deletions

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.api.config.remote.request;
/**
* ConfigChangeListenRequest.
* @author liuzunfei
* @version $Id: ConfigChangeListenRequest.java, v 0.1 2020年07月13日 9:01 PM liuzunfei Exp $
*/

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.api.config.remote.response;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
/**
* ConfigChangeListenResponse.
@ -26,13 +27,37 @@ import com.alibaba.nacos.api.remote.response.Response;
*/
public class ConfigChangeListenResponse extends Response {
public ConfigChangeListenResponse() {
super();
}
public ConfigChangeListenResponse(int resultCode, String message) {
super(ConfigResponseTypeConstants.CONFIG_CHANGE, resultCode, message);
@Override
public String getType() {
return ConfigResponseTypeConstants.CONFIG_CHANGE;
}
/**
* build sucess response.
*
* @return
*/
public static ConfigChangeListenResponse buildSucessResponse() {
ConfigChangeListenResponse response = new ConfigChangeListenResponse();
response.setResultCode(ResponseCode.SUCCESS.getCode());
return response;
}
/**
* build fail response.
*
* @param errorMessage errorMessage.
* @return
*/
public static ConfigChangeListenResponse buildFailResponse(String errorMessage) {
ConfigChangeListenResponse response = new ConfigChangeListenResponse();
response.setResultCode(ResponseCode.FAIL.getCode());
response.setMessage(errorMessage);
return response;
}
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.api.config.remote.response;
import com.alibaba.nacos.api.remote.response.Response;
@ -25,17 +26,18 @@ import com.alibaba.nacos.api.remote.response.ResponseCode;
public class ConfigChangeNotifyResponse extends Response {
private String dataId;
private String group;
private String tenant;
public ConfigChangeNotifyResponse() {
}
public ConfigChangeNotifyResponse(int resultCode, String message) {
super(ConfigResponseTypeConstants.CONFIG_CHANGE_NOTIFY, resultCode, message);
@Override
public String getType() {
return ConfigResponseTypeConstants.CONFIG_CHANGE_NOTIFY;
}
/**
@ -47,8 +49,7 @@ public class ConfigChangeNotifyResponse extends Response {
* @return ConfigChangeNotifyResponse
*/
public static ConfigChangeNotifyResponse buildSuccessResponse(String dataId, String group, String tenant) {
ConfigChangeNotifyResponse response = new ConfigChangeNotifyResponse(ResponseCode.SUCCESS.getCode(),
"data changed");
ConfigChangeNotifyResponse response = new ConfigChangeNotifyResponse();
response.setDataId(dataId);
response.setGroup(group);
response.setTenant(tenant);
@ -63,7 +64,7 @@ public class ConfigChangeNotifyResponse extends Response {
public String getDataId() {
return dataId;
}
/**
* Setter method for property <tt>dataId</tt>.
*
@ -72,7 +73,7 @@ public class ConfigChangeNotifyResponse extends Response {
public void setDataId(String dataId) {
this.dataId = dataId;
}
/**
* Getter method for property <tt>group</tt>.
*
@ -81,7 +82,7 @@ public class ConfigChangeNotifyResponse extends Response {
public String getGroup() {
return group;
}
/**
* Setter method for property <tt>group</tt>.
*
@ -90,7 +91,7 @@ public class ConfigChangeNotifyResponse extends Response {
public void setGroup(String group) {
this.group = group;
}
/**
* Getter method for property <tt>tenant</tt>.
*
@ -99,7 +100,7 @@ public class ConfigChangeNotifyResponse extends Response {
public String getTenant() {
return tenant;
}
/**
* Setter method for property <tt>tenant</tt>.
*

View File

@ -31,8 +31,9 @@ public class ConfigPubishResponse extends Response {
super();
}
public ConfigPubishResponse(int resultCode, String message) {
super(ConfigResponseTypeConstants.CONFIG_PUBLISH, resultCode, message);
@Override
public String getType() {
return ConfigResponseTypeConstants.CONFIG_PUBLISH;
}
/**
@ -41,7 +42,7 @@ public class ConfigPubishResponse extends Response {
* @return
*/
public static ConfigPubishResponse buildSuccessResponse() {
return new ConfigPubishResponse(ResponseCode.SUCCESS.getCode(), "");
return new ConfigPubishResponse();
}
/**
@ -50,6 +51,9 @@ public class ConfigPubishResponse extends Response {
* @return
*/
public static ConfigPubishResponse buildFailResponse(String errorMsg) {
return new ConfigPubishResponse(ResponseCode.FAIL.getCode(), errorMsg);
ConfigPubishResponse configPubishResponse = new ConfigPubishResponse();
configPubishResponse.setResultCode(ResponseCode.FAIL.getCode());
configPubishResponse.setMessage(errorMsg);
return configPubishResponse;
}
}

View File

@ -41,14 +41,18 @@ public class ConfigQueryResponse extends Response {
Map<String, String> labels = new HashMap<String, String>();
public ConfigQueryResponse() {
super(ConfigResponseTypeConstants.CONFIG_QUERY);
}
@Override
public String getType() {
return ConfigResponseTypeConstants.CONFIG_QUERY;
}
/**
* add label to this response.
*
* @param key
* @param value
* @param key key.
* @param value value.
*/
public void addLabel(String key, String value) {
this.labels.put(key, value);
@ -62,19 +66,21 @@ public class ConfigQueryResponse extends Response {
* @return
*/
public static ConfigQueryResponse buildFailResponse(int errorCode, String message) {
ConfigQueryResponse response = new ConfigQueryResponse(ResponseCode.FAIL.getCode(), message);
ConfigQueryResponse response = new ConfigQueryResponse();
response.setResultCode(ResponseCode.FAIL.getCode());
response.setErrorCode(errorCode);
response.setMessage(message);
return response;
}
/**
* Buidl success resposne.
*
* @param content.
* @param content content.
* @return
*/
public static ConfigQueryResponse buildSuccessResponse(String content) {
ConfigQueryResponse response = new ConfigQueryResponse(ResponseCode.SUCCESS.getCode(), "");
ConfigQueryResponse response = new ConfigQueryResponse();
response.setContent(content);
return response;
}
@ -115,10 +121,6 @@ public class ConfigQueryResponse extends Response {
this.content = content;
}
public ConfigQueryResponse(int resultCode, String message) {
super(ConfigResponseTypeConstants.CONFIG_QUERY, resultCode, message);
}
/**
* Getter method for property <tt>contentType</tt>.
*

View File

@ -31,8 +31,9 @@ public class ConfigRemoveResponse extends Response {
super();
}
public ConfigRemoveResponse(int resultCode, String message) {
super(ConfigResponseTypeConstants.CONFIG_REMOVE, resultCode, message);
@Override
public String getType() {
return ConfigResponseTypeConstants.CONFIG_REMOVE;
}
/**
@ -41,7 +42,7 @@ public class ConfigRemoveResponse extends Response {
* @return
*/
public static ConfigRemoveResponse buildSuccessResponse() {
return new ConfigRemoveResponse(ResponseCode.SUCCESS.getCode(), "");
return new ConfigRemoveResponse();
}
/**
@ -50,6 +51,9 @@ public class ConfigRemoveResponse extends Response {
* @return
*/
public static ConfigRemoveResponse buildFailResponse(String errorMsg) {
return new ConfigRemoveResponse(ResponseCode.FAIL.getCode(), errorMsg);
ConfigRemoveResponse removeResponse = new ConfigRemoveResponse();
removeResponse.setResultCode(ResponseCode.FAIL.getCode());
removeResponse.setMessage(errorMsg);
return removeResponse;
}
}

View File

@ -25,15 +25,18 @@ import com.alibaba.nacos.api.remote.response.Response;
*/
public class InstanceResponse extends Response {
private String type;
public InstanceResponse() {
}
public InstanceResponse(int resultCode, String message, String type) {
this(resultCode, 0, message, type);
@Override
public String getType() {
return this.type;
}
public InstanceResponse(int resultCode, int errorCode, String message, String type) {
super(type, resultCode, message);
setErrorCode(errorCode);
public InstanceResponse(String type) {
this.type = type;
}
}

View File

@ -19,6 +19,7 @@ package com.alibaba.nacos.api.naming.remote.response;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
/**
* Nacos naming query request.
@ -30,16 +31,28 @@ public class ServiceQueryResponse extends Response {
private ServiceInfo serviceInfo;
public ServiceQueryResponse() {
setType(NamingRemoteConstants.QUERY_SERVICE);
}
@Override
public String getType() {
return NamingRemoteConstants.QUERY_SERVICE;
}
public ServiceQueryResponse(ServiceInfo serviceInfo) {
this(200, "success");
this.serviceInfo = serviceInfo;
}
public ServiceQueryResponse(int resultCode, String message) {
super(NamingRemoteConstants.QUERY_SERVICE, resultCode, message);
public static ServiceQueryResponse buildSuccessResponse(ServiceInfo serviceInfo) {
ServiceQueryResponse serviceQueryResponse = new ServiceQueryResponse();
serviceQueryResponse.setServiceInfo(serviceInfo);
return serviceQueryResponse;
}
public static ServiceQueryResponse buildFailResponse(String message) {
ServiceQueryResponse serviceQueryResponse = new ServiceQueryResponse();
serviceQueryResponse.setResultCode(ResponseCode.FAIL.getCode());
serviceQueryResponse.setMessage(message);
return serviceQueryResponse;
}
public ServiceInfo getServiceInfo() {

View File

@ -28,6 +28,7 @@ import com.alibaba.nacos.api.naming.remote.response.ServiceQueryResponse;
import com.alibaba.nacos.api.remote.response.ConnectResetResponse;
import com.alibaba.nacos.api.remote.response.HeartBeatResponse;
import com.alibaba.nacos.api.remote.response.ResponseTypeConstants;
import com.alibaba.nacos.api.remote.response.UnKnowResponse;
import java.util.HashMap;
import java.util.Map;
@ -47,6 +48,7 @@ public class ResponseRegistry {
//internal response regitry
REGISTRY_RESPONSES.put(ResponseTypeConstants.HEART_BEAT, HeartBeatResponse.class);
REGISTRY_RESPONSES.put(ResponseTypeConstants.CONNECT_SWITCH, ConnectResetResponse.class);
REGISTRY_RESPONSES.put(ResponseTypeConstants.UNKNOW, UnKnowResponse.class);
//config response registry
REGISTRY_RESPONSES.put(ConfigResponseTypeConstants.CONFIG_CHANGE, ConfigChangeListenResponse.class);

View File

@ -24,7 +24,8 @@ package com.alibaba.nacos.api.remote.response;
*/
public class ConnectResetResponse extends Response {
public ConnectResetResponse(int resultCode, String message) {
super(ResponseTypeConstants.CONNECT_SWITCH, resultCode, message);
@Override
public String getType() {
return ResponseTypeConstants.CONNECT_SWITCH;
}
}

View File

@ -24,8 +24,8 @@ package com.alibaba.nacos.api.remote.response;
*/
public class HeartBeatResponse extends Response {
public HeartBeatResponse(int resultCode, String message) {
super(ResponseTypeConstants.HEART_BEAT, resultCode, message);
@Override
public String getType() {
return ResponseTypeConstants.HEART_BEAT;
}
}

View File

@ -30,8 +30,9 @@ public class PlainBodyResponse extends Response {
}
public PlainBodyResponse(int resultCode, String message) {
super(ResponseTypeConstants.PLAIN_BODY_STRING, resultCode, message);
@Override
public String getType() {
return ResponseTypeConstants.PLAIN_BODY_STRING;
}
/**

View File

@ -24,14 +24,12 @@ package com.alibaba.nacos.api.remote.response;
*/
public class Response {
int resultCode;
int resultCode = ResponseCode.SUCCESS.getCode();
int errorCode;
String message;
String type;
/**
* Check Response is Successd.
*
@ -42,18 +40,14 @@ public class Response {
}
public Response() {
}
public Response(String type) {
this.type = type;
}
public Response(String type, int resultCode, String message) {
this.type = type;
this.resultCode = resultCode;
this.message = message;
}
/**
* get type.
*
* @return type value;
*/
public abstract String getType();
/**
* Getter method for property <tt>resultCode</tt>.
@ -109,22 +103,4 @@ public class Response {
this.errorCode = errorCode;
}
/**
* Getter method for property <tt>type</tt>.
*
* @return property value of type
*/
public String getType() {
return type;
}
/**
* Setter method for property <tt>type</tt>.
*
* @param type value to be assigned to property type
*/
public void setType(String type) {
this.type = type;
}
}

View File

@ -29,4 +29,7 @@ public class ResponseTypeConstants {
public static final String CONNECT_SWITCH = "CONNECT_SWITCH";
public static final String UNKNOW = "UNKNOW";
}

View File

@ -0,0 +1,31 @@
/*
* 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.api.remote.response;
/**
* UnKnowResponse.
*
* @author liuzunfei
* @version $Id: UnKnowResponse.java, v 0.1 2020年07月16日 9:47 PM liuzunfei Exp $
*/
public class UnKnowResponse extends Response {
@Override
public String getType() {
return ResponseTypeConstants.UNKNOW;
}
}

View File

@ -64,7 +64,7 @@ public class ConfigChangeListenRequestHandler extends RequestHandler {
} else {
asyncListenContext.addListen(NacosRemoteConstants.LISTEN_CONTEXT_CONFIG, configKey, connectionId);
}
return new ConfigChangeListenResponse(200, "success");
return ConfigChangeListenResponse.buildSucessResponse();
}
@Override

View File

@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.core.remote;
import java.util.List;
package com.alibaba.nacos.core.remote;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.remote.request.HeartBeatRequest;
@ -25,32 +24,36 @@ import com.alibaba.nacos.api.remote.request.RequestTypeConstants;
import com.alibaba.nacos.api.remote.response.HeartBeatResponse;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* HeartBeatRequestHandler.
*
* @author liuzunfei
* @version $Id: HeartBeatRequestHandler.java, v 0.1 2020年07月14日 1:58 PM liuzunfei Exp $
*/
@Component
public class HeartBeatRequestHandler extends RequestHandler{
public class HeartBeatRequestHandler extends RequestHandler {
@Autowired
ConnectionManager connectionManager;
@Override
public Request parseBodyString(String bodyString) {
return JacksonUtils.toObj(bodyString, HeartBeatRequest.class);
}
@Override
public Response handle(Request request, RequestMeta meta) throws NacosException {
String connectionId = meta.getConnectionId();
connectionManager.refreshActiveTime(connectionId);
return new HeartBeatResponse(200,"heart beat success");
return new HeartBeatResponse();
}
@Override
public List<String> getRequestTypes() {
return Lists.newArrayList(RequestTypeConstants.HEART_BEAT);

View File

@ -16,17 +16,17 @@
package com.alibaba.nacos.core.remote.grpc;
import java.io.UnsupportedEncodingException;
import com.alibaba.nacos.api.grpc.GrpcMetadata;
import com.alibaba.nacos.api.grpc.GrpcResponse;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.alibaba.nacos.api.remote.response.UnKnowResponse;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import java.io.UnsupportedEncodingException;
/**
* GrpcUtils.
*
@ -64,8 +64,7 @@ public class GrpcUtils {
* @return
*/
public static GrpcResponse buildFailResponse(String msg) {
Response response = new Response();
UnKnowResponse response = new UnKnowResponse();
response.setErrorCode(ResponseCode.FAIL.getCode());
response.setMessage(msg);
byte[] bytes = JacksonUtils.toJsonBytes(response);

View File

@ -476,7 +476,43 @@
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="grpc-core"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/grpc-core.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/grpc-core.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<maxFileSize>2GB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>7GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<Pattern>%date %level %msg%n%n</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="grpc-digest"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/grpc-digest.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/grpc-digest.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<maxFileSize>2GB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>7GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<Pattern>%date %level %msg%n%n</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<logger name="com.alibaba.nacos.address.main" additivity="false">
<level value="INFO"/>
<appender-ref ref="nacos-address"/>
@ -486,7 +522,17 @@
<level value="INFO"/>
<appender-ref ref="cmdb-main"/>
</logger>
<logger name="com.alibaba.nacos.core.grpc" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="grpc-core"/>
</logger>
<logger name="com.alibaba.nacos.core.grpc.digest" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="grpc-digest"/>
</logger>
<logger name="com.alibaba.nacos.naming.main" additivity="false">
<level value="INFO"/>
<appender-ref ref="async-naming-server"/>

View File

@ -81,18 +81,18 @@ public class InstanceRequestHandler extends RequestHandler<InstanceRequest> {
instance.setMarked(true);
instance.validate();
serviceManager.addInstance(namespace, serviceName, instance.isEphemeral(), instance);
return new InstanceResponse(200, "success", NamingRemoteConstants.REGISTER_INSTANCE);
return new InstanceResponse(NamingRemoteConstants.REGISTER_INSTANCE);
}
private Response deregisterInstance(String namespace, String serviceName, InstanceRequest instanceRequest,
RequestMeta meta) throws NacosException {
if (!serviceManager.containService(namespace, serviceName)) {
Loggers.SRV_LOG.warn("remove instance from non-exist service: {}", serviceName);
return new InstanceResponse(200, "success", NamingRemoteConstants.DE_REGISTER_INSTANCE);
return new InstanceResponse(NamingRemoteConstants.DE_REGISTER_INSTANCE);
}
Instance instance = parseInstance(instanceRequest.getInstance());
serviceManager.removeInstance(namespace, serviceName, instance.isEphemeral(), instance);
return new InstanceResponse(200, "success", NamingRemoteConstants.DE_REGISTER_INSTANCE);
return new InstanceResponse(NamingRemoteConstants.DE_REGISTER_INSTANCE);
}
private Instance parseInstance(com.alibaba.nacos.api.naming.pojo.Instance instance) {