27 Ağustos 2020 Perşembe

SpringCache HazelcastCacheManager Sınıfı

Giriş
Şu satırı dahil ederiz
import com.hazelcast.spring.cache.HazelcastCacheManager;
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>com.hazelcast</groupId>
  <artifactId>hazelcast-spring</artifactId>
  <version>${hazelcast.version}</version>
  <scope>runtime</scope>
</dependency>
HazelCast Farklı Şekillerde Kullanılabilir
Bunlar şöylee
1. Embedded (Distributed)
2. Client-Server
3. Cloud
4. Sidecar
5.Reverse Proxy
6. Reverse Proxy Sidecar
Embedded (Distributed) kullanım hariç diğerleri konfigürasyon gerektirir.
Konfigürasyon Nerededir
Açıklaması şöyle
Then, you need to add Hazelcast configuration in one of the following manners:

1. Add hazelcast.yaml configuration OR
2. Add hazelcast.xml configuration OR
3 Define @Bean with Hazelcast configuration in the source code
1. yaml
Örnek
Şöyle yaparız
# hazelcast.yaml
hazelcast:
  network:
    join:
      multicast:
        enabled: true
2. XML
Örnek ver

3. Config
Örnek 
Şöyle yaparız
@Bean
public Config hazelCastConfig() {
  Config config = new Config();
  ...
  return config;
}

Örnek - Embedded Distributed
Şöyle yaparız
@Configuration
public class CacheConfiguration {
  @Bean
  CacheManager cacheManager() {
    return new HazelcastCacheManager(Hazelcast.newHazelcastInstance());
  }
}
Örnek
Aynı JVM içinde Hazelcast cluster'ına ait Hazelcast düğümü açmak için şöyle yaparız.
@Configuration
@EnableCaching
public class HazelcastConfig {
  @Bean
  public Config config() {
    Config config = new Config();
    config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
    config.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1:5701");
    return config;
  }

  @Bean
  public HazelcastInstance hazelcastInstance(final Config config) {
    return Hazelcast.getOrCreateHazelcastInstance(config);
  }
}
Örnek - Client-Server
Şöyle yaparız. Burada HazelcastClient.newHazelcastClient() kullanılıyor
@Configuration
@EnableCaching
public class CachingConfiguration extends CachingConfigurerSupport {
  @Bean
  public CacheManager cacheManager() {
    ClientConfig config = new ClientConfig();
    config.getNetworkConfig().addAddress("127.0.0.1:5701");
    HazelcastInstance hInstance = HazelcastClient.newHazelcastClient(config);
    return new HazelcastCacheManager(hInstance);
  }

  @Bean
  public KeyGenerator keyGenerator() {
    return null;
  }
}

Hiç yorum yok:

Yorum Gönder