Spring boot 当运行spring启动测试时,得到hazelcast.core.DuplicateInstanceNameException
如何使用Hazelcast执行spring boot应用程序的集成测试,因为当运行所有测试时都会得到Hazelcast.core.DuplicateInstanceNameException 我使用Spring Boot2.0.0.RC1和Hazelcast3.9.2 对hazelcast使用java配置:Spring boot 当运行spring启动测试时,得到hazelcast.core.DuplicateInstanceNameException,spring-boot,integration-testing,hazelcast,Spring Boot,Integration Testing,Hazelcast,如何使用Hazelcast执行spring boot应用程序的集成测试,因为当运行所有测试时都会得到Hazelcast.core.DuplicateInstanceNameException 我使用Spring Boot2.0.0.RC1和Hazelcast3.9.2 对hazelcast使用java配置: @Bean public Config getHazelCastServerConfig() { final Config config = new Config(); co
@Bean
public Config getHazelCastServerConfig() {
final Config config = new Config();
config.setInstanceName(hzInstance);
config.getGroupConfig().setName(hzGroupName).setPassword(hzGroupPassword);
final ManagementCenterConfig managementCenterConfig = config.getManagementCenterConfig();
managementCenterConfig.setEnabled(true);
managementCenterConfig.setUrl(mancenter);
final MapConfig mapConfig = new MapConfig();
mapConfig.setName(mapName);
mapConfig.setEvictionPolicy(EvictionPolicy.NONE);
mapConfig.setTimeToLiveSeconds(0);
mapConfig.setMaxIdleSeconds(0);
config.getScheduledExecutorConfig(scheduler)
.setPoolSize(16)
.setCapacity(100)
.setDurability(1);
final NetworkConfig networkConfig = config.getNetworkConfig();
networkConfig.setPort(5701);
networkConfig.setPortAutoIncrement(true).setPortCount(30);
final JoinConfig joinConfig = networkConfig.getJoin();
joinConfig.getMulticastConfig().setEnabled(false);
joinConfig.getAwsConfig().setEnabled(false);
final TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig();
tcpIpConfig.addMember(memberOne)
.addMember(memberTwo);
tcpIpConfig.setEnabled(true);
return config;
}
@Bean
public HazelcastInstance getHazelCastServerInstance() {
final HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(getHazelCastServerConfig());
hazelcastInstance.getClientService().addClientListener(new ClientListener() {
@Override
public void clientConnected(Client client) {
System.out.println(String.format("Connected %s %s %s", client.getClientType(), client.getUuid(), client.getSocketAddress()));
log.info(String.format("Connected %s %s %s", client.getClientType(), client.getUuid(), client.getSocketAddress()));
}
@Override
public void clientDisconnected(Client client) {
System.out.println(String.format("Disconnected %s %s %s", client.getClientType(), client.getUuid(), client.getSocketAddress()));
log.info(String.format("Disconnected %s %s %s", client.getClientType(), client.getUuid(), client.getSocketAddress()));
}
});
return hazelcastInstance;
}
我有一个简单的测试:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UpaSdcApplication.class)
@ActiveProfiles("test")
public class CheckEndpoints {
@Autowired
private ApplicationContext context;
private static final String HEALTH_ENDPOINT = "/actuator/health";
private static WebTestClient testClient;
@Before
public void init() {
testClient = org.springframework.test.web.reactive.server.WebTestClient
.bindToApplicationContext(context)
.configureClient()
.filter(basicAuthentication())
.build();
}
@Test
public void testHealth(){
testClient.get().uri(HEALTH_ENDPOINT).accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.isOk()
.expectBody()
.json("{\"status\": \"UP\"}");
}
}
如果在测试类与其他测试分离的情况下运行,则执行良好并通过测试。
如果与其他测试一起运行-获取异常:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.hazelcast.core.HazelcastInstance]: Factory method 'getHazelCastServerInstance' threw exception; nested exception is com.hazelcast.core.DuplicateInstanceNameException: HazelcastInstance with name 'counter-instance' already exists!
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579)
... 91 more
Caused by: com.hazelcast.core.DuplicateInstanceNameException: HazelcastInstance with name 'counter-instance' already exists!
at com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance(HazelcastInstanceFactory.java:170)
at com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance(HazelcastInstanceFactory.java:124)
at com.hazelcast.core.Hazelcast.newHazelcastInstance(Hazelcast.java:58)
at net.kyivstar.upa.sdc.config.HazelcastConfiguration.getHazelCastServerInstance(HazelcastConfiguration.java:84)
at net.kyivstar.upa.sdc.config.HazelcastConfiguration$$EnhancerBySpringCGLIB$$c7da65f3.CGLIB$getHazelCastServerInstance$0(<generated>)
at net.kyivstar.upa.sdc.config.HazelcastConfiguration$$EnhancerBySpringCGLIB$$c7da65f3$$FastClassBySpringCGLIB$$b920d5ef.invoke(<generated>)
原因:org.springframework.beans.beans实例化异常:未能实例化[com.hazelcast.core.HazelcastInstance]:工厂方法“getHazelCastServerInstance”引发异常;嵌套异常为com.hazelcast.core.DuplicateInstanceNameException:名为“计数器实例”的HazelcastInstance已存在!
位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:185)
位于org.springframework.beans.factory.support.ConstructorResolver.InstanceUsingFactoryMethod(ConstructorResolver.java:579)
... 91多
原因:com.hazelcast.core.DuplicateInstanceNameException:名为“计数器实例”的HazelcastInstance已存在!
位于com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance(HazelcastInstanceFactory.java:170)
位于com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance(HazelcastInstanceFactory.java:124)
位于com.hazelcast.core.hazelcast.newHazelcastInstance(hazelcast.java:58)
位于net.kyivstar.upa.sdc.config.HazelcastConfiguration.getHazelCastServerInstance(HazelcastConfiguration.java:84)
位于net.kyivstar.upa.sdc.config.HazelcastConfiguration$$EnhancerBySpringCGLIB$$c7da65f3.CGLIB$getHazelCastServerInstance$0()
在net.kyivstar.upa.sdc.config.HazelcastConfiguration$$EnhancerBySpringCGLIB$$c7da65f3$$FastClassBySpringCGLIB$$b920d5ef.invoke()
你如何解决这个问题?如何运行集成测试?instanceName配置元素用于创建命名的Hazelcast成员,并且对于JVM中的每个Hazelcast实例都应该是唯一的。在您的情况下,要么您应该为同一JVM中的每个
HazelcastInstance
bean创建设置不同的实例名称,要么您可以完全删除instanceName
配置,如果您不使用实例名称调用实例。在运行我的测试时遇到了相同的问题。在我的例子中,原因是,spring测试框架试图启动新的上下文,同时缓存旧的上下文-从而尝试创建另一个具有相同名称的hazelcast实例,而其中一个已经在缓存的上下文中
一旦TestContext框架加载ApplicationContext(或
WebApplicationContext)对于测试,该上下文被缓存和重用
对于声明相同唯一上下文的所有后续测试
同一测试套件中的配置
阅读本文以了解有关spring测试框架如何管理测试上下文的更多信息。
我目前正在研究解决方案,稍后将发布。我能看到的一个可能的解决方案是在每次测试后使用
@DirtiesContext(classMode=DirtiesContext.classMode.after_CLASS)
删除测试上下文,但这在性能方面非常昂贵。我遇到了同样的问题,我通过检查实例是否已经存在来解决它:
@Bean
public CacheManager cacheManager() {
HazelcastInstance existingInstance = Hazelcast.getHazelcastInstanceByName(CACHE_INSTANCE_NAME);
HazelcastInstance hazelcastInstance = null != existingInstance
? existingInstance
: Hazelcast.newHazelcastInstance(hazelCastConfig());
return new HazelcastCacheManager(hazelcastInstance);
}
您可以看到代码的其余部分