An Apache Ignite Thick Client is a more feature-rich tool that connects directly to an Apache Ignite cluster, but unlike the Thin Client, it runs a part of the Ignite server software on the client machine.
In simpler terms, imagine the Thick Client as a “full version” of the Apache Ignite software that lets your application interact with the Ignite cluster while also running some of the core functionality locally. This means the client can store data and execute some operations on its own, reducing the reliance on the server for certain tasks.
Because it includes more features, the Thick Client requires more resources (like memory and CPU) than the Thin Client. It’s suitable for cases where you need full control over the Ignite cluster from the client side, like handling large amounts of data or performing complex tasks locally. However, it might not be as lightweight or fast as the Thin Client.
Apache Ignite is a powerful in-memory computing platform that supports both thin and thick clients. In this article, we will explore how to configure and use the Apache Ignite thick client in a Spring Boot application using Java-based configuration.
1. Add Dependencies
To get started, include the necessary Apache Ignite dependencies in your Maven pom.xml
:
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>2.15.0</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>2.15.0</version> <!-- Use the latest version -->
</dependency>
2. Configure Ignite Client
We will use a Java-based configuration to set up the Apache Ignite thick client. Create a configuration class for the Ignite instance:
Ignite Configuration Class
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;
@Configuration
public class IgniteConfig {
@Bean
public Ignite igniteInstance() {
IgniteConfiguration cfg = new IgniteConfiguration();
// Enable client mode
cfg.setClientMode(true);
// Set the Ignite instance name
cfg.setIgniteInstanceName("spring-ignite-client");
// Configure the discovery service
TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi();
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
discoverySpi.setIpFinder(ipFinder);
cfg.setDiscoverySpi(discoverySpi);
// Start the Ignite client
return Ignition.start(cfg);
}
}
This configuration ensures that the Ignite client operates in clientMode
and connects to a cluster using multicast IP discovery.
3. Create a Service to Use Ignite
Next, create a service that interacts with the Ignite cluster. This service will demonstrate basic operations such as creating a cache, inserting data, and querying it.
Ignite Service Class
import org.apache.ignite.Ignite;
import org.apache.ignite.cache.query.ScanQuery;
import org.springframework.stereotype.Service;
import javax.cache.Cache;
@Service
public class IgniteService {
private final Ignite ignite;
public IgniteService(Ignite ignite) {
this.ignite = ignite;
}
public void performOperations() {
// Create or get a cache
var cache = ignite.getOrCreateCache("userCache");
// Put some data into the cache
cache.put(1, "Hello");
cache.put(2, "World");
// Retrieve data from the cache
System.out.println("Key 1: " + cache.get(1));
System.out.println("Key 2: " + cache.get(2));
// Query the cache
cache.query(new ScanQuery<>((key, value) -> true))
.forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
}
}
This service uses Ignite.getOrCreateCache()
to interact with a cache and perform basic CRUD operations.
4. Expose Ignite Operations via a REST Controller
To trigger the Ignite operations, create a REST controller:
Ignite Controller Class
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IgniteController {
private final IgniteService igniteService;
public IgniteController(IgniteService igniteService) {
this.igniteService = igniteService;
}
@GetMapping("/ignite-test")
public String testIgnite() {
igniteService.performOperations();
return "Ignite operations completed.";
}
}
This controller provides an endpoint /ignite-test
that, when accessed, triggers the performOperations
method in IgniteService
.
5. Run and Verify the Application
Steps to Run:
- Start the Apache Ignite server node on your local machine or a cluster.
- Run the Spring Boot application.
- Access the endpoint
http://localhost:8080/ignite-test
.
Expected Output:
The console logs of your Spring Boot application should display the data added to the cache and the results of querying it:
Key 1: Hello
Key 2: World
Key: 1, Value: Hello
Key: 2, Value: World
Conclusion
This article demonstrated how to integrate an Apache Ignite thick client into a Spring Boot application using Java-based configuration. With this setup, you can leverage Ignite’s powerful in-memory data grid and compute capabilities seamlessly in your Spring Boot projects.
Githu repo : https://github.com/dinesharney/ignite-spring-boot-client-thick
Post a Comment