Add service connection support for Hazelcast
See gh-42416
This commit is contained in:
committed by
Moritz Halbritter
parent
fb3dd68dd3
commit
cee7439dbe
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.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.autoconfigure.hazelcast.HazelcastConnectionDetails;
|
||||
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
services:
|
||||
hazelcast:
|
||||
image: '{imageName}'
|
||||
environment:
|
||||
HZ_CLUSTERNAME: "spring-boot"
|
||||
ports:
|
||||
- '5701'
|
||||
@@ -0,0 +1,5 @@
|
||||
services:
|
||||
hazelcast:
|
||||
image: '{imageName}'
|
||||
ports:
|
||||
- '5701'
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 com.hazelcast.client.config.ClientConfig;
|
||||
|
||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastConnectionDetails;
|
||||
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;
|
||||
|
||||
/**
|
||||
* {@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();
|
||||
this.environment.getClusterName().ifPresent(config::setClusterName);
|
||||
config.getNetworkConfig().addAddress(this.host + ":" + this.port);
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Hazelcast environment details.
|
||||
*
|
||||
* @author Dmytro Nosan
|
||||
*/
|
||||
class HazelcastEnvironment {
|
||||
|
||||
private final String clusterName;
|
||||
|
||||
HazelcastEnvironment(Map<String, String> env) {
|
||||
this.clusterName = env.get("HZ_CLUSTERNAME");
|
||||
}
|
||||
|
||||
Optional<String> getClusterName() {
|
||||
return Optional.ofNullable(this.clusterName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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;
|
||||
@@ -11,6 +11,7 @@ org.springframework.boot.docker.compose.service.connection.activemq.ArtemisDocke
|
||||
org.springframework.boot.docker.compose.service.connection.cassandra.CassandraDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.elasticsearch.ElasticsearchDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.flyway.JdbcAdaptingFlywayConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.hazelcast.HazelcastDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.ldap.OpenLdapDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.liquibase.JdbcAdaptingLiquibaseConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbJdbcDockerComposeConnectionDetailsFactory,\
|
||||
@@ -20,8 +21,8 @@ org.springframework.boot.docker.compose.service.connection.mysql.MySqlJdbcDocker
|
||||
org.springframework.boot.docker.compose.service.connection.mysql.MySqlR2dbcDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.neo4j.Neo4jDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeJdbcDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeJdbcDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeR2dbcDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeJdbcDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeR2dbcDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryLoggingDockerComposeConnectionDetailsFactory,\
|
||||
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryMetricsDockerComposeConnectionDetailsFactory,\
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getClusterNameWhenHzClusterNameSet() {
|
||||
HazelcastEnvironment environment = new HazelcastEnvironment(Map.of("HZ_CLUSTERNAME", "spring-boot"));
|
||||
assertThat(environment.getClusterName()).isNotEmpty().hasValue("spring-boot");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user