Merge pull request #2647 from wangweizZZ/issue_2618

[ISSUE #2618]modify config service md5 generation method
This commit is contained in:
liaochuntao 2020-04-21 19:52:43 +08:00 committed by GitHub
commit 900fa661ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 122 additions and 258 deletions

View File

@ -23,7 +23,7 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener;
import com.alibaba.nacos.client.config.utils.MD5; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.TenantUtil; import com.alibaba.nacos.client.utils.TenantUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -242,7 +242,7 @@ public class CacheData {
} }
static public String getMd5String(String config) { static public String getMd5String(String config) {
return (null == config) ? Constants.NULL : MD5.getInstance().getMD5String(config); return (null == config) ? Constants.NULL : MD5Utils.md5Hex(config, Constants.ENCODE);
} }
private String loadCacheContentFromDiskLocal(String name, String dataId, String group, String tenant) { private String loadCacheContentFromDiskLocal(String name, String dataId, String group, String tenant) {

View File

@ -25,7 +25,7 @@ import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
import com.alibaba.nacos.client.config.http.HttpAgent; import com.alibaba.nacos.client.config.http.HttpAgent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.ContentUtils; import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.MD5; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ParamUtil;
@ -279,7 +279,7 @@ public class ClientWorker {
// 没有 -> // 没有 ->
if (!cacheData.isUseLocalConfigInfo() && path.exists()) { if (!cacheData.isUseLocalConfigInfo() && path.exists()) {
String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant);
String md5 = MD5.getInstance().getMD5String(content); String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
cacheData.setUseLocalConfigInfo(true); cacheData.setUseLocalConfigInfo(true);
cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setLocalConfigInfoVersion(path.lastModified());
cacheData.setContent(content); cacheData.setContent(content);
@ -301,7 +301,7 @@ public class ClientWorker {
if (cacheData.isUseLocalConfigInfo() && path.exists() if (cacheData.isUseLocalConfigInfo() && path.exists()
&& cacheData.getLocalConfigInfoVersion() != path.lastModified()) { && cacheData.getLocalConfigInfoVersion() != path.lastModified()) {
String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant);
String md5 = MD5.getInstance().getMD5String(content); String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
cacheData.setUseLocalConfigInfo(true); cacheData.setUseLocalConfigInfo(true);
cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setLocalConfigInfoVersion(path.lastModified());
cacheData.setContent(content); cacheData.setContent(content);

View File

@ -17,7 +17,7 @@ package com.alibaba.nacos.client.config.impl;
import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.utils.MD5; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.IoUtils;
@ -42,8 +42,8 @@ public class HttpSimpleClient {
String encoding, long readTimeoutMs, boolean isSSL) throws IOException { String encoding, long readTimeoutMs, boolean isSSL) throws IOException {
String encodedContent = encodingParams(paramValues, encoding); String encodedContent = encodingParams(paramValues, encoding);
url += (null == encodedContent) ? "" : ("?" + encodedContent); url += (null == encodedContent) ? "" : ("?" + encodedContent);
if (Limiter.isLimit(MD5.getInstance().getMD5String( if (Limiter.isLimit(MD5Utils.md5Hex(
new StringBuilder(url).append(encodedContent).toString()))) { new StringBuilder(url).append(encodedContent).toString(), Constants.ENCODE))) {
return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD, return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD,
"More than client-side current limit threshold"); "More than client-side current limit threshold");
} }
@ -98,8 +98,8 @@ public class HttpSimpleClient {
String encoding, long readTimeoutMs, boolean isSSL) throws IOException { String encoding, long readTimeoutMs, boolean isSSL) throws IOException {
String encodedContent = encodingParams(paramValues, encoding); String encodedContent = encodingParams(paramValues, encoding);
encodedContent = (null == encodedContent) ? "" : encodedContent; encodedContent = (null == encodedContent) ? "" : encodedContent;
if (Limiter.isLimit(MD5.getInstance().getMD5String( if (Limiter.isLimit(MD5Utils.md5Hex(
new StringBuilder(url).append(encodedContent).toString()))) { new StringBuilder(url).append(encodedContent).toString(), Constants.ENCODE))) {
return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD, return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD,
"More than client-side current limit threshold"); "More than client-side current limit threshold");
} }
@ -150,8 +150,8 @@ public class HttpSimpleClient {
String encoding, long readTimeoutMs, boolean isSSL) throws IOException { String encoding, long readTimeoutMs, boolean isSSL) throws IOException {
String encodedContent = encodingParams(paramValues, encoding); String encodedContent = encodingParams(paramValues, encoding);
url += (null == encodedContent) ? "" : ("?" + encodedContent); url += (null == encodedContent) ? "" : ("?" + encodedContent);
if (Limiter.isLimit(MD5.getInstance().getMD5String( if (Limiter.isLimit(MD5Utils.md5Hex(
new StringBuilder(url).append(encodedContent).toString()))) { new StringBuilder(url).append(encodedContent).toString(), Constants.ENCODE))) {
return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD, return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD,
"More than client-side current limit threshold"); "More than client-side current limit threshold");
} }
@ -197,7 +197,7 @@ public class HttpSimpleClient {
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
String ts = String.valueOf(System.currentTimeMillis()); String ts = String.valueOf(System.currentTimeMillis());
String token = MD5.getInstance().getMD5String(ts + ParamUtil.getAppKey()); String token = MD5Utils.md5Hex(ts + ParamUtil.getAppKey(), Constants.ENCODE);
conn.addRequestProperty(Constants.CLIENT_APPNAME_HEADER, ParamUtil.getAppName()); conn.addRequestProperty(Constants.CLIENT_APPNAME_HEADER, ParamUtil.getAppName());
conn.addRequestProperty(Constants.CLIENT_REQUEST_TS_HEADER, ts); conn.addRequestProperty(Constants.CLIENT_REQUEST_TS_HEADER, ts);

View File

@ -1,132 +0,0 @@
/*
* Copyright 1999-2018 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.client.config.utils;
import com.alibaba.nacos.api.common.Constants;
import com.google.common.collect.Maps;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
/**
* MD5 util
*
* @author Nacos
*/
@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
public class MD5 {
private static int DIGITS_SIZE = 16;
private static char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static Map<Character, Integer> rDigits = Maps.newHashMapWithExpectedSize(16);
static {
for (int i = 0; i < digits.length; ++i) {
rDigits.put(digits[i], i);
}
}
private static MD5 me = new MD5();
private MessageDigest mHasher;
private ReentrantLock opLock = new ReentrantLock();
private MD5() {
try {
mHasher = MessageDigest.getInstance("md5");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static MD5 getInstance() {
return me;
}
public String getMD5String(String content) {
return bytes2string(hash(content));
}
public String getMD5String(byte[] content) {
return bytes2string(hash(content));
}
public byte[] getMD5Bytes(byte[] content) {
return hash(content);
}
/**
* 对字符串进行md5
*
* @param str
* @return md5 byte[16]
*/
public byte[] hash(String str) {
opLock.lock();
try {
byte[] bt = mHasher.digest(str.getBytes(Constants.ENCODE));
if (null == bt || bt.length != DIGITS_SIZE) {
throw new IllegalArgumentException("md5 need");
}
return bt;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported utf-8 encoding", e);
} finally {
opLock.unlock();
}
}
/**
* 对二进制数据进行md5
*
* @param data
* @return md5 byte[16]
*/
public byte[] hash(byte[] data) {
opLock.lock();
try {
byte[] bt = mHasher.digest(data);
if (null == bt || bt.length != DIGITS_SIZE) {
throw new IllegalArgumentException("md5 need");
}
return bt;
} finally {
opLock.unlock();
}
}
/**
* 将一个字节数组转化为可见的字符串
*
* @param bt
* @return
*/
public String bytes2string(byte[] bt) {
int l = bt.length;
char[] out = new char[l << 1];
for (int i = 0, j = 0; i < l; i++) {
out[j++] = digits[(0xF0 & bt[i]) >>> 4];
out[j++] = digits[0x0F & bt[i]];
}
return new String(out);
}
}

View File

@ -1,71 +0,0 @@
/*
* Copyright 1999-2019 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.client.config.utils;
import org.junit.Assert;
import org.junit.Test;
public class MD5Test {
@Test
public void testGetMD5String() {
Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e",
MD5.getInstance().getMD5String(""));
Assert.assertEquals("acbd18db4cc2f85cedef654fccc4a4d8",
MD5.getInstance().getMD5String("foo"));
Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e",
MD5.getInstance().getMD5String(new byte[0]));
Assert.assertEquals("5289df737df57326fcdd22597afb1fac",
MD5.getInstance().getMD5String(new byte[]{1, 2, 3}));
}
@Test
public void testGetMD5Bytes() {
byte[] bytes1 = new byte[]{-44, 29, -116, -39, -113, 0, -78,
4, -23, -128, 9, -104, -20, -8, 66, 126};
byte[] bytes2 = new byte[]{82, -119, -33, 115, 125, -11, 115,
38, -4, -35, 34, 89, 122, -5, 31, -84};
Assert.assertArrayEquals(bytes1,
MD5.getInstance().getMD5Bytes(new byte[0]));
Assert.assertArrayEquals(bytes2,
MD5.getInstance().getMD5Bytes(new byte[]{1, 2, 3}));
}
@Test
public void testHash() {
byte[] bytes1 = new byte[]{-44, 29, -116, -39, -113, 0, -78,
4, -23, -128, 9, -104, -20, -8, 66, 126};
byte[] bytes2 = new byte[]{-84, -67, 24, -37, 76, -62, -8, 92,
-19, -17, 101, 79, -52, -60, -92, -40};
byte[] bytes3 = new byte[]{82, -119, -33, 115, 125, -11, 115,
38, -4, -35, 34, 89, 122, -5, 31, -84};
Assert.assertArrayEquals(bytes1, MD5.getInstance().hash(""));
Assert.assertArrayEquals(bytes2, MD5.getInstance().hash("foo"));
Assert.assertArrayEquals(bytes1, MD5.getInstance().hash(new byte[0]));
Assert.assertArrayEquals(bytes3,
MD5.getInstance().hash(new byte[]{1, 2, 3}));
}
@Test
public void testBytes2string() {
Assert.assertEquals("", MD5.getInstance().bytes2string(new byte[0]));
Assert.assertEquals("010203",
MD5.getInstance().bytes2string(new byte[]{1, 2, 3}));
}
}

View File

@ -15,16 +15,18 @@
*/ */
package com.alibaba.nacos.common.utils; package com.alibaba.nacos.common.utils;
import java.math.BigInteger;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
/** /**
* MD5 generator * MD5 util
* *
* @author nacos *@author nacos
*/ */
public class Md5Utils { @SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
public class MD5Utils {
private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static ThreadLocal<MessageDigest> MESSAGE_DIGEST_LOCAL = new ThreadLocal<MessageDigest>() { private static ThreadLocal<MessageDigest> MESSAGE_DIGEST_LOCAL = new ThreadLocal<MessageDigest>() {
@Override @Override
@ -37,13 +39,11 @@ public class Md5Utils {
} }
}; };
private static final int HEX_VALUE_COUNT = 16; public static String md5Hex(byte[] bytes) throws NoSuchAlgorithmException {
public static String getMD5(byte[] bytes) throws NoSuchAlgorithmException {
try { try {
MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get(); MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get();
if (messageDigest != null) { if (messageDigest != null) {
return new BigInteger(1, messageDigest.digest(bytes)).toString(HEX_VALUE_COUNT); return encodeHexString(messageDigest.digest(bytes));
} }
throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error"); throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error");
} finally { } finally {
@ -51,11 +51,28 @@ public class Md5Utils {
} }
} }
public static String getMD5(String value, String encode) { public static String md5Hex(String value,String encode) {
try { try {
return getMD5(value.getBytes(encode)); return md5Hex(value.getBytes(encode));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
/**
* 将一个字节数组转化为可见的字符串
*/
public static String encodeHexString(byte[] bytes) {
int l = bytes.length;
char[] out = new char[l << 1];
for (int i = 0, j = 0; i < l; i++) {
out[j++] = DIGITS_LOWER[(0xF0 & bytes[i]) >>> 4];
out[j++] = DIGITS_LOWER[0x0F & bytes[i]];
}
return new String(out);
}
} }

View File

@ -0,0 +1,48 @@
/*
* Copyright 1999-2019 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.common.utils;
import com.alibaba.nacos.api.common.Constants;
import java.security.NoSuchAlgorithmException;
import org.junit.Assert;
import org.junit.Test;
public class MD5UtilsTest {
@Test
public void testMd5Hex() throws NoSuchAlgorithmException {
Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e",
MD5Utils.md5Hex("", Constants.ENCODE));
Assert.assertEquals("acbd18db4cc2f85cedef654fccc4a4d8",
MD5Utils.md5Hex("foo",Constants.ENCODE));
Assert.assertEquals("02f463eb799797e2a978fb1a2ae2991e",
MD5Utils.md5Hex("38c5ee9532f037a20b93d0f804cf111fca4003e451d09a692d9dea8032308d9c64eda9047fcd5e850284a49b1a0cfb2ecd45",Constants.ENCODE));
Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e",
MD5Utils.md5Hex(new byte[0]));
Assert.assertEquals("5289df737df57326fcdd22597afb1fac",
MD5Utils.md5Hex(new byte[]{1, 2, 3}));
}
@Test
public void testEncodeHexString() {
Assert.assertEquals("", MD5Utils.encodeHexString(new byte[0]));
Assert.assertEquals("010203",
MD5Utils.encodeHexString(new byte[]{1, 2, 3}));
}
}

View File

@ -15,7 +15,7 @@
*/ */
package com.alibaba.nacos.config.server.aspect; package com.alibaba.nacos.config.server.aspect;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.service.ConfigService; import com.alibaba.nacos.config.server.service.ConfigService;
@ -67,7 +67,7 @@ public class RequestLogAspect {
public Object interfacePublishSingle(ProceedingJoinPoint pjp, HttpServletRequest request, public Object interfacePublishSingle(ProceedingJoinPoint pjp, HttpServletRequest request,
HttpServletResponse response, String dataId, String group, String tenant, HttpServletResponse response, String dataId, String group, String tenant,
String content) throws Throwable { String content) throws Throwable {
final String md5 = content == null ? null : Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE);
MetricsMonitor.getPublishMonitor().incrementAndGet(); MetricsMonitor.getPublishMonitor().incrementAndGet();
return logClientRequest("publish", pjp, request, response, dataId, group, tenant, md5); return logClientRequest("publish", pjp, request, response, dataId, group, tenant, md5);
} }

View File

@ -15,7 +15,7 @@
*/ */
package com.alibaba.nacos.config.server.model; package com.alibaba.nacos.config.server.model;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -47,7 +47,7 @@ public class ConfigInfoBase implements Serializable, Comparable<ConfigInfoBase>
this.group = group; this.group = group;
this.content = content; this.content = content;
if (this.content != null) { if (this.content != null) {
this.md5 = Md5Utils.getMD5(this.content, Constants.ENCODE); this.md5 = MD5Utils.md5Hex(this.content, Constants.ENCODE);
} }
} }

View File

@ -15,7 +15,7 @@
*/ */
package com.alibaba.nacos.config.server.service; package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.CacheItem; import com.alibaba.nacos.config.server.model.CacheItem;
import com.alibaba.nacos.config.server.model.ConfigInfoBase; import com.alibaba.nacos.config.server.model.ConfigInfoBase;
@ -70,7 +70,7 @@ public class ConfigService {
} }
try { try {
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentMd5(groupKey))) { if (md5.equals(ConfigService.getContentMd5(groupKey))) {
dumpLog.warn( dumpLog.warn(
@ -116,7 +116,7 @@ public class ConfigService {
} }
try { try {
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentBetaMd5(groupKey))) { if (md5.equals(ConfigService.getContentBetaMd5(groupKey))) {
dumpLog.warn( dumpLog.warn(
"[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " "[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
@ -155,7 +155,7 @@ public class ConfigService {
} }
try { try {
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentTagMd5(groupKey, tag))) { if (md5.equals(ConfigService.getContentTagMd5(groupKey, tag))) {
dumpLog.warn( dumpLog.warn(
"[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " "[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
@ -192,7 +192,7 @@ public class ConfigService {
} }
try { try {
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) { if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant); String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant);
if (md5.equals(loacalMd5)) { if (md5.equals(loacalMd5)) {

View File

@ -16,7 +16,7 @@
package com.alibaba.nacos.config.server.service; package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.LogUtil;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -172,7 +172,7 @@ public class DiskUtil {
static public String getLocalConfigMd5(String dataId, String group, String tenant) static public String getLocalConfigMd5(String dataId, String group, String tenant)
throws IOException { throws IOException {
return Md5Utils.getMD5(getConfig(dataId, group, tenant), Constants.ENCODE); return MD5Utils.md5Hex(getConfig(dataId, group, tenant), Constants.ENCODE);
} }
static public File heartBeatFile() { static public File heartBeatFile() {

View File

@ -15,8 +15,8 @@
*/ */
package com.alibaba.nacos.config.server.service; package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.enums.FileTypeEnum; import com.alibaba.nacos.config.server.enums.FileTypeEnum;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.config.server.model.*; import com.alibaba.nacos.config.server.model.*;
@ -538,7 +538,7 @@ public class PersistService {
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
try { try {
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
jt.update( jt.update(
"INSERT INTO config_info_beta(data_id,group_id,tenant_id,app_name,content,md5,beta_ips,src_ip," "INSERT INTO config_info_beta(data_id,group_id,tenant_id,app_name,content,md5,beta_ips,src_ip,"
+ "src_user,gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)", + "src_user,gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
@ -564,7 +564,7 @@ public class PersistService {
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim();
try { try {
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
jt.update( jt.update(
"INSERT INTO config_info_tag(data_id,group_id,tenant_id,tag_id,app_name,content,md5,src_ip,src_user," "INSERT INTO config_info_tag(data_id,group_id,tenant_id,tag_id,app_name,content,md5,src_ip,src_user,"
+ "gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)", + "gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
@ -628,7 +628,7 @@ public class PersistService {
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
try { try {
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
jt.update( jt.update(
"UPDATE config_info_beta SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE " "UPDATE config_info_beta SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE "
+ "data_id=? AND group_id=? AND tenant_id=?", + "data_id=? AND group_id=? AND tenant_id=?",
@ -654,7 +654,7 @@ public class PersistService {
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim();
try { try {
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
jt.update( jt.update(
"UPDATE config_info_tag SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE " "UPDATE config_info_tag SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE "
+ "data_id=? AND group_id=? AND tenant_id=? AND tag_id=?", + "data_id=? AND group_id=? AND tenant_id=? AND tag_id=?",
@ -2695,7 +2695,7 @@ public class PersistService {
final String type = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("type"); final String type = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("type");
final String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema"); final String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema");
final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
KeyHolder keyHolder = new GeneratedKeyHolder(); KeyHolder keyHolder = new GeneratedKeyHolder();
@ -2904,7 +2904,7 @@ public class PersistService {
final Timestamp time, Map<String, Object> configAdvanceInfo) { final Timestamp time, Map<String, Object> configAdvanceInfo) {
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
String desc = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("desc"); String desc = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("desc");
String use = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("use"); String use = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("use");
String effect = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("effect"); String effect = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("effect");
@ -3061,7 +3061,7 @@ public class PersistService {
final Timestamp time, String ops) { final Timestamp time, String ops) {
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
try { try {
jt.update( jt.update(
"INSERT INTO his_config_info (id,data_id,group_id,tenant_id,app_name,content,md5,src_ip,src_user,gmt_modified,op_type) VALUES(?,?,?,?,?,?,?,?,?,?,?)", "INSERT INTO his_config_info (id,data_id,group_id,tenant_id,app_name,content,md5,src_ip,src_user,gmt_modified,op_type) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
@ -3355,7 +3355,7 @@ public class PersistService {
String md5InDb = cf.getMd5(); String md5InDb = cf.getMd5();
final String content = cf.getContent(); final String content = cf.getContent();
final String tenant = cf.getTenant(); final String tenant = cf.getTenant();
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (StringUtils.isBlank(md5InDb)) { if (StringUtils.isBlank(md5InDb)) {
try { try {
updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified())); updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified()));

View File

@ -16,7 +16,7 @@
package com.alibaba.nacos.config.server.service.dump; package com.alibaba.nacos.config.server.service.dump;
import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.manager.TaskManager; import com.alibaba.nacos.config.server.manager.TaskManager;
import com.alibaba.nacos.config.server.model.ConfigInfo; import com.alibaba.nacos.config.server.model.ConfigInfo;
@ -347,7 +347,7 @@ public class DumpService {
ConfigInfo cf = MergeTaskProcessor.merge(dataId, group, tenant, datumList); ConfigInfo cf = MergeTaskProcessor.merge(dataId, group, tenant, datumList);
String aggrContent = cf.getContent(); String aggrContent = cf.getContent();
String localContentMD5 = ConfigService.getContentMd5(GroupKey.getKey(dataId, group)); String localContentMD5 = ConfigService.getContentMd5(GroupKey.getKey(dataId, group));
String aggrConetentMD5 = Md5Utils.getMD5(aggrContent, Constants.ENCODE); String aggrConetentMD5 = MD5Utils.md5Hex(aggrContent, Constants.ENCODE);
if (!StringUtils.equals(localContentMD5, aggrConetentMD5)) { if (!StringUtils.equals(localContentMD5, aggrConetentMD5)) {
persistService.insertOrUpdate(null, null, cf, time, null, false); persistService.insertOrUpdate(null, null, cf, time, null, false);

View File

@ -15,7 +15,7 @@
*/ */
package com.alibaba.nacos.config.server.service.dump; package com.alibaba.nacos.config.server.service.dump;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.manager.AbstractTask; import com.alibaba.nacos.config.server.manager.AbstractTask;
import com.alibaba.nacos.config.server.manager.TaskProcessor; import com.alibaba.nacos.config.server.manager.TaskProcessor;
@ -265,7 +265,7 @@ class DumpAllProcessor implements TaskProcessor {
cf.getLastModified(), cf.getType()); cf.getLastModified(), cf.getType());
final String content = cf.getContent(); final String content = cf.getContent();
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
LogUtil.dumpLog.info("[dump-all-ok] {}, {}, length={}, md5={}", LogUtil.dumpLog.info("[dump-all-ok] {}, {}, length={}, md5={}",
GroupKey2.getKey(cf.getDataId(), cf.getGroup()), cf.getLastModified(), content.length(), md5); GroupKey2.getKey(cf.getDataId(), cf.getGroup()), cf.getLastModified(), content.length(), md5);
} }
@ -410,7 +410,7 @@ class DumpChangeProcessor implements TaskProcessor {
boolean result = ConfigService.dumpChange(cf.getDataId(), cf.getGroup(), cf.getTenant(), boolean result = ConfigService.dumpChange(cf.getDataId(), cf.getGroup(), cf.getTenant(),
cf.getContent(), cf.getLastModified()); cf.getContent(), cf.getLastModified());
final String content = cf.getContent(); final String content = cf.getContent();
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
LogUtil.defaultLog.info( LogUtil.defaultLog.info(
"[dump-change-ok] {}, {}, length={}, md5={}", "[dump-change-ok] {}, {}, length={}, md5={}",
new Object[] { new Object[] {

View File

@ -15,7 +15,7 @@
*/ */
package com.alibaba.nacos.config.server.service.trace; package com.alibaba.nacos.config.server.service.trace;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.LogUtil;
@ -62,7 +62,7 @@ public class ConfigTraceService {
} }
//localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed = -1] | ext //localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed = -1] | ext
// (md5) // (md5)
String md5 = content == null ? null : Md5Utils.getMD5(content, Constants.ENCODE); String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE);
LogUtil.traceLog.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", LOCAL_IP, dataId, group, tenant, LogUtil.traceLog.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", LOCAL_IP, dataId, group, tenant,
requestIpAppName, ts, handleIp, "persist", type, -1, md5); requestIpAppName, ts, handleIp, "persist", type, -1, md5);

View File

@ -17,7 +17,8 @@ package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.naming.pojo.Record; import com.alibaba.nacos.naming.pojo.Record;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -66,7 +67,7 @@ public class Instances implements Record {
sb.append(","); sb.append(",");
} }
return Md5Utils.getMD5(sb.toString(), "UTF-8"); return MD5Utils.md5Hex(sb.toString(), Constants.ENCODE);
} }
public String convertMap2String(Map<String, String> map) { public String convertMap2String(Map<String, String> map) {

View File

@ -17,7 +17,8 @@ package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.boot.SpringContext;
import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.RecordListener; import com.alibaba.nacos.naming.consistency.RecordListener;
@ -472,7 +473,7 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
ipsString.append(","); ipsString.append(",");
} }
checksum = Md5Utils.getMD5(ipsString.toString(), "UTF-8"); checksum = MD5Utils.md5Hex(ipsString.toString(), Constants.ENCODE);
} }
private void updateOrAddCluster(Collection<Cluster> clusters) { private void updateOrAddCluster(Collection<Cluster> clusters) {