Pivotal GemFire locators 和 start-locator 属性仅供服务器使用。这两个属性都不适用于客户端。
locators 标识新成员将用于作为对等点加入的集群的定位器。格式如下:host1[port1],host2[port2],...,hostN[portN]。
start-locator 用于在对等 Cache [application] 节点或服务器中启动嵌入式定位器。在 IDE 中启动一个小型 GemFire 服务器(对等成员节点)集群时,此属性非常方便。
提示:通常,在独立的生产环境中,您希望启动多个独立的专用定位器节点(JVM 进程,通常在不同的主机上)以获得弹性。但在开发过程中,使用start-locator 属性很方便,尤其是在测试时。
您可以阅读有关locators 和start-locator 属性here 的更多信息。您可以阅读有关定位器的更多信息,特别是成员发现,here 和 here。
那么,第一个问题,为什么在这种情况下需要一个或多个定位器?
关于...
我需要放置一个定位器“localhost[1099]”,但是当我放置:gemfireProperties.setProperty("locators", "localhost[1099]"); 时,我从 Spring 收到一条错误消息,提示找不到定位器(更具体地说是 NULL),...
当您尚未启动定位器时会出现这种情况。如果您指定 start-locator,那么您的 Spring(Pivotal GemFire 的数据),对等 Cache 应用程序将在启动时嵌入一个定位器(服务),在这种情况下,您不需要指定 locators 属性,因为它将是多余的。
使用嵌入式定位器,您可以启动,或让其他成员加入集群中的该成员。
例如,我经常使用这样的类来启动一个小型 Pivotal GemFire 集群,用于开发、测试和演示目的......
@SpringBootApplication
//@CacheServerApplication(name = "ExampleServerApplication", locators = "localhost[10334]")
@PeerCacheApplication(name = "BookExampleServerApplication", locators = "localhost[10334]")
@EnablePdx(readSerialized = true)
@SuppressWarnings("unused")
public class GeodeServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(GeodeServerApplication.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
}
@Profile("locator-manager")
@Configuration
@EnableLocator
@EnableManager(start = true)
static class LocatorManagerConfiguration { }
}
完整的源代码可用here。
有几点需要注意。
首先,@CacheServerApplication 和 @PeerCacheApplication 注释几乎是同义词。他们都创建了一个 Spring Boot、Pivotal GemFire peer Cache 应用程序节点(服务器)。唯一的区别是,@CacheServerApplication 额外添加了一个ServerSocket,允许基于 GemFire ClientCache 的应用程序(即带有 SDG 的 @ClientClientApplication 注释的 Spring Boot 应用程序)连接到服务器。
如您所见,我创建了一个内部的静态 LocatorManagerConfiguration 类。此类使用@EnableLocator 和@EnableManager 进行注释。 @EnableLocator 等效于 start-locator 属性,允许您控制嵌入式定位器服务在启动时将绑定到的 NIC 和端口。默认情况下,嵌入式定位器服务在“localhost”上启动,绑定到默认定位器端口10334。
我添加了 @EnableManager 注释来启动基于 GemFire JMX 的管理服务,它允许 Gfsh 连接到我们的 Spring Boot 配置/引导的 GemFire 服务器。
我使用 Spring Profile ("locator-manager") 启用此 Spring @Configuration 类,以便只有 1 个服务器以嵌入式定位器/管理器开始。当然,我可以让多个服务器启动一个定位器和/或管理器,但是我需要小心地在监听客户端连接时改变定位器和管理器服务使用的端口号(例如,管理器监听来自 JMX 客户端的连接比如Gfsh,或者JConsole,或者JVisualVM等)。
那么,我们如何运行它?
好吧,假设我们要创建一个包含 3 个服务器的小型 GemFire 集群。在我的 IDE 中,我使用相同的 GeodeServerApplication 类创建了 3 个不同的运行配置文件。第一个运行配置文件将从启用的“locator-manager”Spring 配置文件开始,就像这样......
-server -ea -Dspring.profiles.active=locator-manager -Dspring.data.gemfire.name=GeodeServerOne
我的 IDE 中接下来的 2 个运行配置文件是这样设置的...
-server -ea -Dspring.profiles.active=none -Dspring.data.gemfire.name=GeodeServerTwo
-server -ea -Dspring.profiles.active=none -Dspring.data.gemfire.name=GeodeServerThree
唯一的区别是,对于我上次运行的配置文件,运行配置文件 3,我需要将服务器命名为不同的名称,例如“GeodeServerThree”。 Pivotal GemFire 期望集群中对等成员的名称是唯一的。
现在,从我的 IDE 中,我可以先启动启用了定位器/管理器的服务器,然后在没有启用嵌入式定位器/管理器的情况下运行最后两台服务器。然后,我可以使用 Gfsh 检查我的集群,如下所示:
$ echo $GEODE_HOME
/Users/jblum/pivdev/apache-geode-1.2.1
$ gfsh
_________________________ __
/ _____/ ______/ ______/ /____/ /
/ / __/ /___ /_____ / _____ /
/ /__/ / ____/ _____/ / / / /
/______/_/ /______/_/ /_/ 1.2.1
Monitor and Manage Apache Geode
gfsh>connect
Connecting to Locator at [host=localhost, port=10334] ..
Connecting to Manager at [host=10.0.0.121, port=1099] ..
Successfully connected to: [host=10.0.0.121, port=1099]
gfsh>list members
Name | Id
---------------- | ---------------------------------------------
GeodeServerOne | 10.0.0.121(GeodeServerOne:42883)<ec><v0>:1024
GeodeServerTwo | 10.0.0.121(GeodeServerTwo:42911)<v1>:1025
GeodeServerThree | 10.0.0.121(GeodeServerThree:42913)<v2>:1026
如果您已启用这些 Spring Boot、Pivotal GemFire peer Cache 应用程序为 CacheServers(通过将 @PeerCacheApplication 注释替换为 @CacheServerApplication),那么您将能够连接缓存客户端应用程序,类似下面...
https://github.com/jxblum/contacts-application/blob/master/boot-example/src/main/java/example/app/geode/cache/client/BootExampleApplication.java
提示:BootExampleApplication 正在使用新的 Spring Boot for Apache Geode/Pivotal GemFire (SBDG) 项目,该项目自动配置 ClientCache 实例,默认情况下,spring-geode-starter 或 @ 987654376@,在您的应用程序的classpath 上。有关更多详细信息,请参阅docs。如果这个项目没有使用 SBDG,那么您必须明确地使用 SDG 的 @ClientCacheApplication 注释您的 Spring Boot 应用程序类。
阅读 SDG 参考指南的 chapter 和 section 了解更多详情。
无论如何,我希望这能给您一些指导,让您了解这些属性的用途以及 SDG 注释的功能。还可以查看新的 Spring Boot for Apache Geode/Pivotal GemFire 项目(参见 blog post),它使用约定优于配置和 Spring Boot 的自动配置支持进一步简化客户端或服务器 Apache Geode/Pivotal GemFire 应用程序的配置和引导。
干杯,
-约翰