-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into develop-issue#6284
- Loading branch information
Showing
49 changed files
with
1,835 additions
and
385 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
api/src/main/java/com/alibaba/nacos/api/naming/pojo/builder/InstanceBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* 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.naming.pojo.builder; | ||
|
||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Builder for {@link Instance}. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class InstanceBuilder { | ||
|
||
private String instanceId; | ||
|
||
private String ip; | ||
|
||
private Integer port; | ||
|
||
private Double weight; | ||
|
||
private Boolean healthy; | ||
|
||
private Boolean enabled; | ||
|
||
private Boolean ephemeral; | ||
|
||
private String clusterName; | ||
|
||
private String serviceName; | ||
|
||
private Map<String, String> metadata = new HashMap<>(); | ||
|
||
private InstanceBuilder() { | ||
} | ||
|
||
public InstanceBuilder setInstanceId(String instanceId) { | ||
this.instanceId = instanceId; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setIp(String ip) { | ||
this.ip = ip; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setPort(Integer port) { | ||
this.port = port; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setWeight(Double weight) { | ||
this.weight = weight; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setHealthy(Boolean healthy) { | ||
this.healthy = healthy; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setEnabled(Boolean enabled) { | ||
this.enabled = enabled; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setEphemeral(Boolean ephemeral) { | ||
this.ephemeral = ephemeral; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setClusterName(String clusterName) { | ||
this.clusterName = clusterName; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setServiceName(String serviceName) { | ||
this.serviceName = serviceName; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder setMetadata(Map<String, String> metadata) { | ||
this.metadata = metadata; | ||
return this; | ||
} | ||
|
||
public InstanceBuilder addMetadata(String metaKey, String metaValue) { | ||
this.metadata.put(metaKey, metaValue); | ||
return this; | ||
} | ||
|
||
/** | ||
* Build a new {@link Instance}. | ||
* | ||
* @return new instance | ||
*/ | ||
public Instance build() { | ||
Instance result = new Instance(); | ||
if (!Objects.isNull(instanceId)) { | ||
result.setInstanceId(instanceId); | ||
} | ||
if (!Objects.isNull(ip)) { | ||
result.setIp(ip); | ||
} | ||
if (!Objects.isNull(port)) { | ||
result.setPort(port); | ||
} | ||
if (!Objects.isNull(weight)) { | ||
result.setWeight(weight); | ||
} | ||
if (!Objects.isNull(healthy)) { | ||
result.setHealthy(healthy); | ||
} | ||
if (!Objects.isNull(enabled)) { | ||
result.setEnabled(enabled); | ||
} | ||
if (!Objects.isNull(ephemeral)) { | ||
result.setEphemeral(ephemeral); | ||
} | ||
if (!Objects.isNull(clusterName)) { | ||
result.setClusterName(clusterName); | ||
} | ||
if (!Objects.isNull(serviceName)) { | ||
result.setServiceName(serviceName); | ||
} | ||
result.setMetadata(metadata); | ||
return result; | ||
} | ||
|
||
public static InstanceBuilder newBuilder() { | ||
return new InstanceBuilder(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
api/src/main/java/com/alibaba/nacos/api/naming/spi/generator/IdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.naming.spi.generator; | ||
|
||
/** | ||
* Generator SPI for Instance Id. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public interface IdGenerator { | ||
|
||
/** | ||
* Generate instance id. | ||
* | ||
* @return instance id | ||
*/ | ||
String generateInstanceId(); | ||
} |
85 changes: 85 additions & 0 deletions
85
api/src/test/java/com/alibaba/nacos/api/naming/pojo/builder/InstanceBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.naming.pojo.builder; | ||
|
||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class InstanceBuilderTest { | ||
|
||
private static final String SERVICE_NAME = "testService"; | ||
|
||
private static final String CLUSTER_NAME = "testCluster"; | ||
|
||
private static final String INSTANCE_ID = "ID"; | ||
|
||
private static final String IP = "127.0.0.1"; | ||
|
||
private static final int PORT = 8848; | ||
|
||
private static final double WEIGHT = 2.0; | ||
|
||
private static final boolean HEALTHY = false; | ||
|
||
private static final boolean ENABLED = false; | ||
|
||
private static final boolean EPHEMERAL = false; | ||
|
||
private static final String META_KEY = "key"; | ||
|
||
private static final String META_VALUE = "value"; | ||
|
||
@Test | ||
public void testBuildFullInstance() { | ||
InstanceBuilder builder = InstanceBuilder.newBuilder(); | ||
Instance actual = builder.setServiceName(SERVICE_NAME).setClusterName(CLUSTER_NAME).setInstanceId(INSTANCE_ID) | ||
.setIp(IP).setPort(PORT).setWeight(WEIGHT).setHealthy(HEALTHY).setEnabled(ENABLED) | ||
.setEphemeral(EPHEMERAL).addMetadata(META_KEY, META_VALUE).build(); | ||
assertThat(actual.getServiceName(), is(SERVICE_NAME)); | ||
assertThat(actual.getClusterName(), is(CLUSTER_NAME)); | ||
assertThat(actual.getInstanceId(), is(INSTANCE_ID)); | ||
assertThat(actual.getIp(), is(IP)); | ||
assertThat(actual.getPort(), is(PORT)); | ||
assertThat(actual.getWeight(), is(WEIGHT)); | ||
assertThat(actual.isHealthy(), is(HEALTHY)); | ||
assertThat(actual.isEnabled(), is(ENABLED)); | ||
assertThat(actual.isEphemeral(), is(EPHEMERAL)); | ||
assertThat(actual.getMetadata().size(), is(1)); | ||
assertThat(actual.getMetadata().get(META_KEY), is(META_VALUE)); | ||
} | ||
|
||
@Test | ||
public void testBuildEmptyInstance() { | ||
InstanceBuilder builder = InstanceBuilder.newBuilder(); | ||
Instance actual = builder.build(); | ||
assertNull(actual.getServiceName()); | ||
assertNull(actual.getClusterName()); | ||
assertNull(actual.getInstanceId()); | ||
assertNull(actual.getIp()); | ||
assertThat(actual.getPort(), is(0)); | ||
assertThat(actual.getWeight(), is(1.0)); | ||
assertTrue(actual.isHealthy()); | ||
assertTrue(actual.isEnabled()); | ||
assertTrue(actual.isEphemeral()); | ||
assertTrue(actual.getMetadata().isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.