攜程Apollo(阿波羅)安裝部署以及java整合實現

這篇文章主要介紹了攜程Apollo(阿波羅)安裝部署以及java整合實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

服務器部署

可以按照apollo wiki 進行部署

https://github.com/ctripcorp/apollo/wiki/Quick-Start

安裝 Java 環境

java

創建數據庫

Apollo服務端共需要兩個數據庫:ApolloPortalDB和ApolloConfigDB,我們把數據庫、表的創建和樣例數據都分別準備了sql文件,只需要導入數據庫即可。
執行兩個sql文件
sql/apolloportaldb.sql
sql/apolloconfigdb.sql
會創建兩個數據庫

下載安裝包

https://github.com/nobodyiam/apollo-build-scripts

服務器部署

將快速部署包apollo-quick-start放進服務器
解壓文件

服務器配置

Apollo服務端需要知道如何連接到你前面創建的數據庫,所以需要編輯demo.sh,修改ApolloPortalDB和ApolloConfigDB相關的數據庫連接串信息。

#apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=用戶名
apollo_config_db_password=密碼(如果沒有密碼,留空即可)

# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=用戶名
apollo_portal_db_password=密碼(如果沒有密碼,留空即可)

修改服務器端口地址信息

meta server url

config_server_url=http://10.168.16.125:8080
admin_server_url=http://10.168.16.125:8090
portal_url=http://10.168.16.125:8070

如果部署在linux服務器上需要將地址改爲服務器IP地址
注意:不要修改demo.sh的其它部分

確保端口未被佔用

Quick Start腳本會在本地啓動3個服務,分別使用8070, 8080, 8090端口,請確保這3個端口當前沒有被使用。

例如,在Linux/Mac下,可以通過如下命令檢查:

lsof -i:8080

執行啓動腳本

./demo.sh start

當看到如下輸出後,就說明啓動成功了!

==== starting service ====
Service logging file is ./service/apollo-service.log
Started [10768]
Waiting for config service startup.......
Config service started. You may visit http://localhost:8080 for service status now!
Waiting for admin service startup....
Admin service started
==== starting portal ====
Portal logging file is ./portal/apollo-portal.log
Started [10846]
Waiting for portal startup......
Portal started. You can visit http://localhost:8070 now!

使用Apollo配置中心

訪問 服務器的IP:8070 可以進行訪問
例如 :http://10.168.16.125:8070

這裏寫圖片描述

默認 用戶名密碼是 apollo admin
登陸後如下界面

這裏寫圖片描述

可以創建自己的項目

這裏寫圖片描述

添加namespace

namespace 相當於配置文件名稱
在namespace中添加屬性,可以以文本形式添加

可以自己研究琢磨下

java服務整合

pom整合

官方提供的maven

<dependency>
  <groupId>com.ctrip.framework.apollo</groupId>
  <artifactId>apollo-client</artifactId>
  <version>0.9.1</version>
</dependency>

發現拉不下來jar包
解決方案
下載源代碼
https://github.com/ctripcorp/apollo.git
編譯其中的 apollo-client 包並安裝到本地
引入編譯後的jar包即可

<dependency>
  <groupId>com.ctrip.framework.apollo</groupId>
  <artifactId>apollo-client</artifactId>
  <version>0.10.0-SNAPSHOT</version>
</dependency>

現有應用接入

在應用接入Apollo之後,這些配置是可以非常方便的遷移到Apollo的,具體步驟如下:

在Apollo爲應用新建項目
在應用中配置好META-INF/app.properties
把原先配置(必須是properties格式)複製一下,然後通過Apollo提供的文本編輯模式全部粘帖到應用的application namespace,發佈配置
如果原來是其它格式,如yml,請先轉成properties格式
把原先的配置文件如bootstrap.properties, application.properties從項目中刪除

app.properties 內容是創建項目的APPID(應用ID)如前面創建的12345

# test
app.id=12345

並在resources 下加入apollo-env.properties 各環境的服務器地址

local.meta=http://10.168.16.125:8080
dev.meta=http://10.168.16.125:8080
fat.meta=${fat_meta}
uat.meta=${uat_meta}
lpt.meta=${lpt_meta}
pro.meta=${pro_meta}

修改環境

修改/opt/settings/server.properties(Mac/Linux)或C:\opt\settings\server.properties(Windows)文件,設置env爲DEV:

env=DEV

Spring 整合

apollo啓動配置

<apollo:config/>

apollo加載namespace配置

 <apollo:config namespaces="dubbo" order="1"/>

####官方配置如下

Apollo也支持和Spring整合(Spring 3.1.1+),只需要做一些簡單的配置就可以了。

Apollo目前既支持比較傳統的基於XML的配置,也支持目前比較流行的基於Java(推薦)的配置。

需要注意的是,如果之前有使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的,請替換成org.springframework.context.support.PropertySourcesPlaceholderConfigurer。Spring 3.1以後就不建議使用PropertyPlaceholderConfigurer了,要改用PropertySourcesPlaceholderConfigurer。

基於XML的配置

注:需要把apollo相關的xml namespace加到配置文件頭上,不然會報xml語法錯誤。

1.注入默認namespace的配置到Spring中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:apollo="http://www.ctrip.com/schema/apollo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
  <!-- 這個是最簡單的配置形式,一般應用用這種形式就可以了,用來指示Apollo注入application namespace的配置到Spring環境中 -->
  <apollo:config/>
  <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
    <property name="timeout" value="${timeout:100}"/>
    <property name="batch" value="${batch:200}"/>
  </bean>
</beans>

2.注入多個namespace的配置到Spring中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:apollo="http://www.ctrip.com/schema/apollo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
  <!-- 這個是最簡單的配置形式,一般應用用這種形式就可以了,用來指示Apollo注入application namespace的配置到Spring環境中 -->
  <apollo:config/>
  <!-- 這個是稍微複雜一些的配置形式,指示Apollo注入FX.apollo和FX.soa namespace的配置到Spring環境中 -->
  <apollo:config namespaces="FX.apollo,FX.soa"/>
  <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
    <property name="timeout" value="${timeout:100}"/>
    <property name="batch" value="${batch:200}"/>
  </bean>
</beans>

dubbo服務整合

 <apollo:config/>
<apollo:config namespaces="dubbo" order="1"/>


  <!--   公共信息,也可以用dubbo.properties配置 -->
  <dubbo:application name="${dubbo.application.name}" />
  <!--   需要強制指定地址,配置文件默認可以不配置 -->
  <dubbo:registry address="${dubbo.registry.address}"/>

  <bean id="xxxService" class="com.xxx.cache.service.impl.RedisServiceImpl"/>
  <dubbo:service interface="com.xxx.xxx.service.RedisService" ref="redisService"
          version="${dubbo.version}"/>

dubbo 默認沒有不需要配置,但使用apollo後沒有自動注入dubbo註冊地址,需要手動聲明,原因待定

 <dubbo:registry address="${dubbo.registry.address}"/>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

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