Apache Geode的Spring数据教程
寂寞的心情wang 发布于2021-12 浏览:835 回复:0
0
收藏

原标题:Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程二十(Spring中国教育管理中心)

 
7.8.接线Declarable组件
Apache Geode XML 配置(通常称为cache.xml)允许将用户对象声明为配置的一部分。通常这些对象是CacheLoadersApache Geode 支持的或其他可插入的回调组件。使用原生 Apache Geode 配置,通过 XML 声明的每个用户类型都必须实现Declarable接口,该接口允许通过Properties实例将任意参数传递给声明的类。

在本节中,我们将描述如何在cache.xml 使用 Spring定义时配置这些可插拔组件,同时保持在cache.xml. 这允许您的可插拔组件专注于应用程序逻辑,而不是DataSources 其他协作者的位置或创建。

但是,如果您正在启动一个绿地项目,建议您直接在 Spring 中配置 Cache、Region 和其他可插入的 Apache Geode 组件。这避免了从Declarable本节中介绍的接口或基类继承。

有关此方法的更多信息,请参阅以下侧边栏。

消除Declarable组件

开发人员可以完全通过 Spring 配置自定义类型,如配置区域中所述。这样,开发人员就不必实现Declarable接口,还可以从 Spring IoC 容器的所有功能中受益(不仅仅是依赖注入,还有生命周期和实例管理)。

作为Declarable使用 Spring配置组件的示例,请考虑以下声明(取自Declarable Javadoc):


com.company.app.DBLoader

jdbc://12.34.56.78/mydb


为了简化解析、转换参数和初始化对象的任务,Apache Geode 的 Spring Data 提供了一个基类 ( WiringDeclarableSupport),它允许通过模板bean 定义连接 Apache Geode 用户对象,或者,如果缺少,执行自动- 通过 Spring IoC 容器接线。要利用此功能,用户对象需要扩展WiringDeclarableSupport,它会自动定位声明BeanFactory 并作为初始化过程的一部分执行连接。

为什么需要基类?

在当前的 Apache Geode 版本中,没有对象工厂的概念,声明的类型被实例化并按原样使用。换句话说,没有简单的方法来管理 Apache Geode 之外的对象创建。

7.8.1.使用模板bean 定义的配置
使用时,WiringDeclarableSupport尝试首先定位现有的 bean 定义并将其用作接线模板。除非指定,否则组件类名称将用作隐式 bean 定义名称。

让我们DBLoader看看在这种情况下我们的声明会是什么样子:

class DBLoader extends WiringDeclarableSupport implements CacheLoader {

private DataSource dataSource;

public void setDataSource(DataSource dataSource){
this.dataSource = dataSource;
}

public Object load(LoaderHelper helper) { ... }
}

com.company.app.DBLoader



xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
">




 
在上面的场景中,由于没有指定参数,一个带有 id/name 的
beancom.company.app.DBLoader被用作连接由 Apache Geode 创建的实例的模板。对于 bean 名称使用不同约定的情况,可以bean-name在 Apache Geode 配置中传入参数:


com.company.app.DBLoader


template-bean



xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
">



 
该模板bean定义中没有XML声明。允许任何格式(Groovy、注释等)。

7.8.2.使用自动连接和注释的配置
默认情况下,如果没有找到 bean 定义,WiringDeclarableSupport将 自动装配 声明的实例。这意味着除非实例提供任何依赖注入元数据,否则容器将找到对象设置器并尝试自动满足这些依赖关系。但是,开发人员还可以使用 JDK 5 注释为自动装配过程提供附加信息。

例如,DBLoader上面的假设声明可以通过DataSource 以下方式注入 Spring-configured :

class DBLoader extends WiringDeclarableSupport implements CacheLoader {

// use annotations to 'mark' the needed dependencies
@javax.inject.Inject
private DataSource dataSource;

public Object load(LoaderHelper helper) { ... }
}

com.company.app.DBLoader



xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
">



 
通过使用 JSR-330 注释,CacheLoader代码得到了简化,因为 的位置和创建DataSource已被外部化,并且用户代码只关心加载过程。的DataSource可能是事务性的,以延迟方式创建,多个对象之间共享或从JNDI检索。这些方面可以通过 Spring 容器轻松配置和更改,而无需接触DBLoader代码。

7.9.支持 Spring Cache 抽象
Spring Data for Apache Geode 提供了 Spring Cache Abstraction的实现, 以将 Apache Geode 定位为Spring 缓存基础设施中的缓存提供者。

要使用 Apache Geode 作为支持实现,Spring 的 Cache Abstraction 中的“缓存提供者” ,只需添加到您的配置中:GemfireCacheManager

xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:gfe="https://www.springframework.org/schema/geode"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsd
https://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd
">



p:cache-ref="gemfire-cache">


在cache-ref该属性CacheManager如果默认缓存bean的名字被使用(即“gemfireCache”),即bean定义是没有必要的没有一个明确的标识。

当GemfireCacheManager声明(单例)bean 实例并启用声明性缓存时(在 XML 中或在 JavaConfig 中使用 Spring 的@EnableCaching注释),S​pring 缓存注释(例如@Cacheable)标识将使用 Apache Geode Regions 在内存中缓存数据的“缓存” .

这些缓存(即区域)必须在使用它们的缓存注解之前存在,否则会发生错误。

举例来说,假设您有一个带有CustomerService执行缓存的应用程序组件的客户服务应用程序......

@Service
class CustomerService {

@Cacheable(cacheNames="Accounts", key="#customer.id")
Account createAccount(Customer customer) {
...
}
然后你将需要以下配置。

XML:

xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:gfe="https://www.springframework.org/schema/geode"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache https://www.springframework.org/schema/cache/spring-cache.xsd
https://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd
">



...


Java配置:

@Configuration
@EnableCaching
class ApplicationConfiguration {

@Bean
CacheFactoryBean gemfireCache() {
return new CacheFactoryBean();
}

@Bean
GemfireCacheManager cacheManager() {
GemfireCacheManager cacheManager = GemfireCacheManager();
cacheManager.setCache(gemfireCache());
return cacheManager;
}

@Bean("Accounts")
PartitionedRegionFactoryBean accountsRegion() {
PartitionedRegionFactoryBean accounts = new PartitionedRegionFactoryBean();

accounts.setCache(gemfireCache());
accounts.setClose(false);
accounts.setPersistent(true);

return accounts;
}
}
当然,您可以自由选择您喜欢的任何 Region 类型(例如 REPLICATE、PARTITION、LOCAL 等)。

8. 使用 Apache Geode 序列化
为了提高 Apache Geode In-memory Data Grid 的整体性能,Apache Geode 支持一种称为 PDX 的专用序列化协议,除了在各种语言平台(Java、Java、 C++ 和 .NET)。

本章讨论 Spring Data for Apache Geode 简化和改进 Apache Geode 在 Java 中的自定义序列化的各种方式。

8.1.连接反序列化实例
序列化对象具有瞬态数据是相当普遍的。瞬态数据通常取决于它在某个时间点所处的系统或环境。例如,aDataSource是特定于环境的。序列化此类信息是无用的,甚至可能是危险的,因为它是特定 VM 或机器的本地信息。对于这种情况,用于 Apache Geode 的 Spring Data 提供了一种特殊的方法Instantiator ,可以在反序列化期间为 Apache Geode 创建的每个新实例执行连接。

通过这样的机制,你可以依靠Spring容器来注入和管理某些依赖,从而可以轻松地从持久数据中分离transient,并以透明的方式拥有丰富的域对象。

Spring 用户可能会发现这种方法类似于@Configurable)。其WiringInstantiator工作方式与 类似WiringDeclarableSupport,尝试首先将 bean 定义定位为接线模板,否则返回到自动接线。

要使用 SDG Instantiator,请将其声明为 bean,如以下示例所示:



org.pkg.SomeDataSerializableClass

95

在 Spring 容器启动期间,一旦初始化,Instantiator默认情况下,它会向 Apache Geode 序列化系统注册自己,并SomeDataSerializableClass在反序列化期间对 Apache Geode 创建的所有实例进行连接。

8.2.自动生成自定义Instantiators
对于数据密集型应用程序,随着数据流入,可能会在每台机器上创建大量实例。Apache Geode 使用反射来创建新类型,但是,对于某些场景,这可能被证明是昂贵的。与往常一样,最好进行分析以量化是否属于这种情况。对于这种情况,Apache Geode 的 Spring Data 允许自动生成Instatiator类,这些类在不使用反射的情况下实例化一个新类型(使用默认构造函数)。以下示例显示了如何创建实例化器:









前面的定义自动Instantiators为两个类(CustomTypeA和CustomTypeB)生成两个,并在用户 ID1025和下将它们注册到 Apache Geode 1026。两者Instantiators避免使用反射,直接通过Java代码创建实例。
 

收藏
点赞
0
个赞
TOP
切换版块