日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

pivotal_Spring Data Pivotal Gemfire教程

發(fā)布時(shí)間:2023/12/3 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pivotal_Spring Data Pivotal Gemfire教程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

pivotal

1. Spring Data Pivotal Gemfire –簡(jiǎn)介

在這篇文章中,我們將介紹有關(guān)Spring Data Pivotal Gemfire的全面教程。 Pivotal Gemfire是由Apache Geode支持的內(nèi)存中數(shù)據(jù)網(wǎng)格解決方案。 使用Pivotal Gemfire構(gòu)建的應(yīng)用程序使您可以在分布式服務(wù)器節(jié)點(diǎn)之間輕松擴(kuò)展系統(tǒng)。 無(wú)論分布結(jié)構(gòu)如何,Pivotal Gemfire均可確保數(shù)據(jù)一致性。 它使應(yīng)用程序能夠向數(shù)百萬(wàn)用戶提供實(shí)時(shí)數(shù)據(jù)。
另一方面,Spring框架是一種廣泛使用的框架,它為構(gòu)建企業(yè)級(jí)應(yīng)用程序提供了基礎(chǔ)。 在本文中,我們將討論Spring數(shù)據(jù)(Spring框架的眾多模塊之一)如何與Pivotal Gemfire集成,以加快開發(fā)過程并將Spring框架的功能引入Pivotal Gemfire應(yīng)用程序。

2.本教程的先決條件

在進(jìn)入本教程之前,有必要了解所做的假設(shè)以及繼續(xù)進(jìn)行本教程所需的工具。 在此,我假設(shè)您了解以下內(nèi)容:

  • 了解訪問關(guān)鍵數(shù)據(jù)
  • 對(duì)Spring框架的基本了解
  • 對(duì)Pivotal API調(diào)用的基本了解

在整個(gè)教程中,我們將使用以下工具和規(guī)范:

  • JDK 1.8
  • 彈簧工具套件/ IntelliJ
  • Maven的3.2+

3.開始

首先從項(xiàng)目開始,讓我們創(chuàng)建一個(gè)Maven項(xiàng)目并添加以下依賴項(xiàng)。

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>

保存具有這些依賴項(xiàng)的項(xiàng)目,并允許其進(jìn)行構(gòu)建。 上面的文件包含必需的Spring Boot依賴關(guān)系以及Pivotal Gemfire的Spring Data依賴關(guān)系。 一旦項(xiàng)目下載了相關(guān)的依賴項(xiàng),就可以繼續(xù)編碼部分。

帶有Pivotal Gemfire的Spring Data幫助我們配置對(duì)分布式數(shù)據(jù)訪問中心的訪問。 這種結(jié)合可以幫助我們降低內(nèi)存訪問量,并使用內(nèi)存中的緩存級(jí)別來維持更好的響應(yīng)時(shí)間。 本教程將帶您完成完整的設(shè)置和配置過程。

4.創(chuàng)建一個(gè)實(shí)體

首先,主要要求是創(chuàng)建一個(gè)實(shí)體。 讓我們創(chuàng)建一個(gè)簡(jiǎn)單的Person實(shí)體,其中包含一個(gè)人的詳細(xì)信息,例如其名稱和年齡。 要?jiǎng)?chuàng)建這樣的實(shí)體,請(qǐng)使用以下代碼。

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());} }

如您所見,有兩個(gè)屬性-名稱和年齡以及一個(gè)持久性構(gòu)造函數(shù)。 在這里,請(qǐng)注意,該類已使用注釋@Region進(jìn)行注釋。 此批注是樞軸指示符,用于指示框架使用特定名稱存儲(chǔ)此類的實(shí)例。 當(dāng)它讀取注釋@Region("People") ,它將理解必須將PersonEntity的實(shí)例存儲(chǔ)為People的名稱。 用注釋@Id注釋的字段將是實(shí)例的唯一鍵。

這里假設(shè)您了解Pivotal沒有任何自動(dòng)密鑰生成系統(tǒng)。 因此,在實(shí)際進(jìn)行數(shù)據(jù)持久化之前,您需要確保設(shè)置了id字段。

5.創(chuàng)建簡(jiǎn)單的查詢

與Pivotal Gemfire框架結(jié)合的Spring Data就是關(guān)于存儲(chǔ)和持久化數(shù)據(jù)的。 它著重于此管理對(duì)數(shù)據(jù)的訪問。 此外,它還繼承了Spring Data框架的強(qiáng)大功能,例如獲得查詢的功能。 框架的強(qiáng)大之處在于您不再需要學(xué)習(xí)關(guān)鍵的gemfire查詢語(yǔ)言。 您所需要做的就是編寫一些Java代碼段,該框架將在后端構(gòu)建查詢。
讓我們從為上面顯示的實(shí)體創(chuàng)建類似的片段開始。

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); }

在上面的代碼中,請(qǐng)注意,該類擴(kuò)展了CrudRepository ,該類是Spring Data框架提供的類。 注釋@Trace標(biāo)識(shí)需要使用相關(guān)的函數(shù)來為后端運(yùn)行的Pivotal Gemfire框架創(chuàng)建查詢。 這些功能很容易理解。 下面提供了簡(jiǎn)要說明:

  • findByName :通過作為參數(shù)提供的name值查找實(shí)體
  • findByAgeGreaterThan :查找年齡大于提供的值的實(shí)體。 返回PersonEntity實(shí)例的可迭代列表。
  • findAgeLessThan :查找年齡小于提供的值的實(shí)體。 返回PersonEntity實(shí)例的可迭代列表。
  • findByAgeGreaterThanAndLessThan :查找年齡大于或小于提供的值的實(shí)體。 返回PersonEntity實(shí)例的可迭代列表。

6.創(chuàng)建一個(gè)應(yīng)用程序

現(xiàn)在我們已經(jīng)準(zhǔn)備好查詢和實(shí)體,讓我們開始創(chuàng)建有助于數(shù)據(jù)事務(wù)的實(shí)際應(yīng)用程序。 將創(chuàng)建具有視圖的應(yīng)用程序,以實(shí)例化實(shí)體并處理數(shù)據(jù)。 以下代碼創(chuàng)建具有所有必要組件的應(yīng)用程序。

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));};} }

上面的類包含對(duì)已定義實(shí)體的所有可能的查詢調(diào)用。 請(qǐng)注意,在該類中已添加了許多注釋。 下面提供了每個(gè)注釋的描述:

@SpringBootApplication :此注釋指定該類將被視為Spring引導(dǎo)應(yīng)用程序的起點(diǎn)。
@ClientCacheApplication :此批注指定應(yīng)用程序應(yīng)在后端使用由Spring Data支持的數(shù)據(jù)的客戶端緩存。
@EnableDefinedRegions :注釋用于指定需要使用并可用的實(shí)體。 該注釋基本上完成了公開該類的相應(yīng)實(shí)體的方法的任務(wù)。 @EnableGemfireRepositories :這是最重要的注釋。 從名稱本身可以清楚地看到注釋的目的。 為了在Spring應(yīng)用程序啟動(dòng)時(shí)啟用gemfire存儲(chǔ)庫(kù),此注釋是必需的。 該注釋將強(qiáng)制掃描當(dāng)前包,以查找擴(kuò)展Spring Data倉(cāng)庫(kù)類之一(例如PersonEntity 。

有時(shí)候,我們可能不希望將所有Spring Data實(shí)體公開到Gemfire框架中。 可以通過顯式指定所需實(shí)體擴(kuò)展的類來防止這種情況。 可以使用其屬性basePackageClasses = TheRepository.class完成此操作
這里要注意,在區(qū)域定義中,我們指定了局部區(qū)域。 這對(duì)于Pivotal Gemfire很重要。 為了存儲(chǔ)數(shù)據(jù),Pivotal需要至少1個(gè)或更多區(qū)域。

7.緩存配置

Pivotal中可能有三種不同的緩存配置。 根據(jù)我們計(jì)劃使用的區(qū)域,我們可以使用所需的批注之一使用Spring Data框架通過Pivotal Gemfire后端指定緩存和數(shù)據(jù)持久性。 以下是可以使用的三種可能的注釋:

@ClientCacheApplication :在本地存儲(chǔ)中緩存數(shù)據(jù)客戶端
@PeerCacheApplication :在同級(jí)之間緩存數(shù)據(jù)
@CacheServerApplication :在服務(wù)器端緩存數(shù)據(jù)

Pivotal Gemfire支持多種緩存拓?fù)?#xff0c;例如客戶端/服務(wù)器,對(duì)等甚至WAN或LAN安排。 在客戶端/服務(wù)器緩存拓?fù)渲?#xff0c;客戶端緩存查詢的數(shù)據(jù),而服務(wù)器緩存所有數(shù)據(jù)。 在對(duì)等拓?fù)渲?#xff0c;即使網(wǎng)絡(luò)中的設(shè)備也將緩存數(shù)據(jù),以將其提供給最近的對(duì)等體。 對(duì)于WAN或LAN拓?fù)?#xff0c;如果您已連接到特定網(wǎng)絡(luò),則設(shè)備將緩存數(shù)據(jù),并開始將數(shù)據(jù)分發(fā)給其他用戶。 在上述情況下,我們使用了客戶端緩存,因此一旦執(zhí)行查詢,緩存將完全在客戶端完成。 出于相同的原因,我們指定了LOCAL區(qū)域。

我們將實(shí)體連接到名為People的區(qū)域。 這是使用注釋Region指定的。 該注釋是從Spring Data框架使用的。 稍后使用代碼片段ClientRegionFactoryBean<String, PersonEntity>用于bean定義在應(yīng)用程序?qū)又杏成浯藚^(qū)域。 因此,我們注入了一個(gè)bean定義,并在People區(qū)域中定義了實(shí)例,否則,如果沒有Spring Data框架,這是不可能的。

8.存放物件

在本指南中,您將創(chuàng)建三個(gè)本地Person對(duì)象,Abhishek,Sumit John。 最初,它們僅存在于內(nèi)存中。 創(chuàng)建它們之后,您必須將它們保存到Pivotal GemFire。

現(xiàn)在,您運(yùn)行幾個(gè)查詢。 第一個(gè)按名稱查找所有人。 然后,您將執(zhí)行一些查詢,以使用age屬性來查找成人,嬰兒和青少年。 打開日志記錄后,您可以看到Spring Data for Pivotal GemFire代表您編寫的查詢。

9.執(zhí)行代碼并構(gòu)建一個(gè)jar

現(xiàn)在,我們已經(jīng)了解了代碼的性能以及下一步的時(shí)間。 下一步是實(shí)際執(zhí)行代碼,看看代碼如何工作。 要執(zhí)行代碼,請(qǐng)?jiān)谀腟pring Tool Suite或IntelliJ中將該應(yīng)用程序作為Java應(yīng)用程序運(yùn)行。 在執(zhí)行該應(yīng)用程序時(shí),您將看到類似于以下所示的輸出。 它可能會(huì)略有不同,具體取決于您使用的庫(kù)的版本。

. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: 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

可以看出,該應(yīng)用程序已執(zhí)行,并且提取的lamba函數(shù)根據(jù)指定的過濾器使用數(shù)據(jù)。 請(qǐng)注意,這里我們創(chuàng)建了一個(gè)完成實(shí)體,存儲(chǔ)并檢索它,而沒有真正對(duì)Pivotal gemfire數(shù)據(jù)庫(kù)進(jìn)行任何設(shè)置。 這些查詢返回了我們這些實(shí)例,而沒有進(jìn)行任何大的努力。 通過這種方式,Spring Data批注有助于簡(jiǎn)化Pivotal Gemfire應(yīng)用程序的應(yīng)用程序開發(fā),并幫助您減少?gòu)念^開始進(jìn)行編碼和設(shè)置的整個(gè)工作量。

為了構(gòu)建此應(yīng)用程序并將其導(dǎo)出到其他地方以供遠(yuǎn)程使用,您需要做的就是使用Maven來構(gòu)建應(yīng)用程序jar。 為此,只需執(zhí)行以下命令。

./mvnw spring-boot:run

上面的命令將構(gòu)建一個(gè)可運(yùn)行的jar,供您在任何系統(tǒng)中執(zhí)行。 因此,您可以使用帶有Pivotal Gemfire數(shù)據(jù)分發(fā)的Spring Data Framework輕松構(gòu)建可移植的應(yīng)用程序。

10.下載項(xiàng)目

上面已經(jīng)討論過的STS項(xiàng)目可以在下面的鏈接中找到。

下載
您可以在此處下載此示例的完整源代碼: axisal-tutorial.zip

翻譯自: https://www.javacodegeeks.com/2018/08/spring-data-pivotal-gemfire-tutorial.html

pivotal

總結(jié)

以上是生活随笔為你收集整理的pivotal_Spring Data Pivotal Gemfire教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。