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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/333152.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux命令之 chsh -- 用来更换登录系统时使用的shell

文章目录命令简介常用选项参考示例查看系统安装了哪些shell的两种方法查看当前正在使用的 shell修改当前登录用户的shell命令简介 chsh 命令用来更换登录系统时使用的shell。若不指定任何参数与用户名称&#xff0c;则 chsh 会以应答的方式进行设置。 chsh 用于更改登录 shell…

layui绑定json_JSON-B非对称属性绑定

layui绑定jsonJSON-B规范定义了诸如JsonbProperty或JsonbTransient类的绑定注释&#xff0c;以声明方式将Java对象映射到JSON&#xff0c;然后又映射回JSON。 这些注释可以“非对称地”用于定义序列化和反序列化的不同处理。 如果在Java属性上或在getter和setter上都注释了JSO…

熔断器

一、引入依赖 1.consumer_service中加入依赖 <!--开启熔断器--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>2.开启熔断的注解 //注…

Linux 命令之 pwck -- 用来验证系统认证文件内容和格式的完整性

文章目录命令介绍常用选项参考示例检验用户配置文件 /etc/passwd和/etc/shadow的内容是否合法、完整命令介绍 pwck 命令用来校验用户配置文件 /etc/passwd 和 /etc/shadow 内容是否合法或完整 常用选项 选项说明-q仅报告错误信息-s以用户id排序文件“/etc/passwd”和“/etc/…

不停机与停机更新_Istio的零停机滚动更新

不停机与停机更新本系列文章的第一部分介绍了如何在Kubernetes集群中实现真正的零停机时间更新。 我们专门解决了将流量从旧实例切换到新实例时出现的请求失败。 本文将展示如何使用Istio群集实现相同的目标。 服务网格技术&#xff08;例如Istio&#xff09;通常与容器编排结…

远程调用 Spring Cloud Feign

一、 Feign简介 Feign [feɪn] 译文 伪装。Feign是一个声明式WebService客户端.使用Feign能让编写WebService客户端更加简单,它的使用方法是定义一个接口&#xff0c;然后在上面添加注解。不再需要拼接URL&#xff0c;参数等操作。项目主页&#xff1a;https://github.com/Ope…

Linux 命令之 pwconv -- 开启用户的投影密码

命令介绍 该命令根据文件 /etc/passwd 创建影子文件 /etc/shadow。 用来开启用户的投影密码。Linux系统里的用户和群组密码&#xff0c;本来分别存放在名称为 passwd 和 group 的文件中&#xff0c; 这两个文件位于 /etc 目录下。因系统运作所需&#xff0c;任何人都得以读取…

plsql如何执行单个语句_在单个try-with-resources语句中仔细指定多个资源

plsql如何执行单个语句Java 7最有用的新功能之一是引入了try-with-resources语句 [AKA 自动资源管理 &#xff08; ARM &#xff09;]。 try-with-resources语句的吸引力在于它承诺 “确保在语句末尾关闭每个资源”。 在这种情况下&#xff0c;“资源”是实现AutoCloseable及其…

Spring Cloud Feign 负载均衡

一、Feign负载均衡介绍 Feign本身集成了Ribbon依赖和自动配置&#xff0c;因此不需要额外引入依赖&#xff0c;也不需要再注册RestTemplate对象 Feign内置的ribbon默认设置了请求超时时长&#xff0c;默认是1000&#xff0c;可以修改 ribbon内部有重试机制&#xff0c;一旦超…

Linux 命令之 pwunconv -- 关闭投影密码

文章目录命令介绍命令介绍 pwunconv 命令与 pwconv 功能相反&#xff0c;用来关闭用户的投影密码。它会将密码从 /etc/shadow 文件内&#xff0c;重新回存到 /etc/passwd 文件中。换句话说&#xff0c;pwunconv 命令将文件 /etc/shadow 中的数据同步到文件 /etc/passwd 中&…

apache ignite_Apache Ignite,Hazelcast,Cassandra和Tarantool之间的主要区别

apache igniteApache Ignite在世界范围内得到广泛使用&#xff0c;并且一直在增长。 诸如Barclays&#xff0c;Misys&#xff0c;Sberbank&#xff08;欧洲第三大银行&#xff09;&#xff0c;ING&#xff0c;JacTravel之类的公司都使用Ignite来增强其架构的功能&#xff0c;这…

Spring Cloud Feign 熔断器支持

一、实现步骤 在配置文件application.yml中开启feign熔断器支持编写FallBack处理类&#xff0c;实现FeignClient客户端在FeignClient注解中&#xff0c;指定FallBack处理类。测试 1. 在配置文件application.yml中开启feign熔断器支持&#xff1a;默认关闭 # 开启Feign的熔断功…

system health_可重复使用的MicroProfile Health探针

system healthMicroProfile Health API是一种非常基本的API&#xff0c;它基于一个或多个Health Probe报告您的服务状态。 在某些服务器或群集控制器需要决定是否以及何时重新启动实例的情况下&#xff0c;这非常有用。 在应用程序中使用MicroProfile Health API就像实现一个&…

Linux 命令之 visudo -- 编辑/etc/sudoers文件

文章目录命令介绍常用选项参考示例编辑文件 /etc/sudoers检查 /etc/sudoers 文件的语法错误、所有者和模式命令介绍 使用 visudo 命令可以打开 /etc/sudoers 文件进行编辑。当然 vim 也可以编辑该文件&#xff0c;但是不建议这样做&#xff0c;因为使用命令 visudo 编辑文件 /…

Spring Cloud Feign 请求压缩 、Feign的日志级别配置

一、Spring Cloud Feign 请求压缩 #开启压缩compression:request:enabled: true # 开启请求压缩#最小触发压缩的大小min-request-size: 2048#触发压缩数据类型&#xff0c;&#xff08;注意不能使用"" ,&#xff09;mime-types: text/xml, application/xml, applicat…

Linux 命令之 sudoedit -- 以另外一个用户身份编辑文件

文章目录命令介绍常用选项参考示例以用户 root 身份来编辑指定的文件以其它用户身份编辑指定的文件命令介绍 查看使用帮助&#xff0c;感觉和命令 sudo 完全一样。 [mysqlhtlwk0001host ~]$ sudoedit -h sudoedit - 以其他用户身份编辑文件usage: sudoedit [-AknS] [-r role]…

文档在线签名_为什么需要为文档和合同切换到在线签名

文档在线签名嘿&#xff0c;怪胎&#xff0c; 今天&#xff0c;我们为您带来一些不同。 无论您是开发人员&#xff0c;经理还是设计师&#xff0c;这都会提高您的生产力和效率。 对于公司和个人而言&#xff0c;良好地管理文书工作是强大基础的最重要部分之一。 将工作流程从…

网关 Spring Cloud Gateway

一、 Gateway 简介 Spring Cloud Gateway 是Spring Cloud团队的一个全新项目&#xff0c;基于Spring 5.0、SpringBoot2.0、Project Reactor 等技术开发的网关。 旨在为微服务架构提供一种简单有效统一的API路由管理方式。 Spring Cloud Gateway 作为SpringCloud生态系统中的网…

Linux命令之su -- 用于切换当前用户身份到其他用户身份

文章目录命令介绍特别重要常用选项su 命令存在的安全隐患参考示例切换成指定的用户身份&#xff0c;但是环境变量不变切换成指定的用户身份&#xff0c;并且改成对应用户的环境变量切换成指定的用户&#xff0c;并告知新的 shell 不要去读取启动文件切换成指定的用户执行命令&a…

apache ignite_Apache Ignite变得简单:第一个Java应用程序

apache ignite在本文中&#xff0c;我们将更进一步&#xff0c;让您完成第一个Ignite应用程序的创建&#xff0c;以从分布式缓存中进行读写操作。 作为第一个示例&#xff0c;我们将尽可能简单地向您展示如何用Java编写用于处理Apache Ignite集群数据的应用程序。 可从GitHub存…