【问题标题】:Library for distributed spring config (outside springboot)分布式 spring 配置库(springboot 之外)
【发布时间】:2016-12-02 00:44:39
【问题描述】:

我正在寻找分布式弹簧配置的解决方案。我正在考虑将其存储在动物园管理员中。 https://github.com/spring-cloud/spring-cloud-zookeeper 确实具有该功能,但显然它需要使用 spring-boot

有没有类似的库可以在spring-boot之外使用

【问题讨论】:

    标签: spring spring-boot apache-zookeeper


    【解决方案1】:

    Consul by HashiCorp

    Consul 是一个受欢迎的选择,因为它是:

    • 开源
    • 包括服务发现和配置
    • 开箱即用支持多数据中心

    【讨论】:

      【解决方案2】:

      它不需要您使用 Spring Boot,它只是提供自动配置,以防您确实决定使用 Spring Boot。换句话说,如果您不使用 Spring Boot,则不会自动应用任何配置,您必须自己提供配置。

      Zookeeper 是个不错的选择,去吧。

      编辑: 要在没有 Spring Boot 的情况下使用 Zookeeper,您需要手动注册适当的 bean,或者通过导入 Spring Boot 将为您隐式导入的自动配置类来注册。这个经验法则通常适用于所有启用 Spring Boot 的模块。

      在您的情况下,您很可能只需要导入 ZookeeperConfigBootstrapConfigurationZookeeperConfigAutoConfiguration。这些类可以在 spring-cloud-zookeeper-config 模块中找到,因此不需要 Spring Boot 依赖项。

      或者,您应该查看这些类及其 @Imports 并手动声明 bean。

      【讨论】:

      • 是否有文档/示例我可以查看如何在 spring-boot 之外使用它
      • 我尝试在spring beans xml中添加以下两个bean 但我遇到了两个问题。 * 我的 application.yamlo 中带有前缀 spring.cloud.zookeeper 的属性没有被解析 * PropertySourceBootstrapConfiguration 的 bean 没有被创建,因此没有从 zookeeper 读取属性。
      • 那是因为您在上下文中缺少 BootstrapApplicationListener,请尝试添加它。此外,Zookeeper Config 不是从 application.properties/yml 读取,而是从 bootstrap.properties/yml 读取。
      • @Miloš Milivojević - 感谢您的建议。看起来整个事情都与spring-boot 联系得太紧密了,而且似乎无法在spring-boot 之外完成
      • 根本不正确:这可能无法用几行代码完成,但您可以走更长的路——手动注册 bean。
      【解决方案3】:

      根据https://wenku.baidu.com/view/493cf9eba300a6c30d229f49.html 此处提供的想法,我找到了在没有 Spring Boot 的情况下使用 spring-cloud-zookeeper 的解决方案

      首先,创建一个 CloudEnvironement 类,该类将从 Zookeeper 创建一个 PropertySource:

      CloudEnvironement.java

        public class CloudEnvironment extends StandardServletEnvironment { 
      
        @Override 
        protected void customizePropertySources(MutablePropertySources propertySources) { 
          super.customizePropertySources(propertySources); 
          try { 
            propertySources.addLast(initConfigServicePropertySourceLocator(this)); 
          } 
          catch (Exception ex) { 
            logger.warn("failed to initialize cloud config environment", ex); 
          } 
        } 
      
        private PropertySource<?> initConfigServicePropertySourceLocator(Environment environment) { 
          ZookeeperConfigProperties configProp = new ZookeeperConfigProperties(); 
          ZookeeperProperties props = new ZookeeperProperties(); 
          props.setConnectString("myzookeeper:2181"); 
          CuratorFramework fwk = curatorFramework(exponentialBackoffRetry(props), props); 
          ZookeeperPropertySourceLocator propertySourceLocator = new ZookeeperPropertySourceLocator(fwk, configProp); 
          PropertySource<?> source= propertySourceLocator.locate(environment); 
          return source ; 
        } 
      
        private CuratorFramework curatorFramework(RetryPolicy retryPolicy, ZookeeperProperties properties) { 
          CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); 
          builder.connectString(properties.getConnectString()); 
          CuratorFramework curator = builder.retryPolicy(retryPolicy).build(); 
          curator.start(); 
          try { 
            curator.blockUntilConnected(properties.getBlockUntilConnectedWait(), properties.getBlockUntilConnectedUnit()); 
          } 
          catch (InterruptedException e) { 
            throw new RuntimeException(e); 
          } 
          return curator; 
        } 
      
        private RetryPolicy exponentialBackoffRetry(ZookeeperProperties properties) { 
          return new ExponentialBackoffRetry(properties.getBaseSleepTimeMs(), 
              properties.getMaxRetries(), 
              properties.getMaxSleepMs()); 
        } 
      
      }
      

      然后创建一个自定义的 XmlWebApplicationContext 类:当您的 Web 应用程序启动时,它将启用从 Zookeeper 加载 PropertySource 并替换 Spring Boot 的引导魔法:

      MyConfigurableWebApplicationContext.java

      public class MyConfigurableWebApplicationContext extends XmlWebApplicationContext { 
      
        @Override 
        protected ConfigurableEnvironment createEnvironment() { 
          return new CloudEnvironment(); 
        } 
      }
      

      最后,在您的 web.xml 文件中添加以下上下文参数,以使用您的 MyConfigurableWebApplicationContext 类并引导您的 CloudEnvironement。

      <context-param>           
            <param-name>contextClass</param-name> 
            <param-value>com.kiabi.config.MyConfigurableWebApplicationContext</param-value> 
          </context-param> 
      

      如果您使用标准的属性文件配置器,它仍然应该被加载,这样您就可以在本地文件和 Zookeeper 中拥有属性。

      为了让所有这些工作,你需要在你的类路径中有 spring-cloud-starter-zookeeper-config 和 curator-framework jar 以及它们的依赖关系,如果你使用 maven,你可以将以下内容添加到你的 pom.xml

      <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-zookeeper-dependencies</artifactId>
                      <version>1.1.1.RELEASE</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.apache.curator</groupId>
                  <artifactId>curator-framework</artifactId>
              </dependency>
          </dependencies>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-09
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        • 2017-05-02
        相关资源
        最近更新 更多