转载

Hazelcast Spring集成

Hazelcast 支持一种与 Spring Boot 应用程序集成的简单方法。让我们试着通过一个例子来理解这一点。
我们将创建一个简单的 API 应用程序,该应用程序提供用于获取公司员工信息的 API。为此,我们将使用 Spring Boot 驱动的 RESTController 和 Hazelcast 来缓存数据。
请注意,要将 Hazelcast 集成到 Spring Boot 中,我们需要做两件事-
将 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.xml 配置为我们自动配置了 Hazelcast 实例。
Members {size:1, ver:1} [
   Member [localhost]:5701-91b3df1d-a226-428a-bb74-6eec0a6abb14 this
]
现在让我们通过 curl 执行或使用浏览器访问 API-
curl-X GET http://localhost:8080/v1/employee/5
API 的输出将是我们的示例应用嗯。
{
   "empId": 5,
   "name": "John Smith",
   "department": "CS"
}
在服务器日志(即 Spring Boot 应用程序运行的地方)中,我们看到以下行-
Finding employee information with id 5 ...
但是,请注意,访问信息需要将近 5 秒(因为我们添加了睡眠)。但是如果我们再次调用 API,API 的输出是立即的。这是因为我们指定了@Cacheable 表示法。我们第一个 API 调用的数据已使用 Hazelcast 作为后端进行缓存。
正文到此结束
Loading...