当前位置: 首页 > 面试题库 >

ZooKeeper for Java / Spring Config吗?

庞乐池
2023-03-14
问题内容

是否有任何有据可查的Apache ZooKeeper用例用于分发Java应用程序(尤其是Spring服务)的配置?

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

更新

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

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

    }

}

问题答案:

上周我在James Strachen的Apache
Camel演讲中,他提到使用ZooKeeper作为其基于Java的云服务器在后台的配置信息。

我已经看过Adrian Colyer(SpringSource的CTO)关于Spring中运行时配置更改的演讲,但是Spring今天支持吗?

在我看来,如果您从典型的架构Spring应用程序开始,我看不到您可以在其之上轻松完成对动态配置更改的改造。



 类似资料:
  • 问题内容: 我正在尝试使用DockerCompose使用远程存储库部署ConfigServrService和客户端。 docker-compose.yml类似于: 在他们部署的那一刻,客户端控制台显示: cccConfigServicePropertySourceLocator:从位于http:// localhost:8888的 服务器中获取配置 (而不是http:// config-serve

  • 我试图部署一个ConfigServrService和一个客户端与远程存储库使用DockerComplace。 docker-compose.yml就像: 在它们部署的那一刻,客户端控制台显示: C. C. C. ConfigServiceProperty tySourceLocator:从服务器获取配置:http://localhost:8888 (而不是http://config-server:

  • 本文向大家介绍你体验过QQ漫画吗,对它有什么改进意见吗?相关面试题,主要包含被问及你体验过QQ漫画吗,对它有什么改进意见吗?时的应答技巧和注意事项,需要的朋友参考一下 1. 栏目分类不够独立 某一栏目下的内容比较混乱,包含其它栏目内容 2.漫画内容打擦边球 部分热门漫画内容包含软*** 3.推荐内容不感兴趣 推荐算法需要优化 4.女性向漫画数量过少 整体画风偏男性向 5.漫画类型较为单一 均为长篇

  • 本文向大家介绍你有看过JQuery的源码吗?说下它的实现原理是什么?相关面试题,主要包含被问及你有看过JQuery的源码吗?说下它的实现原理是什么?时的应答技巧和注意事项,需要的朋友参考一下 [jQuery] 你有看过JQuery的源码吗?说下它的实现原理是什么? #163 重复了

  • 本文向大家介绍你知道自定义事件吗?jQuery里的fire函数是什么意思,什么时候用?相关面试题,主要包含被问及你知道自定义事件吗?jQuery里的fire函数是什么意思,什么时候用?时的应答技巧和注意事项,需要的朋友参考一下 [jQuery] 你知道自定义事件吗?jQuery里的fire函数是什么意思,什么时候用?

  • 问题内容: 我对此进行了一些研究,并浏览了StackOverflow上的一些文章以及一些博客文章,但是没有找到确切的答案。我还读到可以使用4.0框架来实现,但尚未找到任何支持证据。 所以我的问题是,是否可以通过LINQ to SQL查询执行SOUNDEX? 问题答案: 您可以通过使用伪造的UDF在数据库上执行此操作;在部分类中,向数据上下文添加一个方法: 您可以使用像这样的表达式:

  • 问题内容: Java程序中可以存在两种主要方法吗? 只有通过他们的论点不同,例如: 第二个可以是 如果可能,将使用哪个方法作为入口点?如何识别? 问题答案: 只要方法参数(数字(或)类型)不同,是的,它们可以。这称为超载。 重载的方法通过传递给方法的参数的数量和类型来区分 只有具有单个(或)参数的main方法将被视为程序的入口点。

  • 问题内容: 我具有实现的接口层次结构。我想使用不可变的对象,所以我想设计一些类来方便地构造这些对象。但是,我有很多接口,并且我不想在每种类型的子生成器中重复构建s 的代码。 因此,假设以下定义: 我怎样才能有效地实施的建设者和?他们应支持以下操作: 和 我不想为每个子生成器实现一个特殊情况。 编辑以添加第二个属性,以澄清使用简单的泛型无法做到这一点。我不是在寻找一种方式来组合和-我正在寻找一种方式