Spring Boot Gemfire Server Configuration

老子叫甜甜 提交于 2019-12-25 01:29:48

问题


I am trying to understand how to host a Spring Boot Gemfire server process.

I found this example Spring Gemfire Server

The problem I am having is the the server I am trying to add to the cluster is not showing up in the cluster after I start the process.

Here are the steps I am taking:

  1. Start a new locator locally (default port): gfsh>start locator --name=loc-one

  2. I want to add this SpringBootGemfireServer to the cluster:

note I have commented out the embeded locator start-up - I want to add this to the existing locator already running

    @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() {
    }
}

}

When I go to gfsh>connect list members

I only see the locator.


回答1:


I haven't verified the full configuration to check whether there's something else wrong, but the main issue I see right now is that you seem to be confusing the start-locator property (automatically starts a locator in the current process when the member connects to the distributed system and stops the locator when the member disconnects) with the locators property (the list of locators used by system members, it must be configured consistently for every member of the distributed system). Since you're not correctly setting the locators property when configuring the server, it just can't join the existing distributed system because it doesn't know which locator to connect to.

The default locator port used by GemFire is 10334, so you should change your gemfireProperties method as follows:

@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;
}

Hope this helps.

Cheers.



来源:https://stackoverflow.com/questions/51737079/spring-boot-gemfire-server-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!