Spring Cloud Config Client Without Spring Boot

前端 未结 5 1715
猫巷女王i
猫巷女王i 2020-12-02 10:23

We have an existing spring web app deployed as a WAR file into Amazon Elastic Beanstalk. Currently we load properties files as http resources to give us a single source of p

5条回答
  •  执念已碎
    2020-12-02 11:26

    I found a solution for using spring-cloud-zookeeper without Spring Boot, based on the idea provided here https://wenku.baidu.com/view/493cf9eba300a6c30d229f49.html

    It should be easily updated to match your needs and using a Spring Cloud Config Server (the CloudEnvironement class needs to be updated to load the file from the server instead of Zookeeper)

    First, create a CloudEnvironement class that will create a PropertySource (ex from Zookeeper) :

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

    Then create a custom XmlWebApplicationContext class : it will enable to load the PropertySource from Zookeeper when your webapplication start and replace the bootstrap magic of Spring Boot:

    MyConfigurableWebApplicationContext.java

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

    Last, in your web.xml file add the following context-param for using your MyConfigurableWebApplicationContext class and bootstraping your CloudEnvironement.

               
          contextClass 
          com.kiabi.config.MyConfigurableWebApplicationContext 
         
    

    If you use a standard property file configurer, it should still be loaded so you can have properties in both a local file and Zookeeper.

    For all this to work you need to have spring-cloud-starter-zookeeper-config and curator-framework jar in your classpath with their dependancy, if you use maven you can add the following to your pom.xml

    
            
                
                    org.springframework.cloud
                    spring-cloud-zookeeper-dependencies
                    1.1.1.RELEASE
                    pom
                    import
                
            
        
    
        
            
                org.springframework.cloud
                spring-cloud-starter-zookeeper-config
            
            
                org.apache.curator
                curator-framework
            
        
    

提交回复
热议问题