perf[net]: 删除protocol的version,因为现在协议可以兼容老协议了

This commit is contained in:
jaysunxiao committed 2022-07-01 13:58:44 +08:00
1 parent 82cf9bf615
commit 1c94c2f311
4 files changed
+14 -114

No files matched your search

@@ -99,7 +99,7 @@ public class RegisterVO {
var modules = Arrays.stream(moduleSplits)
.map(it -> it.trim())
.map(it -> it.split(StringUtils.HYPHEN))
.map(it -> new ProtocolModule(Byte.parseByte(it[0]), it[1], it[2], Integer.parseInt(it[3])))
.map(it -> new ProtocolModule(Byte.parseByte(it[0]), it[1], Integer.parseInt(it[2])))
.collect(Collectors.toList());
return modules;
}
@@ -109,7 +109,7 @@ public class RegisterVO {
}
public String toConsumerString() {
return toString() +
return this +
StringUtils.SPACE + StringUtils.VERTICAL_BAR + StringUtils.SPACE +
uuid;
}
@@ -149,7 +149,7 @@ public class RegisterVO {
}
public String joinWith(String sep, ProtocolModule module) {
return StringUtils.joinWith(sep, module.getId(), module.getName(), ProtocolModule.versionNumToStr(module.getVersion()), module.getGroup());
return StringUtils.joinWith(sep, module.getId(), module.getName(), module.getGroup());
}
public String getId() {
@@ -33,8 +33,8 @@ public class RegistryTest {
@Test
public void registerVoTest() {
var modules = List.of(new ProtocolModule((byte) 100, "aaa", "1.0.0")
, new ProtocolModule((byte) 120, "bbb", "1.0.0"));
var modules = List.of(new ProtocolModule((byte) 100, "aaa")
, new ProtocolModule((byte) 120, "bbb"));
var providerConfig = ProviderConfig.valueOf(HostAndPort.valueOf("127.0.0.1", 80).toHostAndPortStr(), modules);
var consumerConfig = ConsumerConfig.valueOf(modules);
@@ -51,57 +51,4 @@ public class RegistryTest {
System.out.println(NetUtil.LOOPBACK_IF);
}
@Test
public void moduleTest() {
var moduleId = (byte) 1;
var str = "1.0.0";
var module = new ProtocolModule(moduleId, "", str);
str = "0.0.0";
module = new ProtocolModule(moduleId, "", str);
str = "10.0.0";
module = new ProtocolModule(moduleId, "", str);
str = "10.11.10";
module = new ProtocolModule(moduleId, "", str);
str = "10.11.101";
module = new ProtocolModule(moduleId, "", str);
str = "99.99.999";
module = new ProtocolModule(moduleId, "", str);
module = null;
str = "1.00.001";
try {
module = new ProtocolModule(moduleId, "", str);
} catch (Exception e) {
}
Assert.assertNull(module);
str = "1.00.111";
try {
module = new ProtocolModule(moduleId, "", str);
} catch (Exception e) {
}
Assert.assertNull(module);
str = "1.001.0";
try {
module = new ProtocolModule(moduleId, "", str);
} catch (Exception e) {
}
Assert.assertNull(module);
str = "99.999.999";
try {
module = new ProtocolModule(moduleId, "", str);
} catch (Exception e) {
}
Assert.assertNotNull(module);
}
}
@@ -114,7 +114,7 @@ public class ProtocolAnalysis {
var enhanceList = new ArrayList<IProtocolRegistration>();
for (var moduleDefinition : xmlProtocols.getModules()) {
var module = new ProtocolModule(moduleDefinition.getId(), moduleDefinition.getName(), moduleDefinition.getVersion());
var module = new ProtocolModule(moduleDefinition.getId(), moduleDefinition.getName());
AssertionUtils.isTrue(module.getId() > 0, "[module:{}] [id:{}] 模块必须大于等于1", module.getName(), module.getId());
AssertionUtils.isNull(modules[module.getId()], "duplicate [module:{}] [id:{}] Exception!", module.getName(), module.getId());
@@ -13,7 +13,6 @@
package com.zfoo.protocol.registration;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.StringUtils;
/**
@@ -22,76 +21,38 @@ import com.zfoo.protocol.util.StringUtils;
*/
public class ProtocolModule {
public static final ProtocolModule DEFAULT_PROTOCOL_MODULE = new ProtocolModule((byte) 0, "default", "1.0.0");
public static final ProtocolModule DEFAULT_PROTOCOL_MODULE = new ProtocolModule((byte) 0, "default");
private byte id;
private String name;
/**
* 1.xxx.xxx,将1.0.0转化为1000000
*/
private int version;
private String loadBalancer;
private int group;
private transient int hash;
public ProtocolModule(byte id, String name, String version) {
public ProtocolModule(byte id, String name) {
if (id < 0) {
throw new IllegalArgumentException(StringUtils.format("模块[{}]的id[{}]必须大于0", name, id));
}
this.id = id;
this.name = name;
this.version = versionStrToNum(version);
this.perfectHash();
}
public ProtocolModule(byte id, String name, String version, int group) {
public ProtocolModule(byte id, String name, int group) {
this.id = id;
this.name = name;
this.version = versionStrToNum(version);
this.group = group;
this.perfectHash();
}
public ProtocolModule(String name, String loadBalancer, String group) {
this.name = name;
this.loadBalancer = loadBalancer;
this.group = Integer.parseInt(group);
}
public static void assertVersion(String version) {
if (!version.matches("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}")) {
throw new IllegalArgumentException(StringUtils
.format("[version:{}] must like xxx.xxx.xxx", version));
}
}
public static int versionStrToNum(String version) {
assertVersion(version);
var splits = version.split("\\" + StringUtils.PERIOD);
var versionNum = Integer.parseInt(splits[0]) * 1_000_000 + Integer.parseInt(splits[1]) * 1_000 + Integer.parseInt(splits[2]);
var newVersionStr = versionNumToStr(versionNum);
AssertionUtils.isTrue(version.equals(newVersionStr), "版本号转换前[{}]和转换后不相等[{}]", version, newVersionStr);
return versionNum;
}
public static String versionNumToStr(int version) {
var versionStr = version / 1_000_000 + StringUtils.PERIOD +
version / 1_000 % 1_000 + StringUtils.PERIOD +
version % 1_000;
assertVersion(versionStr);
return versionStr;
}
public void perfectHash() {
this.hash = id * 1_000_000 + this.version;
}
public byte getId() {
return id;
@@ -109,14 +70,6 @@ public class ProtocolModule {
this.name = name;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getLoadBalancer() {
return loadBalancer;
}
@@ -143,18 +96,18 @@ public class ProtocolModule {
}
ProtocolModule module = (ProtocolModule) o;
if (group == 0 || module.group == 0) {
return id == module.id && version == module.version;
return id == module.id;
}
return id == module.id && version == module.version && group == module.group;
return id == module.id && group == module.group;
}
@Override
public int hashCode() {
return this.hash;
return this.id;
}
@Override
public String toString() {
return StringUtils.format("[id:{}][name:{}][version:{}][group:{}][hash:{}]", id, name, version, group, hash);
return StringUtils.format("[id:{}][name:{}][group:{}]", id, name, group);
}
}