前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程二十

Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程二十

原创
作者头像
IT胶囊
发布2021-12-30 15:38:45
4370
发布2021-12-30 15:38:45
举报
文章被收录于专栏:IT技能应用IT技能应用

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

Apache Geode 的 Spring 数据教程二十
Apache Geode 的 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):

代码语言:javascript
复制
<cache-loader>
   <class-name>com.company.app.DBLoader</class-name>
   <parameter name="URL">
     <string>jdbc://12.34.56.78/mydb</string>
   </parameter>
</cache-loader>

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

为什么需要基类?

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

7.8.1.使用模板bean 定义的配置

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

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

代码语言:javascript
复制
class DBLoader extends WiringDeclarableSupport implements CacheLoader {

  private DataSource dataSource;

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

  public Object load(LoaderHelper helper) { ... }
}
代码语言:javascript
复制
<cache-loader>
   <class-name>com.company.app.DBLoader</class-name>
   <!-- no parameter is passed (use the bean's implicit name, which is the class name) -->
</cache-loader>
代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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 id="dataSource" ... />

  <!-- template bean definition -->
  <bean id="com.company.app.DBLoader" abstract="true" p:dataSource-ref="dataSource"/>
</beans>
Apache Geode 的 Spring 数据教程二十
Apache Geode 的 Spring 数据教程二十

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

代码语言:javascript
复制
<cache-loader>
   <class-name>com.company.app.DBLoader</class-name>
   <!-- pass the bean definition template name as parameter -->
   <parameter name="bean-name">
     <string>template-bean</string>
   </parameter>
</cache-loader>
代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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 id="dataSource" ... />

   <!-- template bean definition -->
   <bean id="template-bean" abstract="true" p:dataSource-ref="dataSource"/>

</beans>
Apache Geode 的 Spring 数据教程二十
Apache Geode 的 Spring 数据教程二十

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

7.8.2.使用自动连接和注释的配置

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

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

代码语言:javascript
复制
class DBLoader extends WiringDeclarableSupport implements CacheLoader {

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

  public Object load(LoaderHelper helper) { ... }
}
代码语言:javascript
复制
<cache-loader>
   <class-name>com.company.app.DBLoader</class-name>
   <!-- no need to declare any parameters since the class is auto-wired -->
</cache-loader>
代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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
">

     <!-- enable annotation processing -->
     <context:annotation-config/>

</beans>
Apache Geode 的 Spring 数据教程二十
Apache Geode 的 Spring 数据教程二十

通过使用 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

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"
       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
">

  <!-- enable declarative caching -->
  <cache:annotation-driven/>

  <gfe:cache id="gemfire-cache"/>

  <!-- declare GemfireCacheManager; must have a bean ID of 'cacheManager' -->
  <bean id="cacheManager" class="org.springframework.data.gemfire.cache.GemfireCacheManager"
      p:cache-ref="gemfire-cache">

</beans>

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

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

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

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

代码语言:javascript
复制
@Service
class CustomerService {

@Cacheable(cacheNames="Accounts", key="#customer.id")
Account createAccount(Customer customer) {
  ...
}

然后你将需要以下配置。

XML:

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"
       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
">

  <!-- enable declarative caching -->
  <cache:annotation-driven/>

  <bean id="cacheManager" class="org.springframework.data.gemfire.cache.GemfireCacheManager">

  <gfe:cache/>

  <gfe:partitioned-region id="accountsRegion" name="Accounts" persistent="true" ...>
    ...
  </gfe:partitioned-region>
</beans>

Java配置:

代码语言:javascript
复制
@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,如以下示例所示:

代码语言:javascript
复制
<bean id="instantiator" class="org.springframework.data.gemfire.serialization.WiringInstantiator">
  <!-- DataSerializable type -->
  <constructor-arg>org.pkg.SomeDataSerializableClass</constructor-arg>
  <!-- type id -->
  <constructor-arg>95</constructor-arg>
</bean>

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

8.2.自动生成自定义Instantiators

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

代码语言:javascript
复制
<bean id="instantiatorFactory" class="org.springframework.data.gemfire.serialization.InstantiatorFactoryBean">
  <property name="customTypes">
    <map>
      <entry key="org.pkg.CustomTypeA" value="1025"/>
      <entry key="org.pkg.CustomTypeB" value="1026"/>
    </map>
  </property>
</bean>

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 7.8.接线Declarable组件
  • 7.8.1.使用模板bean 定义的配置
  • 7.8.2.使用自动连接和注释的配置
  • 7.9.支持 Spring Cache 抽象
  • 8. 使用 Apache Geode 序列化
  • 8.1.连接反序列化实例
  • 8.2.自动生成自定义Instantiators
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com