...
Micronaut is a microservice microservice framework for building modular applications. This develops a module to configure and setup ignite to work with Micronaut and it's associated API's.
AutoConfiguration
IgniteClientConfiguration
Configuration to load Ignite instance.
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
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
client-mode: true
discovery-spi:
static-ip-finder:
enabled: true
addresses:
- "127.0.0.1:47500"
- "127.0.0.1:47501" |
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 |
---|
|
@Singleton
public class IgniteConfigurationFactoryInterceptor implements BeanCreatedEventListener<DefaultIgniteConfiguration> {
@Override
public DefaultIgniteConfiguration onCreated(BeanCreatedEvent<DefaultIgniteConfiguration> event |
Code Block |
---|
language | java |
---|
title | IgniteClientConfiguration |
---|
collapse | true |
---|
|
/**
* Ignite cache configuration.
*/
@EachProperty(value = IgniteClientConfiguration.PREFIX + ".clients", primary = "default")
public class IgniteClientConfiguration implements Named {
public static final String PREFIX = "ignite";
private final String name;
private String path;
/**
* @param name Name or key of the client.
*/
public IgniteClientConfiguration(@Parameter String name) {
this.name = name;
}
/**
* path to load in bean configuration.
*
* @param path bean config to load.
*/
public void setPath(String path) {
this.path = path;
}
/**
* path to load in bean configuration.
*
* @return bean config to load.
*/
public String getPath() {
return pathDefaultIgniteConfiguration configuration = event.getBean();
}
/**
* @return name or key for client
*/
@NonNull
@Override
public String getName() {configuration.setIgniteInstanceName("instance-a");
return this.nameconfiguration;
}
}
|
...
Ignite Cache Configuration
Code Block |
---|
language | yml |
---|
title | example config |
---|
|
ignite:
enabled: true
clients:
default:cache-configurations:
name:
group-name: string
onheap-cache: boolean
sql-onheap-cache: boolean
patheager-ttl: "classpath:a.cfg"
other: boolean
cache-mode: LOCAL | REPLICATED | PARTITIONED
rebalance-mode: SYNC | ASYNC | NONE
path: "classpath:b.cfg" |
- default is the primary bean
- path loads in a spring bean definition
Application factory configuration
A bean for the ignite instance can be provided from a factory.
...
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 IgniteInitial configurationFactory(){
@Singleton
@Bean
@Primary@IgnitePrimary
@Named("default")
IgniteConfiguration defaultConfigurationCacheConfiguration<String,String> cacheInstance() {
IgniteConfigurationreturn cfgnew = new IgniteConfiguration();
// The node will be started as a client node.
CacheConfiguration<String, String>("my-cache")
cfg.setClientModesetBackups(true10);
// Setting up an IP Finder to ensure the client can locate the servers.
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();}
}
|
Ignite Thin Clients - WIP
Code Block |
---|
language | yml |
---|
title | Thin Client Configuration |
---|
|
ignite-thin-client:
enabled: true
addresses:
ipFinder.setAddresses(Collections.singletonList("localhost:47500..47509"));- "127.0.0.1:10800"
cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
return cfg;
}
}
|
Micronaut Caching API
Micronaut Ignite Cache
Micronaut cache Micronaut cache configuration is covered under snapshot documentation.
Annotations
- CachePut
- PutOperations
- Cacheable
- CacheInvalidate
- InvalidateOperations
- CacheConfig
Micronaut by default will use By default, the primary Ignite instance is used for Micronaut cache and use the DynamicCacheManager where the key key from for each cache operation will refer to the name of the IgniteCache instance.
Example
Code Block |
---|
language | java |
---|
title | Example Cacheable ObjectIgniteCacheManager |
---|
collapse | true |
---|
|
@Singleton
@CacheConfig('counter')
staticpublic class CounterServiceIgniteCacheManager {
implements Map<String, Integer> counters = new LinkedHashMap<>()
Map<String, Integer> counters2 = new LinkedHashMap<>()
int incrementNoCache(String name) DynamicCacheManager<IgniteCache> {
private final int value = counters.computeIfAbsent(name, { 0 })Ignite ignite;
private final counters.put(name, ++value)ConversionService<?> service;
private final return value
}ExecutorService executorService;
@CachePut
int increment(String name) {
int value = counters.computeIfAbsent(name, { 0 })public IgniteCacheManager(@Primary Ignite ignite,
counters.put(name, ++value)
return value
}
@PutOperations([
@CachePut('counter')ConversionService<?> service,
@CachePut('counter2')
])
int increment2(String name) {
int value = counters2.computeIfAbsent(name, { 0 })
counters2.put(name, ++value)
return value
}
@Cacheable
CompletableFuture<Integer> futureValue(String name) @Named(TaskExecutors.IO) ExecutorService executorService) {
return CompletableFuture.completedFuture(counters.computeIfAbsent(name, { 0 }))this.ignite = ignite;
}
@Cacheable
@SingleResult
Flowable<Integer> flowableValue(String name) {
return Flowable.just(counters.computeIfAbsent(name, { 0 }))this.service = service;
}
@Cacheable
Single<Integer> singleValue(String name) {
return Single.just(counters.computeIfAbsent(name, { 0 }))
this.executorService = executorService;
}
@CachePut@NonNull
CompletableFuture<Integer> futureIncrement(String name) {@Override
public int value = counters.computeIfAbsent(name, { 0 })
counters.put(name, ++value)
return CompletableFuture.completedFuture(value)
}
@Cacheable
int getValue(SyncCache<IgniteCache> getCache(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)
}
}new IgniteSyncCache(service, ignite.getOrCreateCache(name), executorService);
}
}
|
Micronaut Ignite Application
https://github.com/pollend/micronaut-ignite-sample