spring cloud 集成分布式配置中心 apollo(單機部署apollo)


一、什么是apollo?

Apollo(阿波羅)是攜程框架部門研發的分布式配置中心,能夠集中化管理應用不同環境、不同集群的配置,配置修改后能夠實時推送到應用端,並且具備規范的權限、流程治理等特性,適用於微服務配置管理場景。

image.png

二、為什么我要集成apollo?

阿波羅特性:
image.png

由於apollo相比spring cloud config等主流配置中心多了可視化管理配置和灰度發布等高級特性,所以打算放棄spring cloud config 改用apollo。

三、部署apollo

1.到github apollo的release頁面下載需要的三個zip包。
https://github.com/ctripcorp/apollo/releases
image.png

2.安裝mysql,然后運行如下兩個apollo初始化腳本,初始化兩個基本庫。

https://github.com/nobodyiam/apollo-build-scripts/blob/master/sql/apolloportaldb.sql

https://github.com/nobodyiam/apollo-build-scripts/blob/master/sql/apolloconfigdb.sql

3.將三個zip包上傳到服務器並解壓,每個zip包解壓出來都有一個application-github.properties文件,修改其中的mysql ip地址、端口、用戶名、密碼信息,指向本地的mysql數據庫,然后注意將application-github.properties文件放在jar包同級目錄,如下是我的目錄結構和application-github.properties文件內容。

[root@localhost apollo]# ls
admin-service  config-service  protal
[root@localhost admin-service]# ls
apollo-adminservice-1.4.0.jar  application-github.properties  app.properties  shutdown.sh  startup.sh
[root@localhost protal]# ls
apollo-env.properties  apollo-portal-1.4.0.jar  application-github.properties  app.properties  shutdown.sh  startup.sh
[root@localhost config-service]# ls
apollo-configservice-1.4.0.jar  application-github.properties  app.properties  shutdown.sh  startup.sh
spring.datasource.url = jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = 123456

4.修改portal目錄下的apollo-env.properties文件,填上對應的各個環境配置中心地址,我是單機版所以都填了本地:

[root@localhost protal]# cat apollo-env.properties 
local.meta=http://localhost:8080
dev.meta=http://localhost:8080
fat.meta=http://localhost:8080
uat.meta=http://localhost:8080
lpt.meta=${lpt_meta}
pro.meta=http://localhost:8080

5.依次運行三個jar包:
java -jar apollo-configservice-1.4.0.jar
java -jar apollo-adminservice-1.4.0.jar
java -jar apollo-portal-1.4.0.jar

6.瀏覽器訪問http://192.168.153.131:8070,看到如下界面說明啟動成功,初始用戶名密碼為:apollo/admin
image.png

四、spring cloud(boot)客戶端集成apollo

1.在應用pom.xml添加如下maven依賴:

        <!-- 阿波羅配置中心客戶端 -->
        <dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client</artifactId>
            <version>1.1.0</version>
        </dependency>

2.在src/main/resources/META-INF目錄下增加app.properties文件,內容如下:

app.id=你自己的應用唯一標識,和apollo管理后台添加的項目appid對應
apollo.autoUpdateInjectedSpringProperties=true

application.yml文件增加如下配置:

apollo:
  meta: http://部署apollo服務器的ip:8080
  bootstrap:
    enabled: true
    eagerLoad:
      enabled: true

3.在代碼中添加如下屬性和注解即可使用apollo后台配置好的信息,apollo管理后台修改后1秒即可生效,無需重啟應用服務器:
image.png

    @Value("${name}")
    private String name;

后續繼續更新分布式部署apollo。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM