24 Şubat 2021 Çarşamba

Hazelcast Config Sınıfı

Giriş
Şu satırı dahil ederiz. Bu sınıf kullanılarak Hazelcast cluster'ın bir parçası olan yeni bir düğüm yaratılır. Cluster'a katıldığımız için ismi "In Memory Data Grid"
import com.hazelcast.config.Config;
Bu sınıfla birlitek kullanılan diğer sınıflar şöyle
NetworkConfig : Network ayarları için
MapConfig -> EvictionConfig, TTL ayarları için
addMapConfig metodu
Örnek
Şöyle yaparız. Burada setTimeToLiveSeconds(-1) yapılıyor ancak aslında değerin 0 - Integer.MAX_VALUE arasında olması gerekir. 0 atanırsa TTL değeri yoktur, sonsuza kadar kalsın denilir. MapConfig'e setName() ile verilen isim, Spring kullanırken farklı cache'lere isim ile erişmek için işe yarar
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy; import com.hazelcast.config.MapConfig; import com.hazelcast.config.MaxSizeConfig; @Bean public Config hazelCastConfig(){ Config config = new Config(); config.setInstanceName("hazelcast-instance") .addMapConfig( new MapConfig() .setName("configuration") .setMaxSizeConfig(new MaxSizeConfig(200, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE)) .setEvictionPolicy(EvictionPolicy.LRU) .setTimeToLiveSeconds(-1)); return config; }
Örnek
Elimizde şöyle bir kod olsun
@Bean
public Config hazelCastConfig(){
  return new Config()
    .setInstanceName("hazelcast-instance")
    .addMapConfig(
      new MapConfig()
        .setName("regularly-changed-value-cache")
        .setMaxSizeConfig(new MaxSizeConfig(200,
MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE))
        .setEvictionPolicy(EvictionPolicy.LRU)
        .setTimeToLiveSeconds(0))
    .addMapConfig(
      new MapConfig()
        .setName("irregularly-changed-value-cache")
        .setMaxSizeConfig(new MaxSizeConfig(200,
MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE))
        .setEvictionPolicy(EvictionPolicy.LRU)
        .setTimeToLiveSeconds(0))
  ;
}
Şöyle yaparız
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
@Service
@CacheConfig(cacheNames = "regularly-changed-value-cache")
public class LongStringServiceImpl implements LongStringService {
    
  @CacheEvict(allEntries = true)
  public void clearCache(){}

  @Override
  @Cacheable
  public String changingIrregularly() {
    return "...";
  }
}
getCPSubSystemConfig metodu
Açıklaması şöyle
Please note that Hazelcast IMDG implementation too falls under the AP category of the CAP system. However, strong consistency (even in failure/exceptional cases) is a fundamental requirement for any tasks that require distributed coordination. Hence, there are cases where the existing locks based on map implementation will fail. To address these issues, Hazelcast later came up with the CPSubsystem implementation. CPSubsystem has got a new distributed lock implementation on top of Raft consensus. The CPSubsystem lives alongside AP data structures of the Hazelcast IMDG cluster. CPSubsystem maintains linearizability in all cases, including client and server failures, network partitions, and prevent split-brain situations. In fact, Hazelcast claims that they are the one and only solution which offers a linearizable and distributed lock implementation. 
Örnek
Şöyle yaparız
Config config = new Config();
CPSubsystemConfig cpSubsystemConfig = config.getCPSubSystemConfig(); cpSubsystemConfig.setCPMemberCount(3); cpSubsystemConfig.setGroupSize(3); HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config);
getMapConfigs metodu
Örnek
Şöyle yaparız
Config config = new Config().setClusterName("Sample Hz Cluster");

EvictionConfig evictionConfig = new EvictionConfig()
  .setEvictionPolicy(EvictionPolicy.LRU)
  .setSize(...)
  .setMaxSizePolicy(MaxSizePolicy.PER_NODE);
MapConfig mapConfig = new MapConfig("...")
  .setEvictionConfig(evictionConfig)
  .setTimeToLiveSeconds(...)
  setMaxIdleSeconds(...)
config.getMapConfigs().put("...",mapConfig);
setClusterName metodu
Örnek
Şöyle yaparız
Config config = new Config().setClusterName("Sample Hz Cluster");

setInstanceName metodu
Örnek ver

setManagementCenterConfig metodu
Örnek
Şöyle yaparız
import com.hazelcast.config.Config;
import com.hazelcast.config.ManagementCenterConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

@Bean
public Config hazelCastConfig() {
  return new Config().setManagementCenterConfig(
    new ManagementCenterConfig()
.setEnabled(true)
.setUrl("http://localhost:8080/hazelcast-mancenter"));
}

@Bean
public HazelcastInstance hazelcastInstance(Config hazelCastConfig) {
  return Hazelcast.newHazelcastInstance(hazelCastConfig);
}
setNetworkConfig metodu
NetworkConfig yazısına taşıdım.

setPartitionGroupConfig metodu
Örnek ver

setProperty metodu
Örnek ver

Hiç yorum yok:

Yorum Gönder