- Hazelcast 教程
- Hazelcast - 首页
- Hazelcast - 简介
- Hazelcast - 设置
- Hazelcast - 第一个应用程序
- Hazelcast - 配置
- 设置多节点实例
- Hazelcast - 数据结构
- Hazelcast - 客户端
- Hazelcast - 序列化
- Hazelcast 高级
- Hazelcast - Spring 集成
- Hazelcast - 监控
- Map Reduce & 聚合
- Hazelcast - 集合监听器
- 常见陷阱 & 性能技巧
- Hazelcast 有用资源
- Hazelcast - 快速指南
- Hazelcast - 有用资源
- Hazelcast - 讨论
Hazelcast - Spring 集成
Hazelcast 支持一种简单的方法来与 Spring Boot 应用程序集成。让我们通过一个示例来了解它。
我们将创建一个简单的 API 应用程序,该应用程序提供一个 API 来获取公司的员工信息。为此,我们将使用 Spring Boot 驱动的 RESTController 以及 Hazelcast 来缓存数据。
请注意,要在 Spring Boot 中集成 Hazelcast,我们需要两件事:
将 Hazelcast 作为依赖项添加到我们的项目中。
定义一个配置(静态或编程)并使其可用于 Hazelcast
让我们首先定义 POM。请注意,我们必须指定 Hazelcast JAR 以在 Spring Boot 项目中使用它。
<?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 中:
<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:
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:
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 控制器来访问员工:
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");
}
}
现在让我们通过运行以下命令来执行上述应用程序:
mvn clean install mvn spring-boot:run
您会注意到命令的输出将包含 Hazelcast 成员信息,这意味着 Hazelcast 实例使用 hazelcast.xml 配置自动为我们配置。
Members {size:1, ver:1} [
Member [localhost]:5701 - 91b3df1d-a226-428a-bb74-6eec0a6abb14 this
]
现在让我们通过 curl 或使用浏览器访问 API:
curl -X GET https://:8080/v1/employee/5
API 的输出将是我们的示例员工。
{
"empId": 5,
"name": "John Smith",
"department": "CS"
}
在服务器日志(即 Spring Boot 应用程序运行的位置)中,我们看到以下行:
Finding employee information with id 5 ...
但是,请注意,访问信息大约需要 5 秒(因为我们添加了 sleep)。但是,如果我们再次调用 API,API 的输出将是即时的。这是因为我们已指定 @Cacheable 注解。我们第一次 API 调用的数据已使用 Hazelcast 作为后端进行缓存。