Design
Micronaut is a microservice framework for building moudlar modular applications. This develops a configuration that bests works with Micronaut.
Documentation: https://micronaut-projects.github.io/micronaut-ignite/snapshot/guide/
Releases: https://github.com/micronaut-projects/micronaut-ignite/releases
Auto-Configuration
Application yaml configuration.
Micronaut can span an instance of Ignite automatically. Below we review several configuration options.
Ignite Thick Clients
Ignite Thick Client Bean Configuration
A thick client bean can be configured via the application properties file as follows:
Code Block |
---|
language | yml |
---|
title | example config |
---|
|
ignite:
enabled: true
comunicationclient-mode: true
discovery-spi:
localstatic-ip-portfinder: 5555
cache-configurationsenabled: true
accountsaddresses:
table-name: ACCOUNTS- "127.0.0.1:47500"
key-type: String
books:
table-name: BOOKS
key-type: String
|
- default is the primary bean
- path loads in a spring bean definition
Ignite factory configuration
A bean for an IgniteConfiguration can be provided from a factory.
Instead of the static IP finder, you can enable the Kubernetes one if a Micronaut app is deployed in a K8S environment:
Code Block |
---|
language | yml |
---|
title | example config |
---|
|
ignite:
enabled: true
client-mode: true
discovery-spi:
kubernetes-ip-finder:
enabled: true
namespace: default
service-name: ignite
|
Additional configuration can be applied on top of the default configuration by intercepting the bean when its created, Using the BeanCreatedEventListener.
...
Code Block |
---|
|
@Factory
@Singleton
public class MyFactoryIgniteConfigurationFactoryInterceptor {
implements BeanCreatedEventListener<DefaultIgniteConfiguration> @Singleton{
@Bean@Override
@Primary
public DefaultIgniteConfiguration @Named("default")
IgniteConfiguration defaultConfiguration(onCreated(BeanCreatedEvent<DefaultIgniteConfiguration> event) {
IgniteConfigurationDefaultIgniteConfiguration cfgconfiguration = new IgniteConfigurationevent.getBean();
// The node will be started as a client node.
cfg.setClientMode(true);
// Setting up an IP Finder to ensure the client can locate the servers.
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList("localhost:47500..47509"));
configuration.setIgniteInstanceName("instance-a");
return configuration;
}
}
|
Ignite Cache Configuration
Code Block |
---|
language | yml |
---|
title | example config |
---|
|
ignite:
enabled: true
cache-configurations:
name:
group-name: string
onheap-cache: boolean
sql-onheap-cache: boolean
eager-ttl: boolean
cache-mode: LOCAL | REPLICATED | PARTITIONED
rebalance-mode: SYNC | ASYNC | NONE
data-region-name: string
key-type: java.Object
value-type: java.Object
second:
group-name: two
|
Code Block |
---|
language | java |
---|
title | example config |
---|
|
@Factory
public class configurationFactory(){
@Singleton
@Bean
@IgnitePrimary
CacheConfiguration<String,String> cacheInstance() {
return new CacheConfiguration<String, String>("my-cache")
cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder) .setBackups(10);
}
}
|
Ignite Thin Clients - WIP
Code Block |
---|
language | yml |
---|
title | Thin Client Configuration |
---|
|
ignite-thin-client:
enabled: true
return cfg;addresses:
}
}
- "127.0.0.1:10800"
- "127.0.0.1:10801" |
Micronaut Ignite Cache
Micronaut cache cache is covered under snapshot documentation. By default, the primary Ignite instance is used for Micronaut cache and the key from for each cache operation will refer to the IgniteCache instance.
Code Block |
---|
language | java |
---|
title | IgniteCacheManager |
---|
collapse | true |
---|
|
@Singleton
public class IgniteCacheManager implements DynamicCacheManager<IgniteCache> {
private final Ignite ignite;
private final ConversionService<?> service;
private final ExecutorService executorService;
public IgniteCacheManager(@Primary Ignite ignite,
ConversionService<?> service,
@Named(TaskExecutors.IO) ExecutorService executorService) {
this.ignite = ignite;
this.service = service;
this.executorService = executorService;
}
@NonNull
@Override
public SyncCache<IgniteCache> getCache(String name) {
return new IgniteSyncCache(service, ignite.getOrCreateCache(name), executorService);
}
}
|
...
This is an example of a service using Micronaut Cache.
Code Block |
---|
language | java |
---|
title | Example Cacheable Object |
---|
collapse | true |
---|
|
@Singleton
@CacheConfig('counter')
static class CounterService {
Map<String, Integer> counters = new LinkedHashMap<>()
Map<String, Integer> counters2 = new LinkedHashMap<>()
int incrementNoCache(String name) {
int value = counters.computeIfAbsent(name, { 0 })
counters.put(name, ++value)
return value
}
@CachePut
int increment(String name) {
int value = counters.computeIfAbsent(name, { 0 })
counters.put(name, ++value)
return value
}
@PutOperations([
@CachePut('counter'),
@CachePut('counter2')
])
int increment2(String name) {
int value = counters2.computeIfAbsent(name, { 0 })
counters2.put(name, ++value)
return value
}
@Cacheable
CompletableFuture<Integer> futureValue(String name) {
return CompletableFuture.completedFuture(counters.computeIfAbsent(name, { 0 }))
}
@Cacheable
@SingleResult
Flowable<Integer> flowableValue(String name) {
return Flowable.just(counters.computeIfAbsent(name, { 0 }))
}
@Cacheable
Single<Integer> singleValue(String name) {
return Single.just(counters.computeIfAbsent(name, { 0 }))
}
@CachePut
CompletableFuture<Integer> futureIncrement(String name) {
int value = counters.computeIfAbsent(name, { 0 })
counters.put(name, ++value)
return CompletableFuture.completedFuture(value)
}
@Cacheable
int getValue(String name) {
return counters.computeIfAbsent(name, { 0 })
}
@Cacheable('counter2')
int getValue2(String name) {
return counters2.computeIfAbsent(name, { 0 })
}
@Cacheable
Optional<Integer> getOptionalValue(String name) {
return Optional.ofNullable(counters.get(name))
}
@CacheInvalidate(all = true)
void reset() {
counters.clear()
}
@CacheInvalidate
void reset(String name) {
counters.remove(name)
}
@InvalidateOperations([
@CacheInvalidate('counter'),
@CacheInvalidate('counter2')
])
void reset2(String name) {
counters.remove(name)
}
@CacheInvalidate(parameters = 'name')
void set(String name, int val) {
counters.put(name, val)
}
} |
Micronaut Ignite Application
https://github.com/pollend/micronaut-ignite-sample
...