首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZooKeeper for Java/Spring?

ZooKeeper for Java/Spring?
EN

Stack Overflow用户
提问于 2012-03-30 09:38:18
回答 5查看 22K关注 0票数 20

是否有任何经过详细记录的Apache用例用于分发ZooKeeper应用程序的配置,特别是ZooKeeper服务?

与云服务的许多用户一样,我需要更改可变数量的Java服务的配置,最好是在运行时,而不需要重新启动服务。

更新

最后,我编写了一些东西,可以将ZooKeeper节点作为属性文件加载,并创建一个ResourcePropertySource并将其插入到Spring上下文中。请注意,这不会反映上下文启动后ZooKeeper节点中的更改。

代码语言:javascript
复制
public class ZooKeeperPropertiesApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    private static final Logger logger = LoggerFactory.getLogger(ZooKeeperPropertiesApplicationContextInitializer.class);

    private final CuratorFramework curator;
    private String projectName;
    private String projectVersion;

    public ZooKeeperPropertiesApplicationContextInitializer() throws IOException {
        logger.trace("Attempting to construct CuratorFramework instance");

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(10, 100);
        curator = CuratorFrameworkFactory.newClient("zookeeper", retryPolicy);
        curator.start();
    }

    /**
     * Add a primary property source to the application context, populated from
     * a pre-existing ZooKeeper node.
     */
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        logger.trace("Attempting to add ZooKeeper-derived properties to ApplicationContext PropertySources");

        try {
            populateProjectProperties();
            Properties properties = populatePropertiesFromZooKeeper();
            PropertiesPropertySource propertySource = new PropertiesPropertySource("zookeeper", properties);
            applicationContext.getEnvironment().getPropertySources().addFirst(propertySource);

            logger.debug("Added ZooKeeper-derived properties to ApplicationContext PropertySources");
            curator.close();
        } catch (IOException e) {
            logger.error("IO error attempting to load properties from ZooKeeper", e);
            throw new IllegalStateException("Could not load ZooKeeper configuration");
        } catch (Exception e) {
            logger.error("IO error attempting to load properties from ZooKeeper", e);
            throw new IllegalStateException("Could not load ZooKeeper configuration");
        } finally {
            if (curator != null && curator.isStarted()) {
                curator.close();
            }
        }
    }

    /**
     * Populate the Maven artifact name and version from a property file that
     * should be on the classpath, with values entered via Maven filtering.
     * 
     * There is a way of doing these with manifests, but it's a right faff when
     * creating shaded uber-jars.
     * 
     * @throws IOException
     */
    private void populateProjectProperties() throws IOException {
        logger.trace("Attempting to get project name and version from properties file");

        try {
            ResourcePropertySource projectProps = new ResourcePropertySource("project.properties");
            this.projectName = (String) projectProps.getProperty("project.name");
            this.projectVersion = (String) projectProps.getProperty("project.version");
        } catch (IOException e) {
            logger.error("IO error trying to find project name and version, in order to get properties from ZooKeeper");
        }
    }

    /**
     * Do the actual loading of properties.
     * 
     * @return
     * @throws Exception
     * @throws IOException
     */
    private Properties populatePropertiesFromZooKeeper() throws Exception, IOException {
        logger.debug("Attempting to get properties from ZooKeeper");

        try {
            byte[] bytes = curator.getData().forPath("/distributed-config/" + projectName + "/" + projectVersion);
            InputStream in = new ByteArrayInputStream(bytes);
            Properties properties = new Properties();
            properties.load(in);
            return properties;
        } catch (NoNodeException e) {
            logger.error("Could not load application configuration from ZooKeeper as no node existed for project [{}]:[{}]", projectName, projectVersion);
            throw e;
        }

    }

}
EN

回答 5

Stack Overflow用户

发布于 2014-09-14 17:56:45

您应该考虑:

http://projects.spring.io/spring-cloud/

git存储库支持的

Spring集中外部配置管理。配置资源直接映射到Spring Environment,但如果需要的话,非Spring应用程序可以使用它。

可在此获得源代码:

https://github.com/spring-cloud/spring-cloud-config

示例应用程序如下:

https://github.com/spring-cloud/spring-cloud-config/blob/master/spring-cloud-config-sample/src/main/java/sample/Application.java

票数 6
EN

Stack Overflow用户

发布于 2013-05-11 12:58:02

我在propertyplaceholderconfigurer中创建了一组spring集成动物园管理员和spring框架,在githubhttps://github.com/james-wu-shanghai/spring-zookeeper.git中您可以查看一下。

票数 2
EN

Stack Overflow用户

发布于 2012-03-30 14:59:03

特别是spring,但是对于java来说,有一个分布式OSGI标准的CXF实现,它使用ZooKeeper作为发现服务器,将更新的包向下推到容器:http://cxf.apache.org/dosgi-discovery.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9940476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档