Move Hazelcast Docker Compose support into spring-boot-hazelcast

This commit is contained in:
Phillip Webb
2025-05-19 19:28:29 -07:00
parent f2361db02a
commit 0519564fec
10 changed files with 9 additions and 7 deletions

View File

@@ -1,66 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.docker.compose.service.connection.hazelcast;
import java.util.UUID;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.hazelcast.autoconfigure.HazelcastConnectionDetails;
import org.springframework.boot.testsupport.container.TestImage;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link HazelcastDockerComposeConnectionDetailsFactory}.
*
* @author Dmytro Nosan
*/
class HazelcastDockerComposeConnectionDetailsFactoryIntegrationTests {
@DockerComposeTest(composeFile = "hazelcast-compose.yaml", image = TestImage.HAZELCAST)
void runCreatesConnectionDetails(HazelcastConnectionDetails connectionDetails) {
ClientConfig config = connectionDetails.getClientConfig();
assertThat(config.getClusterName()).isEqualTo(Config.DEFAULT_CLUSTER_NAME);
verifyConnection(config);
}
@DockerComposeTest(composeFile = "hazelcast-cluster-name-compose.yaml", image = TestImage.HAZELCAST)
void runCreatesConnectionDetailsCustomClusterName(HazelcastConnectionDetails connectionDetails) {
ClientConfig config = connectionDetails.getClientConfig();
assertThat(config.getClusterName()).isEqualTo("spring-boot");
verifyConnection(config);
}
private static void verifyConnection(ClientConfig config) {
HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient(config);
try {
IMap<String, String> map = hazelcastInstance.getMap(UUID.randomUUID().toString());
map.put("docker", "compose");
assertThat(map.get("docker")).isEqualTo("compose");
}
finally {
hazelcastInstance.shutdown();
}
}
}

View File

@@ -1,7 +0,0 @@
services:
hazelcast:
image: '{imageName}'
environment:
HZ_CLUSTERNAME: "spring-boot"
ports:
- '5701'

View File

@@ -1,78 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.docker.compose.service.connection.hazelcast;
import com.hazelcast.client.config.ClientConfig;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.hazelcast.autoconfigure.HazelcastConnectionDetails;
/**
* {@link DockerComposeConnectionDetailsFactory} to create
* {@link HazelcastConnectionDetails} for a {@code hazelcast} service.
*
* @author Dmytro Nosan
*/
class HazelcastDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<HazelcastConnectionDetails> {
private static final int DEFAULT_PORT = 5701;
protected HazelcastDockerComposeConnectionDetailsFactory() {
super("hazelcast/hazelcast", "com.hazelcast.client.config.ClientConfig");
}
@Override
protected HazelcastConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new HazelcastDockerComposeConnectionDetails(source.getRunningService());
}
/**
* {@link HazelcastConnectionDetails} backed by a {@code hazelcast}
* {@link RunningService}.
*/
static class HazelcastDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements HazelcastConnectionDetails {
private final String host;
private final int port;
private final HazelcastEnvironment environment;
HazelcastDockerComposeConnectionDetails(RunningService service) {
super(service);
this.host = service.host();
this.port = service.ports().get(DEFAULT_PORT);
this.environment = new HazelcastEnvironment(service.env());
}
@Override
public ClientConfig getClientConfig() {
ClientConfig config = new ClientConfig();
if (this.environment.getClusterName() != null) {
config.setClusterName(this.environment.getClusterName());
}
config.getNetworkConfig().addAddress(this.host + ":" + this.port);
return config;
}
}
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.docker.compose.service.connection.hazelcast;
import java.util.Map;
/**
* Hazelcast environment details.
*
* @author Dmytro Nosan
*/
class HazelcastEnvironment {
private final String clusterName;
HazelcastEnvironment(Map<String, String> env) {
this.clusterName = env.get("HZ_CLUSTERNAME");
}
String getClusterName() {
return this.clusterName;
}
}

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* 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
*
* https://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.
*/
/**
* Auto-configuration for Docker Compose Hazelcast service connections.
*/
package org.springframework.boot.docker.compose.service.connection.hazelcast;

View File

@@ -1,7 +1,6 @@
# Connection Details Factories
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
org.springframework.boot.docker.compose.service.connection.clickhouse.ClickHouseR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.hazelcast.HazelcastDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.ldap.LLdapDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.ldap.OpenLdapDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbR2dbcDockerComposeConnectionDetailsFactory,\

View File

@@ -1,45 +0,0 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.docker.compose.service.connection.hazelcast;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link HazelcastEnvironment}.
*
* @author Dmytro Nosan
*/
class HazelcastEnvironmentTests {
@Test
void getClusterNameWhenHasNoHzClusterNameSet() {
HazelcastEnvironment environment = new HazelcastEnvironment(Collections.emptyMap());
assertThat(environment.getClusterName()).isNull();
}
@Test
void getClusterNameWhenHzClusterNameSet() {
HazelcastEnvironment environment = new HazelcastEnvironment(Map.of("HZ_CLUSTERNAME", "spring-boot"));
assertThat(environment.getClusterName()).isEqualTo("spring-boot");
}
}