From 4f0ec0a1960bb8da650c700e97579938397aead7 Mon Sep 17 00:00:00 2001 From: "nov.lzf" Date: Thu, 4 Feb 2021 16:22:50 +0800 Subject: [PATCH] optimize config file read (#4875) --- .../remote/ConfigQueryRequestHandler.java | 59 ++++++------------- 1 file changed, 18 insertions(+), 41 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java index 7537c818af1..6e9cd64373c 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java @@ -37,20 +37,17 @@ import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.core.remote.RequestHandler; import com.alibaba.nacos.core.remote.control.TpsControl; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; -import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.nio.charset.Charset; -import static com.alibaba.nacos.api.common.Constants.LINE_BREAK; +import static com.alibaba.nacos.api.common.Constants.ENCODE; import static com.alibaba.nacos.config.server.utils.LogUtil.PULL_LOG; import static com.alibaba.nacos.config.server.utils.RequestUtil.CLIENT_APPNAME_HEADER; @@ -74,8 +71,7 @@ public ConfigQueryRequestHandler(PersistService persistService) { @Override @TpsControl(pointName = "ConfigQuery", parsers = {ConfigQueryGroupKeyParser.class, ConfigQueryGroupParser.class}) @Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class) - public ConfigQueryResponse handle(ConfigQueryRequest request, RequestMeta meta) - throws NacosException { + public ConfigQueryResponse handle(ConfigQueryRequest request, RequestMeta meta) throws NacosException { try { ConfigQueryResponse context = getContext(request, meta, request.isNotify()); @@ -99,7 +95,7 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re final String groupKey = GroupKey2 .getKey(configQueryRequest.getDataId(), configQueryRequest.getGroup(), configQueryRequest.getTenant()); - + String autoTag = configQueryRequest.getHeader(com.alibaba.nacos.api.common.Constants.VIPSERVER_TAG); String requestIpApp = meta.getLabels().get(CLIENT_APPNAME_HEADER); @@ -208,7 +204,7 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re } } } - + response.setMd5(md5); if (PropertyUtil.isDirectRead()) { @@ -218,10 +214,16 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re } else { //read from file - String content = readFileContent(file); - response.setContent(content); - response.setLastModified(lastModified); - response.setResultCode(ResponseCode.SUCCESS.getCode()); + String content = null; + try { + content = readFileContent(file); + response.setContent(content); + response.setLastModified(lastModified); + response.setResultCode(ResponseCode.SUCCESS.getCode()); + } catch (IOException e) { + response.setErrorInfo(ResponseCode.FAIL.getCode(), e.getMessage()); + return response; + } } @@ -262,34 +264,9 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re * @param file file to read. * @return content. */ - public static String readFileContent(File file) { - BufferedReader reader = null; - StringBuffer sbf = new StringBuffer(); - try { - InputStreamReader isr = new InputStreamReader(new FileInputStream(file), Charset.forName(Constants.ENCODE)); - - reader = new BufferedReader(isr); - String tempStr; - while ((tempStr = reader.readLine()) != null) { - sbf.append(tempStr).append(LINE_BREAK); - } - if (sbf.indexOf(LINE_BREAK) > 0) { - sbf.setLength(sbf.length() - 1); - } - reader.close(); - return sbf.toString(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } - } - return sbf.toString(); + public static String readFileContent(File file) throws IOException { + return FileUtils.readFileToString(file, ENCODE); + } private static void releaseConfigReadLock(String groupKey) {