Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove version from the keywords of service name. #95

Merged
merged 2 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,13 @@ public abstract class AbstractInterfaceConfig<T, S extends AbstractInterfaceConf
* 不管普通调用和泛化调用,都是设置实际的接口类名称,
*
* @see #uniqueId
* @see #version
*/
protected String interfaceId;

/**
* 服务标签:做为服务唯一标识的组成部分
*
* @see #interfaceId
* @see #version
*/
protected String uniqueId = getStringValue(DEFAULT_UNIQUEID);

Expand Down Expand Up @@ -138,14 +136,17 @@ public abstract class AbstractInterfaceConfig<T, S extends AbstractInterfaceConf
protected String proxy = getStringValue(DEFAULT_PROXY);

/**
* 服务分组:不做为服务唯一标识
* 服务分组:不做为服务唯一标识的一部分
* @deprecated 不再作为服务唯一标识,请直接使用 {@link #uniqueId} 代替
*/
@Deprecated
protected String group = getStringValue(DEFAULT_GROUP);
/**
* 版本:做为服务唯一标识的一部分
* 服务版本:不做为服务唯一标识的一部分
*
* @see #version
* @see #interfaceId
* @see #uniqueId
* @deprecated 从5.4.0开始,不再作为服务唯一标识,请直接使用 {@link #uniqueId} 代替
*/
protected String version = getStringValue(DEFAULT_VERSION);
/**
Expand Down Expand Up @@ -453,6 +454,7 @@ public S setProxy(String proxy) {
*
* @return the group
*/
@Deprecated
public String getGroup() {
return group;
}
Expand All @@ -462,7 +464,9 @@ public String getGroup() {
*
* @param group the group
* @return the group
* @deprecated Use {@link #setUniqueId(String)}
*/
@Deprecated
public S setGroup(String group) {
this.group = group;
return castThis();
Expand All @@ -473,6 +477,7 @@ public S setGroup(String group) {
*
* @return the version
*/
@Deprecated
public String getVersion() {
return version;
}
Expand All @@ -482,7 +487,9 @@ public String getVersion() {
*
* @param version the version
* @return the version
* @deprecated Use {@link #setUniqueId(String)}
*/
@Deprecated
public S setVersion(String version) {
this.version = version;
return castThis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ PS:大家也看到了,本JSON文档是支持注释的,而标准JSON是不支
"default.consumer.bootstrap": "sofa",
// 默认uniqueId 做为服务唯一标识
"default.uniqueId": "",
// 默认version 做为服务唯一标识
"default.version": "1.0",
// 默认version 不做为服务唯一标识
"default.version": "",
// 默认分组 不做为服务唯一标识
"default.group": "",
// 默认注册中心
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public DefaultClientProxyInvoker(ConsumerBootstrap bootstrap) {

protected void cacheCommonData() {
// 缓存数据
this.serviceName = ConfigUniqueNameGenerator.getUniqueName(consumerConfig);
this.serviceName = ConfigUniqueNameGenerator.getServiceName(consumerConfig);
this.serializeType = parseSerializeType(consumerConfig.getSerialization());
}

Expand Down Expand Up @@ -178,6 +178,6 @@ public Cluster setCluster(Cluster newCluster) {

@Override
public String toString() {
return consumerConfig != null ? ConfigUniqueNameGenerator.getUniqueName(consumerConfig) : super.toString();
return consumerConfig != null ? ConfigUniqueNameGenerator.getServiceName(consumerConfig) : super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
*/
public class ConfigUniqueNameGenerator {

/**
* 得到服务唯一名称,无需兼容之前的版本
*
* @param interfaceConfig 服务提供者或者服务消费者配置
* @return 服务唯一名称
* @since 5.4.0
*/
public static String getServiceName(AbstractInterfaceConfig interfaceConfig) {
String uniqueId = interfaceConfig.getUniqueId();
return interfaceConfig.getInterfaceId() + (StringUtils.isEmpty(uniqueId) ? "" : ":" + uniqueId);
}

/**
* 唯一标识UniqueName的产生方法,主要用于内部找接口等,格式为interface:version[:uniqueId]
*
Expand All @@ -33,8 +45,10 @@ public class ConfigUniqueNameGenerator {
*/
public static String getUniqueName(AbstractInterfaceConfig interfaceConfig) {
// 加上 1.0 是为了兼容之前的版本
String version = interfaceConfig.getVersion();
String uniqueId = interfaceConfig.getUniqueId();
return interfaceConfig.getInterfaceId() + ":" + interfaceConfig.getVersion()
return interfaceConfig.getInterfaceId()
+ (StringUtils.isEmpty(version) ? ":1.0" : ":" + version)
+ (StringUtils.isEmpty(uniqueId) ? "" : ":" + uniqueId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,33 @@
*
*/
public class ConfigUniqueNameGeneratorTest {

@Test
public void getServiceName() {
ProviderConfig providerConfig = (ProviderConfig) new ProviderConfig().setInterfaceId("com.xx");
Assert.assertEquals(ConfigUniqueNameGenerator.getServiceName(providerConfig), "com.xx");

providerConfig = (ProviderConfig) new ProviderConfig().setInterfaceId("com.xx").setVersion("2.0");
Assert.assertEquals(ConfigUniqueNameGenerator.getServiceName(providerConfig), "com.xx");

providerConfig = (ProviderConfig) new ProviderConfig().setInterfaceId("com.xx").setVersion("2.0")
.setUniqueId("ud");
Assert.assertEquals(ConfigUniqueNameGenerator.getServiceName(providerConfig), "com.xx:ud");

ConsumerConfig consumerConfig = (ConsumerConfig) new ConsumerConfig().setInterfaceId("com.xx");
Assert.assertEquals(ConfigUniqueNameGenerator.getServiceName(consumerConfig), "com.xx");

consumerConfig = (ConsumerConfig) new ConsumerConfig().setInterfaceId("com.xx").setVersion("2.0");
Assert.assertEquals(ConfigUniqueNameGenerator.getServiceName(consumerConfig), "com.xx");

consumerConfig = (ConsumerConfig) new ConsumerConfig().setInterfaceId("com.xx").setVersion("2.0")
.setUniqueId("ud");
Assert.assertEquals(ConfigUniqueNameGenerator.getServiceName(consumerConfig), "com.xx:ud");
}

@Test
public void getUniqueName() throws Exception {

ProviderConfig providerConfig = (ProviderConfig) new ProviderConfig().setInterfaceId("com.xx");
Assert.assertEquals(ConfigUniqueNameGenerator.getUniqueName(providerConfig), "com.xx:1.0");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public SofaResourceFactory(ProviderConfig providerConfig) {
super(providerConfig.getRef());
this.providerConfig = providerConfig;
// 缓存服务名计算和应用名计算
this.serviceName = ConfigUniqueNameGenerator.getUniqueName(providerConfig);
this.serviceName = ConfigUniqueNameGenerator.getServiceName(providerConfig);
this.appName = providerConfig.getAppName();
}

Expand Down