日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Spring認(rèn)證指南:了解如何在GemFire中緩存數(shù)據(jù)

本指南演練了使用阿帕奇大地的數(shù)據(jù)管理系統(tǒng),用于緩存應(yīng)用程序代碼中的某些調(diào)用。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括張灣網(wǎng)站建設(shè)、張灣網(wǎng)站制作、張灣網(wǎng)頁(yè)制作以及張灣網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,張灣網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到張灣省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

有關(guān)Apache Geode概念和從Apache Geode訪問(wèn)數(shù)據(jù)的更多一般知識(shí),請(qǐng)閱讀指南,使用 Apache Geode 訪問(wèn)數(shù)據(jù)。

您將構(gòu)建的內(nèi)容

您將構(gòu)建一個(gè)服務(wù),該服務(wù)從CloudFoundry托管的報(bào)價(jià)服務(wù)請(qǐng)求報(bào)價(jià),并將其緩存在Apache Geode中。

然后,您將看到再次獲取相同的報(bào)價(jià)消除了對(duì)報(bào)價(jià)服務(wù)的昂貴調(diào)用,因?yàn)镾pring的緩存抽象由Apache Geode支持,將用于緩存結(jié)果,給定相同的請(qǐng)求。

報(bào)價(jià)服務(wù)位于:

https://quoters.apps.pcfone.io。

報(bào)價(jià)服務(wù)具有以下 API:

GET /api         - get all quotes
GET /api/random - get random quote
GET /api/{id} - get specific quote

您將需要什么

  1. 約15分鐘。
  2. 喜歡的文本編輯器或 IDE。
  3. JDK 1.8或更高版本。
  4. 格拉德?tīng)?4+或梅文 3.2+。
  5. 您還可以將代碼直接導(dǎo)入到 IDE 中:
  • 彈簧工具套件 (STS)。
  • IntelliJ IDEA。

如何完成本指南

像大多數(shù)春天一樣入門(mén)指南,您可以從頭開(kāi)始并完成每個(gè)步驟,也可以繞過(guò)您已經(jīng)熟悉的基本設(shè)置步驟。無(wú)論哪種方式,你最終都會(huì)得到工作代碼。

要從頭開(kāi)始,請(qǐng)轉(zhuǎn)到從 Spring Initializr 開(kāi)始。

要跳過(guò)基礎(chǔ)知識(shí),請(qǐng)執(zhí)行以下操作:

  • 下載并解壓縮本指南的源存儲(chǔ)庫(kù),或使用Git:git clone 。 https://github.com/spring-guides/gs-caching-gemfire.git。
  • 光盤(pán)成gs-caching-gemfire/initial。
  • 跳到創(chuàng)建用于提取數(shù)據(jù)的可綁定對(duì)象。

完成后,您可以根據(jù) 中的代碼檢查結(jié)果。

gs-caching-gemfire/complete。

從 Spring Initializr 開(kāi)始

對(duì)于所有Spring應(yīng)用程序,您應(yīng)該從Spring Initializr.Spring Initializr提供了一種快速的方法來(lái)提取應(yīng)用程序所需的所有依賴項(xiàng),并為您完成許多設(shè)置。此示例需要"Apache Geode 的 Spring for Apache Geode"依賴項(xiàng)。

以下清單顯示了使用 Maven 時(shí)的示例文件:pom.xml。


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

org.springframework.boot
spring-boot-starter-parent
2.5.6

org.springframework
gs-caching-gemfire
0.1.0

1.2.0.RELEASE



javax.cache
cache-api
runtime


org.springframework.boot
spring-boot-starter


org.springframework.data
spring-data-geode


com.fasterxml.jackson.core
jackson-databind


org.projectlombok
lombok


org.springframework.shell
spring-shell
${spring-shell.version}
runtime





org.springframework.boot
spring-boot-maven-plugin




以下清單顯示了使用 Gradle 時(shí)的示例文件:build.gradle。

plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'io.freefair.lombok' version '6.3.0'
id 'java'
}
apply plugin: 'eclipse'
apply plugin: 'idea'
group = "org.springframework"
version = "0.1.0"
sourceCompatibility = '1.8'

repositories {
mavenCentral()
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter"
implementation "org.springframework.data:spring-data-geode"
implementation "com.fasterxml.jackson.core:jackson-databind"
implementation "org.projectlombok:lombok"
runtimeOnly "javax.cache:cache-api"
runtimeOnly "org.springframework.shell:spring-shell:1.2.0.RELEASE"
}

創(chuàng)建用于提取數(shù)據(jù)的可綁定對(duì)象

現(xiàn)在,您已經(jīng)設(shè)置了項(xiàng)目并生成系統(tǒng),您可以專(zhuān)注于定義捕獲從 Quote 服務(wù)提取引號(hào)(數(shù)據(jù))所需的位所需的域?qū)ο蟆?/p>

src/main/java/hello/Quote.java。

package hello;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.util.ObjectUtils;
import lombok.Data;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("unused")
public class Quote {
private Long id;
private String quote;
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Quote)) {
return false;
}
Quote that = (Quote) obj;
return ObjectUtils.nullSafeEquals(this.getId(), that.getId());
}
@Override
public int hashCode() {
int hashValue = 17;
hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getId());
return hashValue;
}
@Override
public String toString() {
return getQuote();
}
}COPY

域類(lèi)具有 和 屬性。這些是您將在本指南中進(jìn)一步收集的兩個(gè)主要屬性。通過(guò)使用QuoteidquoteQuote龍目島項(xiàng)目。

此外,還會(huì)捕獲報(bào)價(jià)服務(wù)在報(bào)價(jià)請(qǐng)求中發(fā)送的響應(yīng)的整個(gè)有效負(fù)載。它包括請(qǐng)求的(又名 )以及 .此類(lèi)還使用

QuoteQuoteResponsestatustypequote龍目島項(xiàng)目以簡(jiǎn)化實(shí)現(xiàn)。

src/main/java/hello/QuoteResponse.java。

package hello;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class QuoteResponse {
@JsonProperty("value")
private Quote quote;
@JsonProperty("type")
private String status;
@Override
public String toString() {
return String.format("{ @type = %1$s, quote = '%2$s', status = %3$s }",
getClass().getName(), getQuote(), getStatus());
}
}COPY

報(bào)價(jià)服務(wù)的典型響應(yīng)如下所示:

{
"type":"success",
"value": {
"id":1,
"quote":"Working with Spring Boot is like pair-programming with the Spring developers."
}
}COPY

這兩個(gè)類(lèi)都標(biāo)有 。這意味著即使可以檢索其他 JSON 屬性,它們也會(huì)被忽略。@JsonIgnoreProperties(ignoreUnknown=true)。

查詢數(shù)據(jù)報(bào)價(jià)服務(wù)

下一步是創(chuàng)建一個(gè)查詢報(bào)價(jià)的服務(wù)類(lèi)。

src/main/java/hello/QuoteService.java。

package hello;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@SuppressWarnings("unused")
@Service
public class QuoteService {
protected static final String ID_BASED_QUOTE_SERVICE_URL = "https://quoters.apps.pcfone.io/api/{id}";
protected static final String RANDOM_QUOTE_SERVICE_URL = "https://quoters.apps.pcfone.io/api/random";
private volatile boolean cacheMiss = false;
private final RestTemplate quoteServiceTemplate = new RestTemplate();
/**
* Determines whether the previous service method invocation resulted in a cache miss.
*
* @return a boolean value indicating whether the previous service method invocation resulted in a cache miss.
*/
public boolean isCacheMiss() {
boolean cacheMiss = this.cacheMiss;
this.cacheMiss = false;
return cacheMiss;
}
protected void setCacheMiss() {
this.cacheMiss = true;
}
/**
* Requests a quote with the given identifier.
*
* @param id the identifier of the {@link Quote} to request.
* @return a {@link Quote} with the given ID.
*/
@Cacheable("Quotes")
public Quote requestQuote(Long id) {
setCacheMiss();
return requestQuote(ID_BASED_QUOTE_SERVICE_URL, Collections.singletonMap("id", id));
}
/**
* Requests a random quote.
*
* @return a random {@link Quote}.
*/
@CachePut(cacheNames = "Quotes", key = "#result.id")
public Quote requestRandomQuote() {
setCacheMiss();
return requestQuote(RANDOM_QUOTE_SERVICE_URL);
}
protected Quote requestQuote(String URL) {
return requestQuote(URL, Collections.emptyMap());
}
protected Quote requestQuote(String URL, Map urlVariables) {
return Optional.ofNullable(this.quoteServiceTemplate.getForObject(URL, QuoteResponse.class, urlVariables))
.map(QuoteResponse::getQuote)
.orElse(null);
}
}COPY

使用 Spring 的來(lái)查詢報(bào)價(jià)服務(wù)的QuoteServiceRestTemplate應(yīng)用程序接口.Quote 服務(wù)返回一個(gè) JSON 對(duì)象,但 Spring 使用 Jackson 將數(shù)據(jù)綁定到一個(gè)對(duì)象,并最終綁定到一個(gè)對(duì)象。QuoteResponseQuote。

此服務(wù)類(lèi)的關(guān)鍵部分是如何使用 進(jìn)行注釋。requestQuote@Cacheable("Quotes")Spring's Caching Abstraction截獲調(diào)用 以檢查服務(wù)方法是否已被調(diào)用。如果是這樣,Spring的緩存抽象只返回緩存的副本。否則,Spring 將繼續(xù)調(diào)用該方法,將響應(yīng)存儲(chǔ)在緩存中,然后將結(jié)果返回給調(diào)用方。requestQuote。

我們還在服務(wù)方法上使用了注釋。由于從此服務(wù)方法調(diào)用返回的報(bào)價(jià)將是隨機(jī)的,因此我們不知道將收到哪個(gè)報(bào)價(jià)。因此,我們不能在調(diào)用之前查閱緩存(即),但我們可以緩存調(diào)用的結(jié)果,這將對(duì)后續(xù)調(diào)用產(chǎn)生積極影響,假設(shè)感興趣的報(bào)價(jià)是之前隨機(jī)選擇和緩存的。@CachePutrequestRandomQuoteQuotesrequestQuote(id)。

使用 SpEL 表達(dá)式 ("#result.id") 訪問(wèn)服務(wù)方法調(diào)用的結(jié)果,并檢索要用作緩存鍵的 ID。您可以了解有關(guān)Spring的Cache Abstraction SpEL上下文的更多信息@CachePutQuote這里。

您必須提供緩存的名稱(chēng)。出于演示目的,我們將其命名為"報(bào)價(jià)",但在生產(chǎn)中,建議選擇一個(gè)適當(dāng)?shù)拿枋鲂悦Q(chēng)。這也意味著不同的方法可以與不同的緩存相關(guān)聯(lián)。如果每個(gè)緩存具有不同的配置設(shè)置(如不同的過(guò)期或逐出策略等),這將非常有用。

稍后,當(dāng)您運(yùn)行代碼時(shí),您將看到運(yùn)行每個(gè)調(diào)用所需的時(shí)間,并能夠辨別緩存對(duì)服務(wù)響應(yīng)時(shí)間的影響。這演示了緩存某些調(diào)用的價(jià)值。如果您的應(yīng)用程序不斷查找相同的數(shù)據(jù),則緩存結(jié)果可以顯著提高性能。

使應(yīng)用程序可執(zhí)行

盡管 Apache Geode 緩存可以嵌入到 Web 應(yīng)用程序和 WAR 文件中,但下面演示的更簡(jiǎn)單方法會(huì)創(chuàng)建一個(gè)獨(dú)立的應(yīng)用程序。您將所有內(nèi)容打包到一個(gè)可執(zhí)行的JAR文件中,由一個(gè)很好的舊Java方法驅(qū)動(dòng)。main()。

src/main/java/hello/Application.java。

package hello;
import java.util.Optional;
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.cache.config.EnableGemfireCaching;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.config.annotation.EnableCachingDefinedRegions;
@SpringBootApplication
@ClientCacheApplication(name = "CachingGemFireApplication")
@EnableCachingDefinedRegions(clientRegionShortcut = ClientRegionShortcut.LOCAL)
@EnableGemfireCaching
@SuppressWarnings("unused")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
ApplicationRunner runner(QuoteService quoteService) {
return args -> {
Quote quote = requestQuote(quoteService, 12L);
requestQuote(quoteService, quote.getId());
requestQuote(quoteService, 10L);
};
}
private Quote requestQuote(QuoteService quoteService, Long id) {
long startTime = System.currentTimeMillis();
Quote quote = Optional.ofNullable(id)
.map(quoteService::requestQuote)
.orElseGet(quoteService::requestRandomQuote);
long elapsedTime = System.currentTimeMillis();
System.out.printf("\"%1$s\"%nCache Miss [%2$s] - Elapsed Time [%3$s ms]%n", quote,
quoteService.isCacheMiss(), (elapsedTime - startTime));
return quote;
}
}COPY

@SpringBootApplication是添加以下所有內(nèi)容的便利注釋?zhuān)?/p>

  • @Configuration:將類(lèi)標(biāo)記為應(yīng)用程序上下文的 Bean 定義的源。
  • @EnableAutoConfiguration:告訴 Spring Boot 根據(jù)類(lèi)路徑設(shè)置、其他 Bean 和各種屬性設(shè)置開(kāi)始添加 Bean。例如,如果在類(lèi)路徑上,則此批注將應(yīng)用程序標(biāo)記為 Web 應(yīng)用程序并激活關(guān)鍵行為,如設(shè)置 .spring-webmvcDispatcherServlet。
  • @ComponentScan:告訴Spring在軟件包中查找其他組件、配置和服務(wù),讓它找到控制器。hello。

該方法使用Spring Boot的方法啟動(dòng)應(yīng)用程序。您是否注意到?jīng)]有一行 XML?也沒(méi)有文件。此Web應(yīng)用程序是100%純Java,您不必處理配置任何管道或基礎(chǔ)架構(gòu)。main()SpringApplication.run()web.xml。

配置的頂部是一個(gè)至關(guān)重要的注釋?zhuān)哼@將打開(kāi)緩存(即使用Spring的注釋進(jìn)行元注釋),并在后臺(tái)聲明其他重要的bean,以支持使用Apache Geode作為緩存提供程序的緩存。@EnableGemfireCaching@EnableCaching。

第一個(gè) Bean 是用于訪問(wèn) Quotes REST-ful Web 服務(wù)的實(shí)例。QuoteService。

另外兩個(gè)用于緩存報(bào)價(jià)并執(zhí)行應(yīng)用程序的操作。

  • quotesRegion在緩存中定義一個(gè) Apache Geode 客戶端區(qū)域來(lái)存儲(chǔ)報(bào)價(jià)。它被特別命名為"報(bào)價(jià)",以匹配我們方法的用法。LOCAL@Cacheable("Quotes")QuoteService。
  • runner是用于運(yùn)行應(yīng)用程序的 Spring Boot 接口的一個(gè)實(shí)例。ApplicationRunner。

第一次請(qǐng)求報(bào)價(jià)(使用)時(shí),會(huì)發(fā)生緩存未命中,并且將調(diào)用服務(wù)方法,從而產(chǎn)生明顯的延遲,該延遲不會(huì)接近于零毫秒。在這種情況下,緩存由服務(wù)方法 的輸入?yún)?shù)(即 )鏈接。換句話說(shuō),方法參數(shù)是緩存鍵。對(duì) ID 標(biāo)識(shí)的相同報(bào)價(jià)的后續(xù)請(qǐng)求將導(dǎo)致緩存命中,從而避免昂貴的服務(wù)調(diào)用。requestQuote(id)idrequestQuoteid。

出于演示目的,對(duì) 的調(diào)用被包裝在一個(gè)單獨(dú)的方法(類(lèi)內(nèi)部)中,以捕獲進(jìn)行服務(wù)調(diào)用的時(shí)間。這使您可以確切地查看任何一個(gè)請(qǐng)求需要多長(zhǎng)時(shí)間。

QuoteServicerequestQuoteApplication。

構(gòu)建可執(zhí)行的 JAR

您可以使用 Gradle 或 Maven 從命令行運(yùn)行應(yīng)用程序。您還可以構(gòu)建一個(gè)包含所有必要依賴項(xiàng)、類(lèi)和資源的可執(zhí)行 JAR 文件并運(yùn)行該文件。通過(guò)構(gòu)建可執(zhí)行 jar,可以輕松地在整個(gè)開(kāi)發(fā)生命周期中跨不同環(huán)境等將服務(wù)作為應(yīng)用程序進(jìn)行交付、版本控制和部署。

如果您使用 Gradle,則可以使用 運(yùn)行應(yīng)用程序。或者,您可以使用 JAR 文件構(gòu)建 JAR 文件,然后運(yùn)行該 JAR 文件,如下所示:./gradlew bootRun./gradlew build。

java -jar build/libs/gs-caching-gemfire-0.1.0.jar

如果使用 Maven,則可以使用 運(yùn)行應(yīng)用程序。或者,您可以使用 JAR 文件構(gòu)建 JAR 文件,然后運(yùn)行該 JAR 文件,如下所示:./mvnw spring-boot:run./mvnw clean package。

java -jar target/gs-caching-gemfire-0.1.0.jar

此處描述的步驟將創(chuàng)建一個(gè)可運(yùn)行的 JAR。您還可以構(gòu)建經(jīng)典 WAR 文件。

將顯示日志記錄輸出。該服務(wù)應(yīng)在幾秒鐘內(nèi)啟動(dòng)并運(yùn)行。

"@springboot with @springframework is pure productivity! Who said in #java one has to write double the code than in other langs? #newFavLib"
Cache Miss [true] - Elapsed Time [776 ms]
"@springboot with @springframework is pure productivity! Who said in #java one has to write double the code than in other langs? #newFavLib"
Cache Miss [false] - Elapsed Time [0 ms]
"Really loving Spring Boot, makes stand alone Spring apps easy."
Cache Miss [true] - Elapsed Time [96 ms]

從中可以看出,第一次調(diào)用報(bào)價(jià)服務(wù)以獲取報(bào)價(jià)需要 776 毫秒,并導(dǎo)致緩存未命中。但是,請(qǐng)求相同報(bào)價(jià)的第二個(gè)調(diào)用花費(fèi)了 0 毫秒,并導(dǎo)致緩存命中。這清楚地表明,第二個(gè)調(diào)用已被緩存,并且從未實(shí)際命中報(bào)價(jià)服務(wù)。但是,當(dāng)對(duì)特定的非緩存報(bào)價(jià)請(qǐng)求進(jìn)行最終服務(wù)調(diào)用時(shí),它花費(fèi)了 96 毫秒并導(dǎo)致緩存未命中,因?yàn)樵谡{(diào)用之前,此新報(bào)價(jià)之前不在緩存中。

總結(jié)

祝賀!您剛剛構(gòu)建了一個(gè)服務(wù),該服務(wù)執(zhí)行了一個(gè)代價(jià)高昂的操作并對(duì)其進(jìn)行了標(biāo)記,以便緩存結(jié)果。


分享標(biāo)題:Spring認(rèn)證指南:了解如何在GemFire中緩存數(shù)據(jù)
轉(zhuǎn)載來(lái)源:http://www.5511xx.com/article/cdgdogi.html