Spring 引导 Gemfire 服务器配置

Spring Boot Gemfire Server Configuration

我正在尝试了解如何托管 Spring Boot Gemfire 服务器进程。

我找到了这个例子Spring Gemfire Server

我遇到的问题是我尝试添加到集群的服务器在我启动进程后没有出现在集群中。

以下是我正在执行的步骤:

  1. 在本地启动一个新的定位器(默认端口):gfsh>start locator --name=loc-one

  2. 我想将此 SpringBootGemfireServer 添加到集群中:

请注意,我已经注释掉了嵌入式定位器启动 - 我想将其添加到现有定位器中 运行

    @SpringBootApplication
    @SuppressWarnings("unused")
    public class SpringGemFireServerApplication {

        private static final boolean DEFAULT_AUTO_STARTUP = true;

        public static void main(String[] args) {
           SpringApplication.run(SpringGemFireServerApplication.class, args);
    }

    @Bean
    static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertyPlaceholderConfigurer();
    }

    private String applicationName() {
    return SpringGemFireServerApplication.class.getSimpleName();
    }

    @Bean
    Properties gemfireProperties(
          @Value("${gemfire.log.level:config}") String logLevel,
          @Value("${gemfire.locator.host-port:localhost[10334]}") String locatorHostPort,
          @Value("${gemfire.manager.port:1099}") String managerPort) {

    Properties gemfireProperties = new Properties();

    gemfireProperties.setProperty("name", applicationName());
    gemfireProperties.setProperty("log-level", logLevel);
    //gemfireProperties.setProperty("start-locator", locatorHostPort);
    //gemfireProperties.setProperty("jmx-manager", "true");
    //gemfireProperties.setProperty("jmx-manager-port", managerPort);
    //gemfireProperties.setProperty("jmx-manager-start", "true");

    return gemfireProperties;
    }

    @Bean
    CacheFactoryBean gemfireCache(@Qualifier("gemfireProperties") Properties gemfireProperties) {

    CacheFactoryBean gemfireCache = new CacheFactoryBean();

    gemfireCache.setClose(true);
    gemfireCache.setProperties(gemfireProperties);

    return gemfireCache;
}

@Bean
CacheServerFactoryBean gemfireCacheServer(Cache gemfireCache,
        @Value("${gemfire.cache.server.bind-address:localhost}") String bindAddress,
        @Value("${gemfire.cache.server.hostname-for-clients:localhost}") String hostNameForClients,
        @Value("${gemfire.cache.server.port:40404}") int port) {

    CacheServerFactoryBean gemfireCacheServer = new CacheServerFactoryBean();


    gemfireCacheServer.setCache(gemfireCache);
    gemfireCacheServer.setAutoStartup(DEFAULT_AUTO_STARTUP);
    gemfireCacheServer.setBindAddress(bindAddress);
    gemfireCacheServer.setHostNameForClients(hostNameForClients);
    gemfireCacheServer.setPort(port);

    return gemfireCacheServer;
}

@Bean
PartitionedRegionFactoryBean<Long, Long> factorialsRegion(Cache gemfireCache,
        @Qualifier("factorialsRegionAttributes") RegionAttributes<Long, Long> factorialsRegionAttributes) {

    PartitionedRegionFactoryBean<Long, Long> factorialsRegion = new PartitionedRegionFactoryBean<>();

    factorialsRegion.setAttributes(factorialsRegionAttributes);
    factorialsRegion.setCache(gemfireCache);
    factorialsRegion.setClose(false);
    factorialsRegion.setName("Factorials");
    factorialsRegion.setPersistent(false);

    return factorialsRegion;
}

@Bean
@SuppressWarnings("unchecked")
RegionAttributesFactoryBean factorialsRegionAttributes() {

    RegionAttributesFactoryBean factorialsRegionAttributes = new RegionAttributesFactoryBean();

    factorialsRegionAttributes.setCacheLoader(factorialsCacheLoader());
    factorialsRegionAttributes.setKeyConstraint(Long.class);
    factorialsRegionAttributes.setValueConstraint(Long.class);

    return factorialsRegionAttributes;
}

FactorialsCacheLoader factorialsCacheLoader() {
    return new FactorialsCacheLoader();
}

class FactorialsCacheLoader implements CacheLoader<Long, Long> {

    // stupid, naive implementation of Factorial!
@Override
    public Long load(LoaderHelper<Long, Long> loaderHelper) throws CacheLoaderException {

        long number = loaderHelper.getKey();

        assert number >= 0 : String.format("Number [%d] must be greater than equal to 0", number);

        if (number <= 2L) {
            return (number < 2L ? 1L : 2L);
        }

        long result = number;

        while (number-- > 1L) {
            result *= number;
        }

        return result;
    }

    @Override
    public void close() {
    }
}

}

当我去gfsh>connect list members

我只看到定位器。

我还没有验证完整的配置来检查是否还有其他问题,但我现在看到的主要问题是你似乎混淆了 start-locator 属性(自动启动成员连接到分布式系统时当前进程中的定位器,成员断开连接时停止定位器)与locators 属性(系统成员使用的定位器列表,必须配置一致对于分布式系统的每个成员)。由于您在配置服务器时没有正确设置 locators 属性,它无法加入现有的分布式系统,因为它不知道要连接到哪个定位器。

GemFire 使用的默认定位器端口是 10334,因此您应该按如下方式更改 gemfireProperties 方法:

@Bean
Properties gemfireProperties(@Value("${gemfire.log.level:config}") String logLevel, @Value("${gemfire.locator.host-port:localhost[10334]}") String locatorHostPort, @Value("${gemfire.manager.port:1099}") String managerPort) {
  Properties gemfireProperties = new Properties();
  gemfireProperties.setProperty("name", applicationName());
  gemfireProperties.setProperty("log-level", logLevel);
  // You can directly use the locatorHostPort variable instead.
  gemfireProperties.setProperty("locators", "localhost[10334]");

  return gemfireProperties;
}

希望对您有所帮助。

干杯。