Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十四
发布于 2 年前 作者 li05 3974 次浏览 来自 分享

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

6.15.配置集群配置推送
这可能是 Spring Data for Apache Geode 中最令人兴奋的新功能。

当客户端应用程序类被注解时@
EnableClusterConfiguration,客户端应用程序在 Spring Container 中定义和声明为 bean 的任何区域或索引都被“推送”到客户端连接到的服务器集群。不仅如此,这种“推送”的执行方式是 Apache Geode 在使用 HTTP 时记住客户端推送的配置。如果集群中的所有节点都出现故障,它们会以与以前相同的配置重新启动。如果将新服务器添加到集群中,它将获得相同的配置。

从某种意义上说,此功能与您使用Gfsh在集群中的所有服务器上手动创建区域和索引没有太大区别。除了现在,使用 Spring Data for Apache Geode,您不再需要使用Gfsh来创建区域和索引。借助 Spring Data for Apache Geode 的强大功能,您的 Spring Boot 应用程序已经包含为您创建区域和索引所需的所有配置元数据。

当您使用 Spring Data Repository 抽象时,我们知道您的应用程序将需要的所有区域(例如由带@Region 注释的实体类定义的区域)和索引(例如由带注释的@Indexed实体字段和属性定义的区域)和索引。

当您使用 Spring 的缓存抽象时,我们还知道应用程序服务组件所需的缓存注释中标识的所有缓存的所有区域。

从本质上讲,您已经告诉我们我们需要知道的一切,只需使用 Spring 框架开发您的应用程序,只需使用其所有 API 和功能,无论是以注释元数据、Java、XML 或其他方式表达的,无论是用于配置、映射、或任何目的。

关键是,您可以在使用框架的功能和支持基础设施(例如 Spring 的缓存抽象、Spring 数据存储库、Spring 的事务管理等)的同时专注于应用程序的业务逻辑,而 Spring Data for Apache Geode 负责所有这些代表您的这些框架功能所需的 Apache Geode 管道。

将配置从客户端推送到集群中的服务器并让集群记住它部分是通过使用 Apache Geode 的集群配置 服务实现的。Apache Geode 的 Cluster Configuration 服务也是Gfsh用来记录gfsh> create region --name=Example --type=PARTITION用户从 shell 向集群发出的与模式相关的更改(例如)的相同服务。

当然,由于集群可能会“记住”客户端在上次运行时推送的先前配置,Apache Geode 的 Spring Data 小心不要踩踏服务器中已经定义的任何现有区域和索引。这尤其重要,例如,当 Regions 已经包含数据时!

目前,没有覆盖任何现有区域或索引定义的选项。要重新创建 Region 或 Index,您必须先使用Gfsh销毁 Region 或 Index,然后重新启动客户端应用程序,以便将配置再次推送到服务器。或者,您可以使用Gfsh手动(重新)定义区域和索引。

与Gfsh不同,Spring Data for Apache Geode 仅支持从客户端在服务器上创建区域和索引。对于高级配置和用例,您应该使用Gfsh来管理(服务器端)集群。

要使用此功能,您必须明确声明对
org.springframework:spring-webSpring、Apache GeodeClientCache应用程序类路径的依赖。

考虑以下配置中表示的功率:

弹簧ClientCache应用

@SpringBootApplication
@ClientCacheApplication
@EnableCachingDefinedRegions
@EnableEntityDefinedRegions
@EnableIndexing
@EnableGemfireCaching
@EnableGemfireRepositories
@EnableClusterConfiguration
class ClientApplication { … }
您可以立即获得带有 Apache GeodeClientCache实例的 Spring Boot 应用程序、Spring Data Repositories、以 Apache Geode 作为缓存提供程序的 Spring 缓存抽象(其中区域和索引不仅在客户端上创建,而且还推送到集群中的服务器)。

从那里,您只需要执行以下操作:

定义使用映射和索引注释注释的应用程序域模型对象。
定义 Repository 接口以支持每个实体类型的基本数据访问操作和简单查询。
定义包含处理实体的业务逻辑的服务组件。
在需要缓存、事务行为等的服务方法上声明适当的注释。
在这种情况下,没有任何内容与应用程序后端服务(例如 Apache Geode)中所需的基础设施和管道有关。数据库用户具有类似的功能。现在 Spring 和 Apache Geode 开发人员也这样做了。

当与以下 Spring Data for Apache Geode 注释结合使用时,这个应用程序真的开始起飞了,只需很少的努力:

@EnableContinuousQueries
@EnableGemfireFunctionExecutions
@EnableGemfireCacheTransactions
有关更多详细信息,请参阅@
EnableClusterConfiguration注释 Javadoc。

6.16.配置 SSL
与序列化要通过线路传输的数据同样重要的是在传输过程中保护数据。当然,在 Java 中实现这一点的常用方法是使用安全套接字扩展 (SSE) 和传输层安全性 (TLS)。

要启用 SSL,请使用 注释您的应用程序类@EnableSsl,如下所示:

ClientCache启用 SSL 的Spring应用程序

@SpringBootApplication
@ClientCacheApplication
@EnableSsl
public class ClientApplication { … }
然后您需要设置必要的 SSL 配置属性或属性:密钥库、用户名/密码等。

您可以使用 SSL单独配置不同的 Apache Geode 组件(GATEWAY、HTTP、JMX、LOCATOR和SERVER),或者您可以使用CLUSTER枚举值共同配置它们以使用 SSL 。

您可以通过使用嵌套@EnableSsl注释、components具有枚举值的属性来指定 SSL 配置设置应应用哪些 Apache Geode 组件Component,如下所示:

ClientCache组件启用 SSL 的Spring应用程序

@SpringBootApplication
@ClientCacheApplication
@EnableSsl(components = { GATEWAY, LOCATOR, SERVER })
public class ClientApplication { … }
此外,您还可以 通过使用相应的注解属性或关联的配置属性来指定组件级 SSL 配置(ciphers,protocols和keystore/truststore信息)。

有关 更多详细信息,请参阅@EnableSsl注释 Javadoc。

可以在此处找到有关 Apache Geode SSL 支持的更多详细信息 。

6.17.配置安全
毫无疑问,应用程序安全性非常重要,Spring Data for Apache Geode 为保护 Apache Geode 客户端和服务器提供了全面的支持。

最近,Apache Geode 引入了一个新的集成安全框架(取代了其旧的身份验证和授权安全模型)来处理身份验证和授权。这个新安全框架的主要特性和好处之一是它与Apache Shiro集成, 因此可以将身份验证和授权请求委托给 Apache Shiro 以加强安全性。

本节的其余部分演示 Spring Data for Apache Geode 如何进一步简化 Apache Geode 的安全故事。

6.17.1.配置服务器安全
您可以通过多种不同的方式为 Apache Geode 集群中的服务器配置安全性。

实现 Apache Geodeorg.apache.geode.security.SecurityManager接口并设置 Apache Geode 的 security-manager属性以SecurityManager使用完全限定的类名引用您的应用程序实现。或者,用户可以构造和初始化其SecurityManager实现的实例,并 在创建 Apache Geode peer 时使用CacheFactory.setSecurityManager(:SecurityManager)方法设置它Cache。
shiro.ini使用为您的应用程序定义的用户、角色和权限创建一个 Apache Shiro文件,然后将 Apache Geodesecurity-shiro-init属性设置为引用此shiro.ini文件,该文件必须在CLASSPATH.
仅使用 Apache Shiro,使用 Spring Data for Apache Geode 的新@EnableSecurity注解来注解您的 Spring Boot 应用程序类, 并将一个或多个 Apache Shiro 定义Realms 为 Spring 容器中的 bean,用于访问应用程序的安全元数据(即授权用户、角色和权限) .
第一种方法的问题是您必须实现自己的SecurityManager,这可能非常乏味且容易出错。实现自定义SecurityManager为从存储元数据的任何数据源(例如 LDAP 甚至专有的内部数据源)访问安全元数据提供了一定的灵活性。然而,这个问题已经通过配置和使用 Apache Shiro 解决了Realms,它更广为人知并且非 Apache Geode 特定。

第二种方法,使用 Apache Shiro INI 文件,稍微好一点,但您首先仍然需要熟悉 INI 文件格式。此外,INI 文件是静态的,在运行时不易更新。

第三种方法是最理想的,因为它遵循广为人知和行业公认的概念(即 Apache Shiro 的安全框架)并且易于设置,如下例所示:

使用 Apache Shiro 的 Spring 服务器应用程序

@SpringBootApplication
@CacheServerApplication
@EnableSecurity
class ServerApplication {

@Bean
PropertiesRealm shiroRealm() {

  PropertiesRealm propertiesRealm = new PropertiesRealm();

  propertiesRealm.setResourcePath("classpath:shiro.properties");
  propertiesRealm.setPermissionResolver(new GemFirePermissionResolver());

  return propertiesRealm;

}
}
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十四
活动目录
JDBC
JNDI
LDAP
ARealm支持INI 格式。
您甚至可以创建 Apache Shiro 的自定义实现Realm。

当 Apache ShiroCLASSPATH位于集群中的服务器上并且一个或多个 Apache ShiroRealms 已在 Spring 容器中定义为 bean 时,Apache Geode 的 Spring Data 会检测此配置并使用 Apache Shiro 作为安全提供者来保护您的 Apache Geode 服务器使用@EnableSecurity注释时。

6.17.2.配置客户端安全
如果不讨论如何保护基于 Spring 的 Apache Geode 缓存客户端应用程序,安全故事就不会完整。

老实说,Apache Geode 保护客户端应用程序的过程相当复杂。简而言之,您需要:

提供org.apache.geode.security.AuthInitialize接口的实现 。
将 Apache Geode security-client-auth-init(系统)属性设置为引用自定义的、应用程序提供的 AuthInitialize界面。
在专有的 Apache Geodegfsecurity.properties文件中指定用户凭据。
Spring Data for Apache Geode 通过使用@EnableSecurity在服务器应用程序中使用的相同注释来简化所有这些步骤。换句话说,相同的@EnableSecurity注释处理客户端和服务器应用程序的安全性。例如,当用户决定将他们的应用程序从嵌入式对等Cache应用程序切换到应用程序时,此功能使他们更容易ClientCache。只需将 SDG 注释从@PeerCacheApplication或更改@CacheServerApplication@ClientCacheApplication,即可完成。

实际上,您需要在客户端上执行以下操作:

Spring客户端应用程序使用 @EnableSecurity

@SpringBootApplication
@ClientCacheApplication
@EnableSecurity
class ClientApplication { … }
然后就可以定义熟悉的application.properties包含所需用户名和密码的Spring Boot文件,如下例所示,大功告成:

application.properties具有所需安全凭证的Spring Boot文件

spring.data.gemfire.security.username=jackBlack
spring.data.gemfire.security.password=b@cK!nB1@cK
默认情况下,application.properties当你的文件放在应用程序的CLASSPATH. 当然,Spring 通过使用其Resource 抽象支持多种定位资源的方式 。

6.18.配置提示
以下提示可以帮助您充分利用新的基于注释的配置模型:

配置组织
其他基于配置的注释
6.18.1.配置组织
正如我们在“配置集群配置推送”一节中看到的,当很多 Apache Geode 或 Spring Data for Apache Geode 特性通过使用注解启用时,我们开始在 Spring@Configuration@SpringBootApplication类上堆叠大量注解。在这种情况下,开始对配置进行一些划分是有意义的。

例如,考虑以下声明:

ClientCache厨房水槽的弹簧应用

@SpringBootApplication
@ClientCacheApplication
@EnableContinuousQueries
@EnableCachingDefinedRegions
@EnableEntityDefinedRegions
@EnableIndexing
@EnableGemfireCacheTransactions
@EnableGemfireCaching
@EnableGemfireFunctionExecutions
@EnableGemfireRepositories
@EnableClusterConfiguration
class ClientApplication { … }
我们可以按关注分解此配置,如下所示:

ClientCache使用 kitcken 接收器启动的Spring应用程序

@SpringBootApplication
@Import({ GemFireConfiguration.class, CachingConfiguration.class,
FunctionsConfiguration.class, QueriesConfiguration.class,
RepositoriesConfiguration.class })
class ClientApplication { … }

@ClientCacheApplication
@EnableClusterConfiguration
@EnableGemfireCacheTransactions
class GemFireConfiguration { … }

@EnableGemfireCaching
@EnableCachingDefinedRegions
class CachingConfiguration { … }

@EnableGemfireFunctionExecutions
class FunctionsConfiguration { … }

@EnableContinuousQueries
class QueriesConfiguration {

@ContinuousQuery(…)
void processCqEvent(CqEvent event) {

}
}

@EnableEntityDefinedRegions
@EnableGemfireRepositories
@EnableIndexing
class RepositoriesConfiguration { … }
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十四
虽然这对 Spring Framework 无关紧要,但我们通常建议以可读性为目标,以便下一个必须维护代码的人(可能是您将来某个时候)。

回到顶部