maven的Springboot專案的攜程Apollo配置中心的配置以及使用
說明:轉發,複製,必須標明文章的出處,標註原地址以及作者
http://mp.blog.csdn.net/postedit/79044988 作者:暮辰邪
使用配置中心注入application.properties
1、Client等jar包的製作
(1)Apollo
github下載地址:https://github.com/Lliangwenbo/apollo.git
Apollo檔案如下圖所示:
(2)其中最重要的檔案就是scripts,如下圖:
sql檔案是兩個mysql型別資料庫檔案,需要建立兩個型別資料庫,資料庫名字以sql檔名命名,使用者名稱密碼都為admin,
修改build.sh檔案中的資料庫資訊如下:
下面這幾個的地址都為相同地址,其實只需要一個就行:dev_meta=http://localhost:8080
此地址就是Apollo Client(即客戶的)部署的地址。dev_meta代表開發,fat_meta,uat_meta代表測試, pro_meta代表生產。
# apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/apolloconfigdb?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=root
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/apolloportaldb?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=root
# meta server url, different environments should have different meta server addresses
dev_meta=http://localhost:8080
fat_meta=http://someIp:8080
uat_meta=http://anotherIp:8080
pro_meta=http://yetAnotherIp:8080
配置好之後用git的Git Base Here啟動:在scripts目錄右鍵選擇:Git Base Here輸入:
sh build.sh
自動在maven本地倉庫中生成apollo的jar包
如果報錯可能是maven的環境變數沒有配置。windows系統下開啟配置環境變數
MAVEN_HOME=E:\maven3.3.9
path中新增%MAVEN_HOME%\bin
Ctrl+R 輸入cmd開啟視窗輸入mvn測試maven環境變數配置是否成功!
重新啟動:sh build.sh
啟動成功!!!
2、Cilent端 快速啟動外掛的下載
百度雲下載地址:
github下載地址:https://github.com/Lliangwenbo/apollo-build-scripts.git
檔案如下圖所示
demo.sh中指定兩個資料庫地址,剛才在第一步中已經詳細描述過,如下圖所示:
接下來在Windows系統建立:C:\opt\settings\server.properties檔案
如果是Linux系統,則建立/opt/settings/server.properties
檔案內容:env=dev
即指定開發環境,同第一步選擇的有關!!!!!!!千萬不要忘了!!!!
配置好之後用git的Git Base Here啟動:在scripts目錄右鍵選擇:Git
Base Here輸入:
sh ./demo.sh start
啟動成功!!!
3、專案配置:
(1),pom.xml 加入由第一步生成的jar包,記得版本和第一步生成的jar包保持一致,如下:
<!-- apollo 攜程apollo配置中心框架 -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>0.9.1</version>
</dependency>
<!-- apollo -->
(2)新增app.properties檔案
目錄如下:
內容僅新增:app.id=apollodemo
(3)在springboot啟動類上方加入兩行註解:@Configuration,@EnableApolloConfig。
@EnableApolloConfig是依賴@Configuration註解的
package com.feeling.apollo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.feeling.apollo.util.TestJavaConfigBean;
@Configuration
@EnableApolloConfig
@ComponentScan(basePackages = { "com.feeling.*" }) // 將該包下的檔案納入容器中
@EnableAutoConfiguration
public class ApolloDemo {
public static void main(String[] args) throws Exception {
SpringApplication.run(ApolloDemo.class, args);
}
}
4、client配置中心配置檔案
開啟http://localhost:8070/
使用者名稱預設:apollo
密碼:admin
點選建立專案,
應用id專案中寫第三部中建立的app.properties檔案中的app.id的值!!!其它隨便填.........
建立成果夠進入新建立的專案中:把專案中的application.properties去掉備註和空行復制到文字中
然後點擊發布!!!
然後把專案中的application.properties檔案刪掉
正常啟動!!!
5、最後補充內容:專案監聽配置中心的修改
(1)建立TestJavaConfigBean實體類監聽
package com.feeling.apollo.util;
import org.springframework.beans.factory.annotation.Value;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
public class TestJavaConfigBean {
@ApolloConfig("application")
private Config config; //inject config for namespace application
@Value("${test:test}")//如果配置中心沒有值,預設key為test的value值為test
private String name;
//config change listener for namespace application
@ApolloConfigChangeListener("application")
private void anotherOnChange(ConfigChangeEvent changeEvent) {
ConfigChange change = changeEvent.getChange("test");
System.out.println(String.format("Found change - key: %s, oldValue: %s,"
+ " newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}
}
(2)在springboot啟動類中加入紅色部分package com.feeling.apollo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.feeling.apollo.util.TestJavaConfigBean;
@Configuration
@EnableApolloConfig
@ComponentScan(basePackages = { "com.feeling.*" }) // 將該包下的檔案納入容器中
@EnableAutoConfiguration
public class ApolloDemo {
public static void main(String[] args) throws Exception {
SpringApplication.run(ApolloDemo.class, args);
}
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
}
(3)在配置中心給專案新增配置
然後1儲存、2釋出
再次啟動專案
(4)進入配置中心修改剛剛的test配置
value改成
然後可以看到eclipse中輸出:
這種配置方式能不修改一些普通的key和value值,像springboot啟動時就載入的類似資料庫埠號的一些引數是不能再不停機的情況下使用的。
普通的配置可以在不停機的情況下修改!!!