1. Overview
1.概述
Apache Geode provides data management solutions through a distributed cloud architecture. It would be ideal to utilize the Spring Data APIs for the data access through the Apache Geode server.
Apache Geode通过分布式云架构提供数据管理解决方案。利用Spring Data API来通过Apache Geode服务器进行数据访问是最理想的。
In this tutorial, we’ll explore Spring Data Geode for the configuration and development of an Apache Geode Java client application.
在本教程中,我们将探讨Spring Data Geode,以配置和开发Apache Geode Java客户端应用程序。
2. Spring Data Geode
2.Spring数据Geode
The Spring Data Geode library empowers a Java application to configure an Apache Geode server through XML and annotations. At the same time, the library is also handy for creating an Apache Geode cache client-server application.
Spring Data Geode库使Java应用程序能够通过XML和注解来配置Apache Geode服务器。同时,该库在创建Apache Geode缓存客户-服务器应用程序时也很方便。
The Spring Data Geode library is similar to Spring Data Gemfire. Apart from subtle differences, the latter provides integration with Pivotal Gemfire, which is a commercial version of Apache Geode.
Spring Data Geode 库与Spring Data Gemfire相似。除了细微的差别之外,后者提供了与Pivotal Gemfire的集成,后者是Apache Geode的商业版本。
Along the way, we’ll explore a few Spring Data Geode annotations to configure a Java application into an Apache Geode’s cache client.
在此过程中,我们将探索一些Spring Data Geode注解,将Java应用配置成Apache Geode的缓存客户端。
3. Maven Dependency
3.Maven的依赖性
Let’s add the latest spring-geode-starter dependency to our pom.xml:
让我们把最新的spring-geode-starter依赖性添加到我们的pom.xml。
<dependency>
<groupId>org.springframework.geode</groupId>
<artifactId>spring-geode-starter</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
4. Apache Geode’s @ClientCacheApplication with Spring Boot
4.Apache Geode的@ClientCacheApplication与Spring Boot的关系
First, let’s create a Spring Boot ClientCacheApp by using @SpringBootApplication:
首先,让我们通过使用@SpringBootApplication创建一个Spring Boot ClientCacheApp。
@SpringBootApplication
public class ClientCacheApp {
public static void main(String[] args) {
SpringApplication.run(ClientCacheApp.class, args);
}
}
Then, to transform the ClientCacheApp class into the Apache Geode cache client, we’ll add the Spring Data Geode provided @ClientCacheApplication:
然后,为了将ClientCacheApp类转化为Apache Geode缓存客户端,我们将添加Spring Data Geode提供的@ClientCacheApplication。
@ClientCacheApplication
// existing annotations
public class ClientCacheApp {
// ...
}
That’s it! The cache client app is ready to run.
这就是了!缓存客户端应用程序已经可以运行了。
However, before starting our app, we’ll need to start the Apache Geode server.
然而,在启动我们的应用程序之前,我们需要启动Apache Geode服务器。
5. Start an Apache Geode Server
5.启动一个Apache Geode服务器
Assuming that Apache Geode and gfsh command-line interface are already set up, we can start a locator named basicLocator and then a server named basicServer.
假设Apache Geode和gfsh命令行界面已经设置好了,我们可以启动一个名为basicLocator的定位器,然后启动一个名为basicServer的服务器。
To do so, let’s run the following commands in the gfsh CLI:
要做到这一点,让我们在gfsh CLI中运行以下命令。
gfsh>start locator --name="basicLocator"
gfsh>start server --name="basicServer"
Once the server starts running, we can list all the members:
一旦服务器开始运行,我们就可以列出所有成员。
gfsh>list members
The gfsh CLI output should list the locator and the server:
gfsh CLI输出应列出定位器和服务器。
Name | Id
------------ | ------------------------------------------------------------------
basicLocator | 10.25.3.192(basicLocator:25461:locator)<ec><v0>:1024 [Coordinator]
basicServer | 10.25.3.192(basicServer:25546)<v1>:1025
Voila! We’re all set to run our cache client app using the Maven command:
瞧!我们已经准备好了。我们已经准备好使用Maven命令来运行我们的缓存客户端应用程序了。
mvn spring-boot:run
6. Configuration
6.配置
Let’s configure our cache client app to access data through the Apache Geode server.
让我们配置我们的缓存客户端应用程序,通过Apache Geode服务器访问数据。
6.1. Region
6.1 地区
First, we’ll create an entity named Author and then define it as an Apache Geode Region. A Region is similar to a table in the RDBMS:
首先,我们将创建一个名为Author 的实体,然后将其定义为Apache Geode Region。一个Region类似于RDBMS中的一个表。
@Region("Authors")
public class Author {
@Id
private Long id;
private String firstName;
private String lastName;
private int age;
}
Let’s review the Spring Data Geode annotations declared in the Author entity.
让我们回顾一下Author实体中声明的Spring Data Geode注解。
To begin with, @Region will create the Authors region in the Apache Geode server to persist the Author object.
首先,@Region将在Apache Geode服务器中创建Authors区域以持久化Author对象。
Then, @Id will mark the property as a primary key.
然后,@Id将标记该属性为一个主键。
6.2. Entity
6.2. 实体
We can enable the Author entity by adding @EnableEntityDefinedRegions.
我们可以通过添加@EnableEntityDefinedRegions来激活Author实体。
Also, we’ll add @EnableClusterConfiguration to let the application create the regions in the Apache Geode server:
另外,我们将添加@EnableClusterConfiguration来让应用程序在Apache Geode服务器中创建区域。
@EnableEntityDefinedRegions(basePackageClasses = Author.class)
@EnableClusterConfiguration
// existing annotations
public class ClientCacheApp {
// ...
}
Therefore, restarting the app will create the regions automatically:
因此,重新启动应用程序将自动创建区域。
gfsh>list regions
List of regions
---------------
Authors
6.3. Repository
6.3.存储库
Next, we’ll add CRUD operations on the Author entity.
接下来,我们将在Author实体上添加CRUD操作。
To do so, let’s create a repository named AuthorRepository, which extends Spring Data’s CrudRepository:
为此,让我们创建一个名为AuthorRepository的仓库,它扩展了Spring Data的CrudRepository。
public interface AuthorRepository extends CrudRepository<Author, Long> {
}
Then, we’ll enable the AuthorRepository by adding @EnableGemfireRepositories:
然后,我们将通过添加@EnableGemfireRepositories来激活AuthorRepository。
@EnableGemfireRepositories(basePackageClasses = AuthorRepository.class)
// existing annotations
public class ClientCacheApp {
// ...
}
Now, we’re all set to perform CRUD operations on the Author entity using methods like save and findById provided by CrudRepository.
现在,我们可以使用CrudRepository提供的save和findById等方法对Author实体执行CRUD操作。
6.4. Indexes
6.4.指数
Spring Data Geode provides an easy way to create and enable the indexes in the Apache Geode server.
Spring Data Geode提供了一种简单的方法来创建和启用Apache Geode服务器中的索引。
First, we’ll add @EnableIndexing to the ClientCacheApp class:
首先,我们要在ClientCacheApp类中添加@EnableIndexing。
@EnableIndexing
// existing annotations
public class ClientCacheApp {
// ...
}
Then, let’s add @Indexed to a property in the Author class:
然后,让我们把@Indexed添加到Author类的一个属性中。
public class Author {
@Id
private Long id;
@Indexed
private int age;
// existing data members
}
Here, Spring Data Geode will automatically implement the indexes based on the annotations defined in the Author entity.
在这里,Spring Data Geode将根据Author实体中定义的注释自动实现索引。
Hence, @Id will implement the primary key index for the id. Similarly, @Indexed will implement the hash index for the age.
因此,@Id将实现id的主键索引。同样地,@Indexed将实现age的哈希索引。
Now, let’s restart the application and confirm the indexes created in the Apache Geode server:
现在,让我们重新启动应用程序,确认在Apache Geode服务器中创建的索引。
gfsh> list indexes
Member Name | Region Path | Name | Type | Indexed Expression | From Clause | Valid Index
----------- | ----------- | ----------------- | ----- | ------------------ | ----------- | -----------
basicServer | /Authors | AuthorsAgeKeyIdx | RANGE | age | /Authors | true
basicServer | /Authors | AuthorsIdHashIdx | RANGE | id | /Authors | true
Likewise, we can use @LuceneIndexed to create an Apache Geode Lucene index for the String typed properties.
同样,我们可以使用@LuceneIndexed来为String类型的属性创建Apache Geode Lucene索引。
6.5. Continuous Query
6.5.连续查询
The continuous query enables the application to receive automatic notifications when data gets changed in the server. It matches the query and relies on the subscription model.
连续查询使应用程序能够在服务器中的数据被改变时收到自动通知。它与查询相匹配,并依赖于订阅模式。
To add the capability, we’ll create the AuthorService and add @ContinuousQuery with the matching query:
为了添加这个能力,我们将创建AuthorService,并添加@ContinuousQuery,并添加匹配的查询。
@Service
public class AuthorService {
@ContinuousQuery(query = "SELECT * FROM /Authors a WHERE a.id = 1")
public void process(CqEvent event) {
System.out.println("Author #" + event.getKey() + " updated to " + event.getNewValue());
}
}
To use the continuous queries, we’ll enable server-to-client subscriptions:
为了使用连续查询,我们将启用服务器到客户端的订阅。
@ClientCacheApplication(subscriptionEnabled = true)
// existing annotations
public class ClientCacheApp {
// ...
}
Hence, our app will receive automatic notification at the process method, whenever we modify an Author object with an id equal to 1.
因此,只要我们修改一个id等于1的Author对象,我们的应用程序将在process方法中收到自动通知。
7. Additional Annotations
7.附加注释
Let’s explore a few handy annotations additionally available in the Spring Data Geode library.
让我们来探讨一下Spring Data Geode库中额外提供的几个方便的注释。
7.1. @PeerCacheApplication
7.1@PeerCacheApplication
So far, we’ve examined a Spring Boot application as an Apache Geode cache client. At times, we may require our application to be an Apache Geode peer cache application.
到目前为止,我们已经研究了一个作为Apache Geode缓存客户端的Spring Boot应用程序。有时,我们可能需要我们的应用程序成为一个Apache Geode对等缓存应用程序。
Then, we should annotate the main class with @PeerCacheApplication in place of @CacheClientApplication.
然后,我们应该用@PeerCacheApplication来代替@CacheClientApplication来注释主类。
Also, @PeerCacheApplication will automatically create an embedded peer cache instance to connect with.
另外,@PeerCacheApplication 将自动创建一个嵌入式的对等缓存实例来连接。
7.2. @CacheServerApplication
7.2@CacheServerApplication
Similarly, to have our Spring Boot application as both a peer member and a server, we can annotate the main class with @CacheServerApplication.
同样,为了让我们的Spring Boot应用程序既是对等成员又是服务器,我们可以用@CacheServerApplication来注释主类。。
7.3. @EnableHttpService
7.3.@EnableHttpService
We can enable Apache Geode’s embedded HTTP server for both of @PeerCacheApplication and @CacheServerApplication.
我们可以为@PeerCacheApplication和@CacheServerApplication.启用Apache Geode的嵌入式 HTTP服务器。
To do so, we need to annotate the main class with @EnableHttpService. By default, the HTTP service starts on port 7070.
为此,我们需要用@EnableHttpService注解主类。默认情况下,HTTP服务在7070端口启动。
7.4. @EnableLogging
7.4.@EnableLogging
We can enable the logging by simply adding @EnableLogging to the main class. At the same time, we can use the logLevel and logFile attributes to set the corresponding properties.
我们可以通过简单地在主类中添加@EnableLogging来启用日志记录。同时,我们可以使用logLevel和logFile属性来设置相应的属性。
7.5. @EnablePdx
7.5.@EnablePdx
Also, we can enable Apache Geode’s PDX serialization technique for all our domains, by merely adding @EnablePdx to the main class.
此外,我们可以为我们所有的域启用Apache Geode的PDX序列化技术,只需在主类中添加@EnablePdx。
7.6. @EnableSsl and @EnableSecurity
7.6.@EnableSsl和@EnableSecurity
We can use @EnableSsl to switch on Apache Geode’s TCP/IP Socket SSL. Similarly, @EnableSecurity can be used to enable Apache Geode’s security for authentication and authorization.
我们可以使用@EnableSsl来开启Apache Geode的TCP/IP Socket SSL。同样,@EnableSecurity可以用来启用Apache Geode的安全认证和授权。
8. Conclusion
8.结语
In this tutorial, we’ve explored Spring Data for Apache Geode.
在本教程中,我们已经探讨了Apache Geode的Spring Data。
To begin with, we’ve created a Spring Boot application to serve as the Apache Geode cache client application.
首先,我们创建了一个Spring Boot应用程序,作为Apache Geode缓存的客户端应用程序。
At the same time, we’ve examined a few handy annotations provided by Spring Data Geode to configure and enable Apache Geode features.
同时,我们研究了Spring Data Geode提供的一些方便的注释,以配置和启用Apache Geode功能。
Last, we’ve explored a few additional annotations like @PeerCacheApplication and @CacheServerApplication to change the application to a peer or server in the cluster configuration.
最后,我们探讨了一些额外的注解,如@PeerCacheApplication 和 @CacheServerApplication ,将应用程序改为集群配置中的一个对等体或服务器。
As usual, all the code implementations are available over on GitHub.
像往常一样,所有的代码实现都可以在GitHub上找到,。