SHARE
TWEET

Untitled

a guest Feb 16th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.context.annotation.Primary;
  4. import com.google.common.collect.ImmutableList;
  5. import com.hazelcast.client.HazelcastClient;
  6. import com.hazelcast.client.config.ClientConfig;
  7. import com.hazelcast.client.config.ClientConnectionStrategyConfig;
  8. import com.hazelcast.client.config.ClientNetworkConfig;
  9. import com.hazelcast.config.DiscoveryConfig;
  10. import com.hazelcast.config.DiscoveryStrategyConfig;
  11. import com.hazelcast.core.HazelcastInstance;
  12. import com.hazelcast.kubernetes.HazelcastKubernetesDiscoveryStrategyFactory;
  13.  
  14. @Configuration
  15. @Primary
  16. public class HazelcastConfiguration {
  17.  
  18.     @Bean
  19.     public HazelcastInstance hazelcastInstance() {
  20.         return HazelcastClient.newHazelcastClient(HazelcastClientConfigFactory.createClientConfig());
  21.     }
  22. }
  23.  
  24. class HazelcastClientConfigFactory {
  25.  
  26.     private static final int CONNECTION_ATTEMPT_PERIOD = 2000;
  27.    
  28.     public static ClientConfig createClientConfig() {
  29.  
  30.         final ClientConfig config = new ClientConfig();
  31.         config.getConnectionStrategyConfig()
  32.                 .setAsyncStart(true)
  33.                 .setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
  34.  
  35.         config.setProperty("hazelcast.discovery.enabled", "true");
  36.  
  37.         final ClientNetworkConfig networkConfig = new ClientNetworkConfig();
  38.  
  39.         networkConfig.setRedoOperation(false);
  40.         networkConfig.setConnectionAttemptLimit(Integer.MAX_VALUE);
  41.         networkConfig.setConnectionAttemptPeriod(CONNECTION_ATTEMPT_PERIOD);
  42.  
  43.         final DiscoveryConfig discoveryConfig = new DiscoveryConfig();
  44.         final HazelcastKubernetesDiscoveryStrategyFactory factory = new HazelcastKubernetesDiscoveryStrategyFactory();
  45.  
  46.         final DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(factory);
  47.         strategyConfig.addProperty("service-dns", "hazelcast");
  48.         strategyConfig.addProperty("service-dns-timeout", 10);
  49.  
  50.         discoveryConfig.setDiscoveryStrategyConfigs(ImmutableList.of(strategyConfig));
  51.         networkConfig.setDiscoveryConfig(discoveryConfig);
  52.         config.setNetworkConfig(networkConfig);
  53.  
  54.         return config;
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top