Apollo與SpringBoot集成

背景

在項目開發中,咱們常常會遇到這麼一種問題,就是開發、測試、線上環境鏈接的數據庫、redis等參數配置都是不同的,當咱們在開發環境把項目開發完成以後,就須要將代碼放到測試或者線上環境,因爲不一樣環境的參數是不同的,咱們還得將配置文件修改一遍,這樣很麻煩,如今出現一個能夠動態配置不一樣環境參數變量的工具,apollo,能夠很方便的就解決了咱們遇到的難題。java

Apollo是由攜程框架部門研發的分佈式配置中心,可以集中化管理不一樣環境、不一樣集羣的配置,配置修改後能實時的推送到應用端,而且具有規範的權限、流程治理等特性,適用於微服務配置場景mysql

github地址:https://github.com/ctripcorp/...git

搭建apollo服務器

Apollo的存儲工具是Mysql,因此要啓動apollo的服務,首先要安裝一套Mysql數據庫github

Apollo的服務端須要的jdk版本是1.8+

Apollo客戶端須要的jdk的版本是1.7+redis

Mysql須要的版本號是5.6.5+spring

安裝Mysql數據庫

具體安裝過程不說了sql

安裝apollo項目

下載quick start安裝包

下載地址:https://github.com/nobodyiam/...數據庫

解壓並執行sql文件

將下載下來的mater.zip進行解壓,並執行sql文件夾下面的兩個sql腳本緩存

source {本地目錄}/apollo-build-scripts-master/sql/apolloconfigdb.sql
source {本地目錄}/apollo-build-scripts-master/sql/apolloportaldb.sql

執行完以後查看是否執行成功springboot

select `NamespaceId`, `Key`, `Value`, `Comment` from ApolloConfigDB.Item;
配置數據庫鏈接信息

修改demo.sh腳本

#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=密碼(若是沒有密碼,留空便可)
啓動apollo服務

因爲apollo會在本地啓動三個服務,分別會使用到8070,8080,8090三個端口,因此須要看看這三個端口是否被佔用

lsof -i:8070
lsof -i:8080
lsof -i:8090

執行腳本

./demo.sh start

會出現下面的日誌信息

==== starting service ====
Service logging file is ./service/apollo-service.log
Started [25980]
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 [26217]
Waiting for portal startup........
Portal started. You can visit http://localhost:8070 now!
登陸apollo

初始密碼是apollo/admin

配置apollo

進入管理界面,以下圖所示
圖片描述

點擊SampleApp進入AppId是SampleApp的管理界面

分佈式環境下Apollo服務搭建

Apollo客戶端配置

添加apollo-client對應的jar包
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.1.2</version>
</dependency>
在pom.xml中添加以下配置
<profiles>
    <profile>
        <id>local</id>
        <properties>
            <!-- 分佈式部署的時候,apollo-configservice和apollo-adminservice須要把本身的ip和端口號註冊到Meta Server中,也就是apollo-configservice中 -->
            <!-- Apollo客戶端和Protal會從Meta Server獲取全部會用到服務的Ip和端口號,而後就直接去訪問所需服務。注意,服務的Ip和端口號必須能讓Portal和客戶端訪問 -->
            <apollo.meta>http://localhost:8080</apollo.meta>
            <apollo.appId>template_portal</apollo.appId>
            <apollo.cacheDir>D:\apollo\data</apollo.cacheDir>
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <properties>
            <apollo.meta>http://ip:8080</apollo.meta>
            <apollo.appId>template_portal</apollo.appId>
            <apollo.cacheDir>D:\apollo\data</apollo.cacheDir>
        </properties>
        <!-- 默認激活開發環境的參數配置,要想使用其它環境的參數配置,須要在mvn package的時候指定 -Puat 或者 -Ppro -->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>uat</id>
        <properties>
            <apollo.meta>http://ip:8080</apollo.meta>
            <apollo.appId>template_portal</apollo.appId>
            <apollo.cacheDir>/opt/data/</apollo.cacheDir>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <properties>
            <apollo.meta>http://ip1:8080,http://ip2:8080</apollo.meta>
            <apollo.appId>template_portal</apollo.appId>
            <apollo.cacheDir>/opt/data/</apollo.cacheDir>
        </properties>
    </profile>
</profiles>

注意:configService和Meta Server基本上都在一個jvm中,不一樣環境(開發、測試、線上)對應的configService服務是不同的,因此配置的meta server地址也是不同的

注意:若是configService服務註冊到MetaServer服務的IP和端口號是內網地址,咱們在外網拉取到內網IP和端口號是訪問不通的,這個時候咱們能夠在啓動時配置一下configService服務的外網IP和端口號:-Dapollo.configService=http://外網IP:端口號 ,經過這種方式,咱們就能夠直接去訪問配置的服務拉取配置信息了

注意:咱們在配置了不一樣的環境下使用不一樣的配置參數,那麼開發環境、仿真環境、線上環境須要在Jenkins啓動打包的時候,須要配置上參數-Ppro或者-Puat,若是想使用本地緩存着的配置,使用參數配置:-Denv=Local

若是在settings.xml中定義profile設置默認激活時的配置以下:

<activeProfiles>  
    <activeProfile>profileTest1</activeProfile> 
    <activeProfile>profileTest2</activeProfile><!-- 後面的參數值會覆蓋前面的參數值,若是都配置了某個參數值的話 -->
</activeProfiles>

關於profile具體配置能夠參考博客:https://elim.iteye.com/blog/1...

敏感參數信息加密
  1. 首先須要一個加密用的字符串,salt字符串,最好長一點,好比ED3C9ADB0A53474098F594D0FD561E9E

單首創建一個配置文件,好比encryptor.properties,這個配置文件是不會放到apollo上進行管理的

裏面的配置內容以下:

jasypt.encryptor.password=ED3C9ADB0A53474098F594D0FD561E9E

而後配置springboot加載這個配置文件

@PropertySource(value = {"classpath:prop/encryptor.properties"}, name = "encryptor.properties", ignoreResourceNotFound = false)
@Configuration
public class WebMvcConfigurerInitializer extends WebMvcConfigurerAdapter {
  1. 導入加密的jar包
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

jdk7版本的就使用下面的jar包

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>1.5-java7</version>
</dependency>
  1. 加須要加密的字符串信息使用下面代碼進行加密處理
public static void main(String[] args) {
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    textEncryptor.setPassword("ED3C9ADB0A53474098F594D0FD561E9E");
    System.out.println("加密後:"+textEncryptor.encrypt("${須要加密的字符串}"));
    System.out.println("解密後:"+textEncryptor.decrypt("${須要解密的字符串}"));
}

或者使用jar包進行加密解密操做,jar包路徑:D:installedmavenlocal_repositoryorgjasyptjasypt1.9.2

// 加密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=ED3C9ADB0A53474098F594D0FD561E9E algorithm=PBEWithMD5AndDES input=要加密的字符串

// 解密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI password=ED3C9ADB0A53474098F594D0FD561E9E algorithm=PBEWithMD5AndDES input=要解密的字符串

最後發現用這兩種方式加密出來的字符串每次都不同,可是都是可使用的。

  1. 修改apollo配置中的關鍵信息

如:

spring.datasource.username=ENC(加密後的字符串)
spring.datasource.password=ENC(加密後的字符串)
相關文章
相關標籤/搜索