原题目: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 无关紧要,但咱们通常倡议以可读性为指标,以便下一个必须保护代码的人(可能是您未来某个时候)。