攜程Apollo(阿波羅)安裝部署以及java整合實(shí)現(xiàn)
服務(wù)器部署
可以按照apollo wiki 進(jìn)行部署
https://github.com/ctripcorp/apollo/wiki/Quick-Start
安裝 Java 環(huán)境
java
創(chuàng)建數(shù)據(jù)庫
Apollo服務(wù)端共需要兩個(gè)數(shù)據(jù)庫:ApolloPortalDB和ApolloConfigDB,我們把數(shù)據(jù)庫、表的創(chuàng)建和樣例數(shù)據(jù)都分別準(zhǔn)備了sql文件,只需要導(dǎo)入數(shù)據(jù)庫即可。
執(zhí)行兩個(gè)sql文件
sql/apolloportaldb.sql
sql/apolloconfigdb.sql
會(huì)創(chuàng)建兩個(gè)數(shù)據(jù)庫
下載安裝包
https://github.com/nobodyiam/apollo-build-scripts
服務(wù)器部署
將快速部署包apollo-quick-start放進(jìn)服務(wù)器
解壓文件
服務(wù)器配置
Apollo服務(wù)端需要知道如何連接到你前面創(chuàng)建的數(shù)據(jù)庫,所以需要編輯demo.sh,修改ApolloPortalDB和ApolloConfigDB相關(guān)的數(shù)據(jù)庫連接串信息。
#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=密碼(如果沒有密碼,留空即可)
修改服務(wù)器端口地址信息
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服務(wù)器上需要將地址改為服務(wù)器IP地址
注意:不要修改demo.sh的其它部分
確保端口未被占用
Quick Start腳本會(huì)在本地啟動(dòng)3個(gè)服務(wù),分別使用8070, 8080, 8090端口,請(qǐng)確保這3個(gè)端口當(dāng)前沒有被使用。
例如,在Linux/Mac下,可以通過如下命令檢查:
lsof -i:8080
執(zhí)行啟動(dòng)腳本
./demo.sh start
當(dāng)看到如下輸出后,就說明啟動(dòng)成功了!
==== 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配置中心
訪問 服務(wù)器的IP:8070 可以進(jìn)行訪問
例如 :http://10.168.16.125:8070

默認(rèn) 用戶名密碼是 apollo admin
登陸后如下界面

可以創(chuàng)建自己的項(xiàng)目

添加namespace
namespace 相當(dāng)于配置文件名稱
在namespace中添加屬性,可以以文本形式添加
可以自己研究琢磨下
java服務(wù)整合
pom整合
官方提供的maven
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>0.9.1</version> </dependency>
發(fā)現(xiàn)拉不下來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>
現(xiàn)有應(yīng)用接入
在應(yīng)用接入Apollo之后,這些配置是可以非常方便的遷移到Apollo的,具體步驟如下:
在Apollo為應(yīng)用新建項(xiàng)目
在應(yīng)用中配置好META-INF/app.properties
把原先配置(必須是properties格式)復(fù)制一下,然后通過Apollo提供的文本編輯模式全部粘帖到應(yīng)用的application namespace,發(fā)布配置
如果原來是其它格式,如yml,請(qǐng)先轉(zhuǎn)成properties格式
把原先的配置文件如bootstrap.properties, application.properties從項(xiàng)目中刪除
app.properties 內(nèi)容是創(chuàng)建項(xiàng)目的APPID(應(yīng)用ID)如前面創(chuàng)建的12345
# test app.id=12345
并在resources 下加入apollo-env.properties 各環(huán)境的服務(wù)器地址
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}
修改環(huán)境
修改/opt/settings/server.properties(Mac/Linux)或C:\opt\settings\server.properties(Windows)文件,設(shè)置env為DEV:
env=DEV
Spring 整合
apollo啟動(dòng)配置
<apollo:config/>
apollo加載namespace配置
<apollo:config namespaces="dubbo" order="1"/>
####官方配置如下
Apollo也支持和Spring整合(Spring 3.1.1+),只需要做一些簡單的配置就可以了。
Apollo目前既支持比較傳統(tǒng)的基于XML的配置,也支持目前比較流行的基于Java(推薦)的配置。
需要注意的是,如果之前有使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的,請(qǐng)?zhí)鎿Q成org.springframework.context.support.PropertySourcesPlaceholderConfigurer。Spring 3.1以后就不建議使用PropertyPlaceholderConfigurer了,要改用PropertySourcesPlaceholderConfigurer。
基于XML的配置
注:需要把a(bǔ)pollo相關(guān)的xml namespace加到配置文件頭上,不然會(huì)報(bào)xml語法錯(cuò)誤。
1.注入默認(rèn)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">
<!-- 這個(gè)是最簡單的配置形式,一般應(yīng)用用這種形式就可以了,用來指示Apollo注入application namespace的配置到Spring環(huán)境中 -->
<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.注入多個(gè)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">
<!-- 這個(gè)是最簡單的配置形式,一般應(yīng)用用這種形式就可以了,用來指示Apollo注入application namespace的配置到Spring環(huán)境中 -->
<apollo:config/>
<!-- 這個(gè)是稍微復(fù)雜一些的配置形式,指示Apollo注入FX.apollo和FX.soa namespace的配置到Spring環(huán)境中 -->
<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服務(wù)整合
<apollo:config/>
<apollo:config namespaces="dubbo" order="1"/>
<!-- 公共信息,也可以用dubbo.properties配置 -->
<dubbo:application name="${dubbo.application.name}" />
<!-- 需要強(qiáng)制指定地址,配置文件默認(rèn)可以不配置 -->
<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 默認(rèn)沒有不需要配置,但使用apollo后沒有自動(dòng)注入dubbo注冊(cè)地址,需要手動(dòng)聲明,原因待定
<dubbo:registry address="${dubbo.registry.address}"/>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)md5和base64加密解密的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)md5和base64加密解密的示例代碼,幫助大家更好的利用Java加密解密文件,感興趣的朋友可以了解下2020-09-09
Spring Boot結(jié)合IDEA自帶Maven插件如何快速切換profile
IDEA是目前 Java 開發(fā)者中使用最多的開發(fā)工具,它有著簡約的設(shè)計(jì)風(fēng)格,強(qiáng)大的集成工具,便利的快捷鍵,這篇文章主要介紹了Spring Boot結(jié)合IDEA自帶Maven插件快速切換profile,需要的朋友可以參考下2023-03-03
Spring Boot啟動(dòng)banner定制的步驟詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot啟動(dòng)banner定制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
利用5分鐘快速搭建一個(gè)springboot項(xiàng)目的全過程
Spring Boot的監(jiān)控能夠使開發(fā)者更好地掌控應(yīng)用程序的運(yùn)行狀態(tài),下面這篇文章主要給大家介紹了關(guān)于如何利用5分鐘快速搭建一個(gè)springboot項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
java返回json請(qǐng)求中文變成問號(hào)的問題及解決
這篇文章主要介紹了java返回json請(qǐng)求中文變成問號(hào)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

