SpringBoot集成Apollo配置中心(5分鐘集成系列)

相關文章

基於Docker 5分鐘搭建攜程Apollo分佈式配置中心

SpringBoot集成

  • 第一步
    登錄Apollo添加測試項目(apollo-test)
    file
  • 第二步
    添加測試配置
    // 服務的端口
    server.port = 8888
    // 測試的key
    key = va
    a = 1
    
    file
  • 第三步
    添加依賴
    <dependency>
         <groupId>com.ctrip.framework.apollo</groupId>
         <artifactId>apollo-client</artifactId>
         <version>1.5.0</version>
    </dependency>
    
  • 第四步,修改application.properties
    apollo.bootstrap.enabled = true
    
    #apollo項目的appid
    app.id=apollo-test
    
    #環境meta server的地址
    #實際的使用中  這個配置不會配置到這裏
    apollo.meta=http://127.0.0.1:8080
    
    file

    到此,一個最基礎的使用配置就已經完成

自定義配置文件及動態刷新

獲取單個配置

// 和普通的配置方式一樣,直接通過@Value獲取即可
@Value("${key}")
private String key;

獲取自定義配置對象ApolloConfig

如果是由多個配置項組成的一個配置集合,那我們可以針對起做一個單獨的配置對象

  • 創建配置文件對象
    // 默認值就是application,如果配置是配在application命名空間下,就可以不用寫value值
    // 如果是自定義的命名空間,就需要加上對於的名稱
    @EnableApolloConfig(value = "application")
    @Component
    // 獲取配置文件
    @ConfigurationProperties
    // lombok的get set
    @Data
    public class ApolloConfig {
        private String key;
        private Integer a;
    }
    
  • 創建測試接口
    @RestController
    public class TestController {
        @Value("${key}")
        private String key;
    
        @Autowired
        ApolloConfig apolloConfig;
    
        @GetMapping("key")
        public String getKey() {
            return key;
        }
    
        @GetMapping("va")
        public String getVa() {
             return apolloConfig.toString();
        }
    }
    
  • 測試
    file
    file

自動刷新

第一種方式(推薦)
  • 導入SpringCloud
    <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter</artifactId>
         <version>2.1.3.RELEASE</version>
    </dependency>
    
  • 創建配置文件刷新幫助類ApolloRefreshConfig
    @Component
    @Slf4j
    public class ApolloRefreshConfig implements ApplicationContextAware {
    
        ApplicationContext applicationContext;
    
        @Autowired
        RefreshScope refreshScope;
    
        // 這裏指定Apollo的namespace,非常重要,如果不指定,默認只使用application
        // 有多少命名空間(namespace)就指定多少個,這裏傳入的是一個數組
        @ApolloConfigChangeListener(value = {ConfigConsts.NAMESPACE_APPLICATION, "namespace1", "namespace2"})
        public void onChange(ConfigChangeEvent changeEvent) {
            for (String changedKey : changeEvent.changedKeys()) {
                log.info("apollo changed namespace:{} Key:{} value:{}", changeEvent.getNamespace(), changedKey, changeEvent.getChange(changedKey));
            }
            refreshProperties(changeEvent);
        }
    
        public void refreshProperties(ConfigChangeEvent changeEvent) {
            this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
            refreshScope.refreshAll();
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    }
    
  • 啓用自動配置向
    spring.boot.enableautoconfiguration=true
    
  • 重啓測試
    file
    file

    啓動之後在apollo管理平臺修改a的值,發佈之後可以看到控制檯會出現一天刷新記錄,直接訪問發現其值以及發生了變化

第二種 @Value註解
@Value("${key}")
private String key;

缺點:該方式只有添加了@Value才能正常的刷新,如果配置比較多的話,無形中會增加很多體力勞動,第一種方式是一勞永逸的

meta server配置

一開始的測試中,我們會在application.properties中添加一個apollo.meta=http://127.0.0.1:8080,這個只是適合本地開發的時候使用一下,如果需要發佈測試環境、正式環境的時候,如何如配置環境的地址來實現切換呢?官方提供了幾種方式

  • 第一種,setting配置文件
    // window在C:\opt\settings
    // linux或者mac在 /opt/settings
    // 下添加server.properties  並加入以下配置
    #集羣環境
    #apollo.cluster=xxx
    #meta server地址
    apollo.meta=http://127.0.0.1:8080
    
  • 第二種,運行時參數
    java -Dapollo.meta=http://127.0.0.1:8080 -jar xxx.jar
    
  • 第三種,代碼中設置
    System.setProperty("apollo.meta", "http://127.0.0.1:8080");
    

    具體的meta server的地址請根據個人的實際情況填寫

到此,Apollo的整合就已經完成,基於目前的情況幾乎可以滿足日常開發過程中的大部分需要了,是不是超級簡單!

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章