Skip to content

Instantly share code, notes, and snippets.

@rasheedamir
Last active February 14, 2019 22:24
Show Gist options
  • Save rasheedamir/d0b8e89c02e98a0bb02089e14538c0f8 to your computer and use it in GitHub Desktop.
Save rasheedamir/d0b8e89c02e98a0bb02089e14538c0f8 to your computer and use it in GitHub Desktop.
hazelcast on kubernetes
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.google.common.collect.ImmutableList;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientConnectionStrategyConfig;
import com.hazelcast.client.config.ClientNetworkConfig;
import com.hazelcast.config.DiscoveryConfig;
import com.hazelcast.config.DiscoveryStrategyConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.kubernetes.HazelcastKubernetesDiscoveryStrategyFactory;
@Configuration
@Primary
public class HazelcastConfiguration {
@Bean
public HazelcastInstance hazelcastInstance() {
return HazelcastClient.newHazelcastClient(HazelcastClientConfigFactory.createClientConfig());
}
}
class HazelcastClientConfigFactory {
private static final int CONNECTION_ATTEMPT_PERIOD = 2000;
public static ClientConfig createClientConfig() {
final ClientConfig config = new ClientConfig();
config.getConnectionStrategyConfig()
.setAsyncStart(true)
.setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
config.setProperty("hazelcast.discovery.enabled", "true");
final ClientNetworkConfig networkConfig = new ClientNetworkConfig();
networkConfig.setRedoOperation(false);
networkConfig.setConnectionAttemptLimit(Integer.MAX_VALUE);
networkConfig.setConnectionAttemptPeriod(CONNECTION_ATTEMPT_PERIOD);
final DiscoveryConfig discoveryConfig = new DiscoveryConfig();
final HazelcastKubernetesDiscoveryStrategyFactory factory = new HazelcastKubernetesDiscoveryStrategyFactory();
final DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(factory);
strategyConfig.addProperty("service-dns", "hazelcast");
strategyConfig.addProperty("service-dns-timeout", 10);
discoveryConfig.setDiscoveryStrategyConfigs(ImmutableList.of(strategyConfig));
networkConfig.setDiscoveryConfig(discoveryConfig);
config.setNetworkConfig(networkConfig);
return config;
}
}
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.9.4</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-kubernetes</artifactId>
<version>1.1.0</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment