javascript
pivotal_Spring Data Pivotal Gemfire教程
pivotal
1. Spring Data Pivotal Gemfire –簡介
在這篇文章中,我們將介紹有關Spring Data Pivotal Gemfire的全面教程。 Pivotal Gemfire是由Apache Geode支持的內存中數據網格解決方案。 使用Pivotal Gemfire構建的應用程序使您可以在分布式服務器節點之間輕松擴展系統。 無論分布結構如何,Pivotal Gemfire均可確保數據一致性。 它使應用程序能夠向數百萬用戶提供實時數據。
另一方面,Spring框架是一種廣泛使用的框架,它為構建企業級應用程序提供了基礎。 在本文中,我們將討論Spring數據(Spring框架的眾多模塊之一)如何與Pivotal Gemfire集成,以加快開發過程并將Spring框架的功能引入Pivotal Gemfire應用程序。
2.本教程的先決條件
在進入本教程之前,有必要了解所做的假設以及繼續進行本教程所需的工具。 在此,我假設您了解以下內容:
- 了解訪問關鍵數據
- 對Spring框架的基本了解
- 對Pivotal API調用的基本了解
在整個教程中,我們將使用以下工具和規范:
- JDK 1.8
- 彈簧工具套件/ IntelliJ
- Maven的3.2+
3.開始
首先從項目開始,讓我們創建一個Maven項目并添加以下依賴項。
pom.xml
<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>org.springframework.samples</groupId><artifactId>pivotal_tutorial</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version></parent><properties><spring-shell.version>1.2.0.RELEASE</spring-shell.version></properties><repositories><repository><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></repository></repositories><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-gemfire</artifactId></dependency><dependency><groupId>org.springframework.shell</groupId><artifactId>spring-shell</artifactId><version>${spring-shell.version}</version><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>保存具有這些依賴項的項目,并允許其進行構建。 上面的文件包含必需的Spring Boot依賴關系以及Pivotal Gemfire的Spring Data依賴關系。 一旦項目下載了相關的依賴項,就可以繼續編碼部分。
帶有Pivotal Gemfire的Spring Data幫助我們配置對分布式數據訪問中心的訪問。 這種結合可以幫助我們降低內存訪問量,并使用內存中的緩存級別來維持更好的響應時間。 本教程將帶您完成完整的設置和配置過程。
4.創建一個實體
首先,主要要求是創建一個實體。 讓我們創建一個簡單的Person實體,其中包含一個人的詳細信息,例如其名稱和年齡。 要創建這樣的實體,請使用以下代碼。
PersonEntity.java
package pivotal_tutorial;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 PersonEntity implements Serializable {@Id@Getterprivate final String name;@Getterprivate final int age;@PersistenceConstructorpublic PersonEntity(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;}@Overridepublic String toString() {return String.format("%s is %d years old", getName(), getAge());} }如您所見,有兩個屬性-名稱和年齡以及一個持久性構造函數。 在這里,請注意,該類已使用注釋@Region進行注釋。 此批注是樞軸指示符,用于指示框架使用特定名稱存儲此類的實例。 當它讀取注釋@Region("People") ,它將理解必須將PersonEntity的實例存儲為People的名稱。 用注釋@Id注釋的字段將是實例的唯一鍵。
這里假設您了解Pivotal沒有任何自動密鑰生成系統。 因此,在實際進行數據持久化之前,您需要確保設置了id字段。
5.創建簡單的查詢
與Pivotal Gemfire框架結合的Spring Data就是關于存儲和持久化數據的。 它著重于此管理對數據的訪問。 此外,它還繼承了Spring Data框架的強大功能,例如獲得查詢的功能。 框架的強大之處在于您不再需要學習關鍵的gemfire查詢語言。 您所需要做的就是編寫一些Java代碼段,該框架將在后端構建查詢。
讓我們從為上面顯示的實體創建類似的片段開始。
PersonQueries.java
package pivotal_tutorial; import org.springframework.data.gemfire.repository.query.annotation.Trace; import org.springframework.data.repository.CrudRepository;public interface PersonRepo extends CrudRepository<PersonEntity, String> {@TracePersonEntity findByName(String name);@TraceIterable findByAgeGreaterThan(int age);@TraceIterable findByAgeLessThan(int age);@TraceIterable findByAgeGreaterThanAndAgeLessThan(int greaterThanAge, int lessThanAge); }在上面的代碼中,請注意,該類擴展了CrudRepository ,該類是Spring Data框架提供的類。 注釋@Trace標識需要使用相關的函數來為后端運行的Pivotal Gemfire框架創建查詢。 這些功能很容易理解。 下面提供了簡要說明:
- findByName :通過作為參數提供的name值查找實體
- findByAgeGreaterThan :查找年齡大于提供的值的實體。 返回PersonEntity實例的可迭代列表。
- findAgeLessThan :查找年齡小于提供的值的實體。 返回PersonEntity實例的可迭代列表。
- findByAgeGreaterThanAndLessThan :查找年齡大于或小于提供的值的實體。 返回PersonEntity實例的可迭代列表。
6.創建一個應用程序
現在我們已經準備好查詢和實體,讓我們開始創建有助于數據事務的實際應用程序。 將創建具有視圖的應用程序,以實例化實體并處理數據。 以下代碼創建具有所有必要組件的應用程序。
App.java
package pivotal_tutorial; import static java.util.Arrays.asList; import static java.util.stream.StreamSupport.stream;import java.io.IOException;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", logLevel = "error") @EnableEntityDefinedRegions(basePackageClasses = PersonEntity.class,clientRegionShortcut = ClientRegionShortcut.LOCAL) @EnableGemfireRepositories public class App {public static void main(String[] args) throws IOException {SpringApplication.run(App.class, args);}@BeanApplicationRunner run(PersonRepo personRepository) {return args -> {PersonEntity abk = new PersonEntity("Abhishek Kothari", 26);PersonEntity sumit = new PersonEntity("Sumit Punjabi", 25);PersonEntity john = new PersonEntity("John Doe", 34);System.out.println("Entering into accessing data from Pivotal GemFire framework");asList(abk, sumit, john).forEach(person -> System.out.println("\t" + person));System.out.println("Saving Alice, Bob and Carol to Pivotal GemFire...");personRepository.save(abk);personRepository.save(sumit);personRepository.save(john);System.out.println("Lookup each person by name...");asList(abk.getName(), sumit.getName(), john.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 teens (less than 30):");stream(personRepository.findByAgeLessThan(30).spliterator(), false).forEach(person -> System.out.println("\t" + person));System.out.println("Query teens (between 12 and 30):");stream(personRepository.findByAgeGreaterThanAndAgeLessThan(12, 30).spliterator(), false).forEach(person -> System.out.println("\t" + person));};} }上面的類包含對已定義實體的所有可能的查詢調用。 請注意,在該類中已添加了許多注釋。 下面提供了每個注釋的描述:
@SpringBootApplication :此注釋指定該類將被視為Spring引導應用程序的起點。
@ClientCacheApplication :此批注指定應用程序應在后端使用由Spring Data支持的數據的客戶端緩存。
@EnableDefinedRegions :注釋用于指定需要使用并可用的實體。 該注釋基本上完成了公開該類的相應實體的方法的任務。 @EnableGemfireRepositories :這是最重要的注釋。 從名稱本身可以清楚地看到注釋的目的。 為了在Spring應用程序啟動時啟用gemfire存儲庫,此注釋是必需的。 該注釋將強制掃描當前包,以查找擴展Spring Data倉庫類之一(例如PersonEntity 。
有時候,我們可能不希望將所有Spring Data實體公開到Gemfire框架中。 可以通過顯式指定所需實體擴展的類來防止這種情況。 可以使用其屬性basePackageClasses = TheRepository.class完成此操作
這里要注意,在區域定義中,我們指定了局部區域。 這對于Pivotal Gemfire很重要。 為了存儲數據,Pivotal需要至少1個或更多區域。
7.緩存配置
Pivotal中可能有三種不同的緩存配置。 根據我們計劃使用的區域,我們可以使用所需的批注之一使用Spring Data框架通過Pivotal Gemfire后端指定緩存和數據持久性。 以下是可以使用的三種可能的注釋:
@ClientCacheApplication :在本地存儲中緩存數據客戶端
@PeerCacheApplication :在同級之間緩存數據
@CacheServerApplication :在服務器端緩存數據
Pivotal Gemfire支持多種緩存拓撲,例如客戶端/服務器,對等甚至WAN或LAN安排。 在客戶端/服務器緩存拓撲中,客戶端緩存查詢的數據,而服務器緩存所有數據。 在對等拓撲中,即使網絡中的設備也將緩存數據,以將其提供給最近的對等體。 對于WAN或LAN拓撲,如果您已連接到特定網絡,則設備將緩存數據,并開始將數據分發給其他用戶。 在上述情況下,我們使用了客戶端緩存,因此一旦執行查詢,緩存將完全在客戶端完成。 出于相同的原因,我們指定了LOCAL區域。
我們將實體連接到名為People的區域。 這是使用注釋Region指定的。 該注釋是從Spring Data框架使用的。 稍后使用代碼片段ClientRegionFactoryBean<String, PersonEntity>用于bean定義在應用程序層中映射此區域。 因此,我們注入了一個bean定義,并在People區域中定義了實例,否則,如果沒有Spring Data框架,這是不可能的。
8.存放物件
在本指南中,您將創建三個本地Person對象,Abhishek,Sumit John。 最初,它們僅存在于內存中。 創建它們之后,您必須將它們保存到Pivotal GemFire。
現在,您運行幾個查詢。 第一個按名稱查找所有人。 然后,您將執行一些查詢,以使用age屬性來查找成人,嬰兒和青少年。 打開日志記錄后,您可以看到Spring Data for Pivotal GemFire代表您編寫的查詢。
9.執行代碼并構建一個jar
現在,我們已經了解了代碼的性能以及下一步的時間。 下一步是實際執行代碼,看看代碼如何工作。 要執行代碼,請在您的Spring Tool Suite或IntelliJ中將該應用程序作為Java應用程序運行。 在執行該應用程序時,您將看到類似于以下所示的輸出。 它可能會略有不同,具體取決于您使用的庫的版本。
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.0.3.RELEASE)[info 2018/08/30 20:36:45.110 IST tid=0x1] Starting App on MacBook-Air.local with PID 96473 (/Users/abhishekkothari/Documents/workspace-sts-3.9.5.RELEASE/pivotal_tutorial/target/classes started by abhishekkothari in /Users/abhishekkothari/Documents/workspace-sts-3.9.5.RELEASE/pivotal_tutorial)[info 2018/08/30 20:36:45.118 IST tid=0x1] No active profile set, falling back to default profiles: default[info 2018/08/30 20:36:45.219 IST tid=0x1] Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6c1a5b54: startup date [Thu Aug 30 20:36:45 IST 2018]; root of context hierarchySLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Entering into accessing data from Pivotal GemFire frameworkAbhishek Kothari is 26 years oldSumit Punjabi is 25 years oldJohn Doe is 34 years old Saving Alice, Bob and Carol to Pivotal GemFire... Lookup each person by name...Abhishek Kothari is 26 years oldSumit Punjabi is 25 years oldJohn Doe is 34 years old Query adults (over 18):Sumit Punjabi is 25 years oldJohn Doe is 34 years oldAbhishek Kothari is 26 years old Query teens (less than 30):Sumit Punjabi is 25 years oldAbhishek Kothari is 26 years old Query teens (between 12 and 30):Sumit Punjabi is 25 years oldAbhishek Kothari is 26 years old可以看出,該應用程序已執行,并且提取的lamba函數根據指定的過濾器使用數據。 請注意,這里我們創建了一個完成實體,存儲并檢索它,而沒有真正對Pivotal gemfire數據庫進行任何設置。 這些查詢返回了我們這些實例,而沒有進行任何大的努力。 通過這種方式,Spring Data批注有助于簡化Pivotal Gemfire應用程序的應用程序開發,并幫助您減少從頭開始進行編碼和設置的整個工作量。
為了構建此應用程序并將其導出到其他地方以供遠程使用,您需要做的就是使用Maven來構建應用程序jar。 為此,只需執行以下命令。
./mvnw spring-boot:run上面的命令將構建一個可運行的jar,供您在任何系統中執行。 因此,您可以使用帶有Pivotal Gemfire數據分發的Spring Data Framework輕松構建可移植的應用程序。
10.下載項目
上面已經討論過的STS項目可以在下面的鏈接中找到。
下載您可以在此處下載此示例的完整源代碼: axisal-tutorial.zip
翻譯自: https://www.javacodegeeks.com/2018/08/spring-data-pivotal-gemfire-tutorial.html
pivotal
總結
以上是生活随笔為你收集整理的pivotal_Spring Data Pivotal Gemfire教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 道虽迩读音 道虽迩读音及出处介绍
- 下一篇: layui绑定json_JSON-B非对