Skip to content
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 @@ -31,13 +31,28 @@
public class SnowFlakeInstanceIdGenerator implements InstanceIdGenerator {

private static final SnowFlowerIdGenerator SNOW_FLOWER_ID_GENERATOR = new SnowFlowerIdGenerator();

static {
SNOW_FLOWER_ID_GENERATOR.init();

private static volatile boolean initialize = false;

private static final Object LOCK = new Object();

/**
* initialize the workerId and ensure that it is only initialized once.
*/
private void ensureWorkerIdInitialization() {
if (!initialize) {
synchronized (LOCK) {
if (!initialize) {
SNOW_FLOWER_ID_GENERATOR.init();
initialize = true;
}
}
}
}

@Override
public String generateInstanceId(Instance instance) {
ensureWorkerIdInitialization();
return SNOW_FLOWER_ID_GENERATOR.nextId() + NAMING_INSTANCE_ID_SPLITTER
+ instance.getClusterName() + NAMING_INSTANCE_ID_SPLITTER
+ instance.getServiceName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.env.StandardEnvironment;

import javax.servlet.http.HttpServletRequest;

Expand Down Expand Up @@ -54,6 +56,7 @@ public class HttpRequestInstanceBuilderTest {
@BeforeClass
public static void setUpBeforeClass() {
NacosServiceLoader.load(InstanceExtensionHandler.class);
EnvUtil.setEnvironment(new StandardEnvironment());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}

@Before
Expand Down