Hazelcast 简明教程
Hazelcast - Spring Integration
Hazelcast 支持一种简单的方法来与 Spring Boot 应用程序集成。让我们通过一个示例来理解这一点。
Hazelcast supports an easy way to integrate with Spring Boot application. Let’s try to understand that via an example.
我们将创建一个简单的 API 应用程序,它为获取公司员工信息提供了一个 API。为此,我们将使用 Spring Boot 驱动的 RESTController 以及 Hazelcast 来缓存数据。
We will create a simple API application which provides an API to get employee information for a company. For this purpose, we will use Spring Boot driven RESTController along with Hazelcast for caching data.
请注意,要在 Spring Boot 中集成 Hazelcast,我们需要两件事——
Note that to integrate Hazelcast in Spring Boot, we will need two things −
-
Add Hazelcast as a dependency to our project.
-
Define a configuration (static or programmatic) and make it available to Hazelcast
让我们首先定义 POM。请注意,我们必须指定 Hazelcast JAR 以便在 Spring Boot 项目中使用它。
Let’s first define the POM. Note that we have to specify Hazelcast JAR to use it in the Spring Boot project.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hazelcast</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project to explain Hazelcast integration with Spring Boot</description>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>4.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
另外,将 hazelcast.xml 添加到 src/main/resources 中——
Also add hazelcast.xml to src/main/resources −
<hazelcast
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.12.12.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<instance-name>XML_Hazelcast_Instance</instance-name>
</hazelcast>
定义 Spring Boot 要使用的入口点文件。确保我们指定了 @EnableCaching
Define an entry point file for Spring Boot to use. Ensure that we have @EnableCaching specified −
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
@SpringBootApplication
public class CompanyApplication {
public static void main(String[] args) {
SpringApplication.run(CompanyApplication.class, args);
}
}
让我们定义我们的员工 POJO ——
Let us define our employee POJO −
package com.example.demo;
import java.io.Serializable;
public class Employee implements Serializable{
private static final long serialVersionUID = 1L;
private int empId;
private String name;
private String department;
public Employee(Integer id, String name, String department) {
super();
this.empId = id;
this.name = name;
this.department = department;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", department=" + department + "]";
}
}
最后,让我们定义一个基本的 REST 控制器来访问员工 ——
And ultimately, let us define a basic REST controller to access employee −
package com.example.demo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v1/")
class CompanyApplicationController{
@Cacheable(value = "employee")
@GetMapping("employee/{id}")
public Employee getSubscriber(@PathVariable("id") int id) throws
InterruptedException {
System.out.println("Finding employee information with id " + id + " ...");
Thread.sleep(5000);
return new Employee(id, "John Smith", "CS");
}
}
现在让我们通过运行命令来执行上述应用程序——
Now let us execute the above application, by running the command −
mvn clean install
mvn spring-boot:run
你会注意到命令的输出将包含 Hazelcast 成员信息,这意味着 Hazelcast 实例会使用 hazelcast.xml 配置自动为我们配置。
You will notice that the output of the command would contain Hazelcast member information which mean Hazelcast Instance is automatically configured for us using hazelcast.xml configuration.
Members {size:1, ver:1} [
Member [localhost]:5701 - 91b3df1d-a226-428a-bb74-6eec0a6abb14 this
]
现在让我们通过 curl 执行或使用浏览器来访问 API ——
Now let us execute via curl or use browser to access API −
curl -X GET http://localhost:8080/v1/employee/5
API 的输出将是我们的员工样本。
The output of the API would be our sample employee.
{
"empId": 5,
"name": "John Smith",
"department": "CS"
}
在服务器日志中(即运行 Spring Boot 应用程序的位置),我们看到以下行——
In the server logs (i.e. where Spring Boot application running), we see the following line −
Finding employee information with id 5 ...
但是,请注意,访问信息需要大约 5 秒(因为我们添加了睡眠)。但如果我们再次调用 API,API 的输出将立即显示。这是因为我们指定了 @Cacheable 符号。我们第一次 API 调用的数据已使用 Hazelcast 作为后端缓存。
However, note that it takes almost 5 secs (because of sleep we added) to access the information. But If we call the API again, the output of the API is immediate. This is because we have specified @Cacheable notation. The data of our first API call has been cached using Hazelcast as a backend.