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 作為後端進行快取。

廣告

© . All rights reserved.