構建應用程序的過程Apache Geode數據管理系統

2022-12-22 10:14:24 來源:51CTO博客

本指南將引導您完成構建應用程序的過程Apache Geode數據管理系統。

您將構建什么

您將使用Apache Geode 的春季數據存儲和檢索 POJO。

你需要什么

約15分鐘最喜歡的文本編輯器或 IDEJDK 1.8或以后格拉德爾 4+?或梅文 3.2+您也可以將代碼直接導入到 IDE 中:彈簧工具套件 (STS)智能理念VSCode

如何完成本指南

像大多數春天一樣入門指南,您可以從頭開始并完成每個步驟,也可以繞過您已經熟悉的基本設置步驟。無論哪種方式,您最終都會得到工作代碼。


(資料圖片)

要從頭開始,請繼續從 Spring 初始化開始.

要跳過基礎知識,請執行以下操作:

下載?并解壓縮本指南的源存儲庫,或使用吉特:git clonehttps://github.com/spring-guides/gs-accessing-data-gemfire.git光盤成gs-accessing-data-gemfire/initial跳轉到定義簡單實體.

完成后,您可以根據 中的代碼檢查結果。??gs-accessing-data-gemfire/complete??

從 Spring 初始化開始

對于所有 Spring 應用程序,您應該從Spring Initializr.Spring Initializr 提供了一種快速的方式來提取應用程序所需的所有依賴項,并為您完成大量設置。這個例子需要 Spring for Apache Geode 依賴項。

你可以使用這個預初始化項目,然后單擊生成以下載 ZIP 文件。此項目配置為適合本教程中的示例。

手動初始化項目:

在 Web 瀏覽器中,導航到https://start.spring.io.此服務拉入應用程序所需的所有依賴項,并為您完成大部分設置。選擇 Gradle 或 Maven 以及您要使用的語言。本指南假定您選擇了 Java。單擊依賴關系,然后選擇Spring for Apache Geode。單擊生成。下載生成的 ZIP 文件,該文件是配置了您選擇的 Web 應用程序的存檔。

如果您的 IDE 集成了 Spring Initializr,則可以從 IDE 完成此過程。

您也可以從 Github 分叉項目,然后在 IDE 或其他編輯器中打開它。

定義簡單實體

Apache Geode 是一個內存數據網格 (IMDG),用于將數據映射到區域。您可以配置分布式區域,以便在群集中的多個節點之間對數據進行分區和復制。但是,在本指南中,我們使用了一個區域,因此您無需設置任何額外的內容,例如整個服務器集群。??LOCAL??

Apache Geode 是一個鍵/值存儲,一個區域實現接口。雖然你可以把一個區域看作一個,它比一個簡單的Java要復雜得多,因為數據是在區域內分發、復制和管理的。??java.util.concurrent.ConcurrentMap????java.util.Map????Map??

在此示例中,僅使用幾個注釋將對象存儲在 Apache Geode(一個區域)中。??Person??

??src/main/java/hello/Person.java??

package hello;import java.io.Serializable;import org.springframework.data.annotation.Id;import org.springframework.data.annotation.PersistenceConstructor;import org.springframework.data.gemfire.mapping.annotation.Region;import lombok.Getter;@Region(value = "People")public class Person implements Serializable {  @Id  @Getter  private final String name;  @Getter  private final int age;  @PersistenceConstructor  public Person(String name, int age) {    this.name = name;    this.age = age;  }  @Override  public String toString() {    return String.format("%s is %d years old", getName(), getAge());  }}

這里有一個包含兩個字段的類:和 。您還有一個持久性構造函數,用于在創建新實例時填充實體。該類使用??Person????name????age??龍目島計劃以簡化實施。

請注意,此類用 .當 Apache Geode 存儲此類的實例時,會在區域內創建一個新條目。此類還用 標記字段。這表示用于識別和跟蹤 Apache Geode 內部數據的標識符。本質上,帶注釋的字段(例如)是鍵,實例是鍵/值條目中的值。Apache Geode 中沒有自動生成密鑰,因此您必須在將實體保存到 Apache Geode 之前設置 ID(該)。??@Region("People")????People????name????@Id????Person????@Id????name????Person????name??

下一個重要的部分是人的年齡。在本指南的后面部分,我們將使用它來設計一些查詢。

重寫的方法打印出人員的姓名和年齡。??toString()??

創建簡單查詢

Spring Data for Apache Geode 專注于使用 Spring 在 Apache Geode 中存儲和訪問數據。它還繼承了Spring Data Commons項目的強大功能,例如派生查詢的能力。從本質上講,您不需要學習Apache Geode(OQL)的查詢語言。您可以編寫一些方法,框架會為您編寫查詢。

要查看其工作原理,請創建一個接口來查詢存儲在 Apache Geode 中的對象:??Person??

??src/main/java/hello/PersonRepository.java??

package hello;import org.springframework.data.gemfire.repository.query.annotation.Trace;import org.springframework.data.repository.CrudRepository;public interface PersonRepository extends CrudRepository {  @Trace  Person findByName(String name);  @Trace  Iterable findByAgeGreaterThan(int age);  @Trace  Iterable findByAgeLessThan(int age);  @Trace  Iterable findByAgeGreaterThanAndAgeLessThan(int greaterThanAge, int lessThanAge);}

??PersonRepository??從 Spring 數據共享擴展接口,并為存儲庫使用的值和 ID(鍵)指定通用類型參數的類型(分別為 和 )。此接口附帶許多操作,包括基本的 CRUD(創建、讀取、更新、刪除)和簡單的查詢數據訪問操作(例如 )。??CrudRepository????Person????String????findById(..)??

您可以根據需要通過聲明其方法簽名來定義其他查詢。在這種情況下,我們添加 ,它本質上搜索類型的對象并找到與 .??findByName????Person????name??

您還有:

??findByAgeGreaterThan??:尋找一定年齡以上的人??findByAgeLessThan??:查找未滿一定年齡的人??findByAgeGreaterThanAndAgeLessThan??:尋找特定年齡段的人

讓我們把它連接起來,看看它是什么樣子的!

創建應用程序類

下面的示例創建一個包含所有組件的應用程序類:

??src/main/java/hello/Application.java??

package hello;import static java.util.Arrays.asList;import static java.util.stream.StreamSupport.stream;import org.apache.geode.cache.client.ClientRegionShortcut;import org.springframework.boot.ApplicationRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions;import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;@SpringBootApplication@ClientCacheApplication(name = "AccessingDataGemFireApplication")@EnableEntityDefinedRegions(  basePackageClasses = Person.class,  clientRegionShortcut = ClientRegionShortcut.LOCAL)@EnableGemfireRepositoriespublic class Application {  public static void main(String[] args) {    SpringApplication.run(Application.class, args);  }  @Bean  ApplicationRunner run(PersonRepository personRepository) {    return args -> {      Person alice = new Person("Adult Alice", 40);      Person bob = new Person("Baby Bob", 1);      Person carol = new Person("Teen Carol", 13);      System.out.println("Before accessing data in Apache Geode...");      asList(alice, bob, carol).forEach(person -> System.out.println("\t" + person));      System.out.println("Saving Alice, Bob and Carol to Pivotal GemFire...");      personRepository.save(alice);      personRepository.save(bob);      personRepository.save(carol);      System.out.println("Lookup each person by name...");      asList(alice.getName(), bob.getName(), carol.getName())        .forEach(name -> System.out.println("\t" + personRepository.findByName(name)));      System.out.println("Query adults (over 18):");      stream(personRepository.findByAgeGreaterThan(18).spliterator(), false)        .forEach(person -> System.out.println("\t" + person));      System.out.println("Query babies (less than 5):");      stream(personRepository.findByAgeLessThan(5).spliterator(), false)        .forEach(person -> System.out.println("\t" + person));      System.out.println("Query teens (between 12 and 20):");      stream(personRepository.findByAgeGreaterThanAndAgeLessThan(12, 20).spliterator(), false)        .forEach(person -> System.out.println("\t" + person));    };  }}

在配置中,您需要添加注釋。??@EnableGemfireRepositories??

默認情況下,掃描當前包以查找擴展 Spring Data 的存儲庫接口之一的任何接口。您可以使用它安全地告訴 Spring Data for Apache Geode 按類型掃描不同的根包,以查找特定于應用程序的存儲庫擴展。@EnableGemfireRepositoriesbasePackageClasses = MyRepository.class

需要包含一個或多個區域的 Apache Geode 緩存來存儲所有數據。為此,您可以使用 Spring Data 之一進行 Apache Geode 方便的基于配置的注釋:、 或 。??@ClientCacheApplication????@PeerCacheApplication????@CacheServerApplication??

Apache Geode支持不同的緩存拓撲,例如客戶端/服務器,點對點(p2p),甚至WAN安排。在 p2p 中,對等緩存實例嵌入在應用程序中,您的應用程序將能夠作為對等緩存成員參與集群。但是,您的應用程序受到集群中作為對等成員的所有約束,因此這不像客戶端/服務器拓撲那樣常用。

在我們的例子中,我們使用創建一個“客戶端”緩存實例,該實例能夠連接到服務器集群并與之通信。但是,為簡單起見,客戶端使用客戶端區域在本地存儲數據,而無需設置或運行任何服務器。??@ClientCacheApplication????LOCAL??

現在,請記住您如何標記要存儲在使用 SDG 映射注釋調用的區域中,?您可以使用 Bean 定義在此處定義該區域。您需要注入剛剛定義的緩存實例,同時還要命名它。??Person????People????@Region("People")????ClientRegionFactoryBean????People??

Apache Geode 緩存實例(無論是對等方還是客戶端)只是存儲數據的區域的容器。您可以將緩存視為 RDBMS 中的架構,將區域視為表。但是,緩存還執行其他管理功能來控制和管理您的所有區域。

類型為 ,將鍵類型 () 與值類型 () 匹配。??????String????Person??

該方法使用 Spring Boot 啟動應用程序并調用(另一個 Bean 定義),該定義使用應用程序的 Spring 數據存儲庫在 Apache Geode 上執行數據訪問操作。??public static void main????SpringApplication.run()????ApplicationRunner??

應用程序會自動連接您剛剛定義的實例。Spring Data for Apache Geode 動態創建一個具體類來實現此接口并插入所需的查詢代碼以滿足接口的義務。該方法使用此存儲庫實例來演示功能。??PersonRepository????run()??

存儲和獲取數據

在本指南中,您將創建三個本地對象:AliceBaby BobTeen Carol。最初,它們僅存在于內存中。創建它們后,您必須將它們保存到Apache Geode。??Person??

現在,您可以運行多個查詢。第一個按名稱查找每個人。然后,您可以使用 age 屬性運行一些查詢來查找成人、嬰兒和青少年。打開日志記錄后,您可以看到 Spring Data for Apache Geode 代表您寫入的查詢。

要查看由 SDG 生成的 Apache Geode OQL 查詢,請將注釋屬性更改為 。由于查詢方法(例如)是使用 SDG 的注釋進行注釋的,因此這將打開 Apache Geode 的 OQL 查詢跟蹤(查詢級日志記錄),它會顯示生成的 OQL、執行時間、查詢是否使用任何 Apache Geode 索引來收集結果以及查詢返回的行數。??@ClientCacheApplication????logLevel????config????findByName????@Trace??

構建可執行的 JAR

您可以使用 Gradle 或 Maven 從命令行運行應用程序。您還可以構建一個包含所有必需依賴項、類和資源的可執行 JAR 文件并運行該文件。通過構建可執行 jar,可以輕松地在整個開發生命周期中跨不同環境等將服務作為應用程序進行交付、版本控制和部署。

如果使用 Gradle,則可以使用 .或者,您可以使用 JAR 文件生成 JAR 文件,然后運行該文件,如下所示:??./gradlew bootRun????./gradlew build??

java -jar build/libs/gs-accessing-data-gemfire-0.1.0.jar

如果使用 Maven,則可以使用 運行應用程序。或者,您可以使用 JAR 文件生成 JAR 文件,然后運行該文件,如下所示:??./mvnw spring-boot:run????./mvnw clean package??

java -jar target/gs-accessing-data-gemfire-0.1.0.jar

此處描述的步驟將創建一個可運行的 JAR。你也可以構建經典 WAR 文件.

您應該看到類似以下內容(包含其他內容,例如查詢):

Before linking up with {apache-geode-name}...  Alice is 40 years old.  Baby Bob is 1 years old.  Teen Carol is 13 years old.Lookup each person by name...  Alice is 40 years old.  Baby Bob is 1 years old.  Teen Carol is 13 years old.Adults (over 18):  Alice is 40 years old.Babies (less than 5):  Baby Bob is 1 years old.Teens (between 12 and 20):  Teen Carol is 13 years old.

總結

祝賀!您設置了 Apache Geode 緩存客戶端,存儲了簡單的實體,并開發了快速查詢。

標簽: 應用程序 數據存儲 一定年齡

上一篇:Argocd/Argocd Rolloouts/Argocd-cli/kubectl argo rollouts插件部署
下一篇:環球快看點丨使用 Spring 創建“Hello, World”超媒體驅動的 REST Web 服務