Apollo配置中心搭建 & SpringBoot整合Apollo客戶端

我的博客:zhenganwen.topjava

Apollo配置中心介紹

Apollo配置中心介紹mysql

Apollo配置中心搭建

配置中心包括server seviceadmin serviceportal web三個項目。其中apollo portal是一個web應用,運維人員可經過它進行配置的編輯和發佈;server默認暴露8090apollo portal提供服務;config server則經過8080端口爲apollo client(也即應用)提供服務(如定時拉取配置信息、監聽apollo portal的發佈消息)。git

server serviceadmin service都須要鏈接數據庫。github

環境準備

  • centos6.7 desktopweb

    • jdk1.8+
    • 確保正確的網絡配置以使虛擬機能和本地通訊,網絡配置可參考 Linux問題彙總
  • mysql5.6.5+spring

    • 能夠是本地的,也能夠是虛擬機上的。若是是本地的,注意安裝在虛擬機上的配置中心可否到,能夠配置root使用123456從任何主機鏈接到本地mysql服務器sql

      mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
      mysql>FLUSH RIVILEGES
      複製代碼
  • 下載配置中心安裝包apollo-build-scripts-masterdocker

  • 下載apollo client依賴包apollo-mastershell

安裝步驟

1.建立數據庫

apollo-master\scripts\sql下的兩個sql文件在mysql中執行一遍,會生成兩個數據庫數據庫

2.更改配置中心啓動腳本

apollo-build-scripts-master.zip上傳到centos/usr/local/software(目錄不存在可自行建立),unzip apollo-build-scripts-master.zip解壓(若沒有安裝可經過yum -y install unzip安裝unzip命令)

修改啓動腳本apollo-build-scripts-master/demo.sh(將鏈接數據庫的IP、用戶名、密碼修改成你本身的,個人mysql是裝在本地的,而本地的vmnet8ip配置爲了192.168.101.2,關於宿主機和虛擬機的通訊以及IP配置等可參考Linux問題彙總

# apollo config db info
apollo_config_db_url=jdbc:mysql://192.168.102.2:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=123456
 # apollo portal db info
apollo_portal_db_url=jdbc:mysql://192.168.102.2:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=123456
複製代碼

3.經過啓動腳本啓動apollo配置中心

./demo.sh start,若出現以下提示則說明啓動成功

[root@localhost apollo-build-scripts-master]# ./demo.sh start
==== starting service ====
Service logging file is ./service/apollo-service.log
Started [29110]
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 [29304]
Waiting for portal startup.......
Portal started. You can visit http://localhost:8070 now!
複製代碼

啓動失敗的緣由可到apollo-build-scripts-master/service/apollo-service.log查看,常見問題以下

  • 給虛擬機設置的運行內存太低,因爲阿波羅配置中心會啓動三個項目(config serviceadmin serviceportal web),所以建議將內存給到2G

  • 若是你的mysql安裝在本地,而虛擬機的阿波羅配置中心再啓動時須要鏈接該數據庫,虛擬機防火牆、mysql訪問權限等緣由會致使鏈接失敗,進而致使啓動失敗

    防火牆關閉命令:service iptables stop

若是你啓動成功,就能夠經過http://192.168.102.101:8070來訪問portal web了(個人虛擬機IP爲192.168.102.101

至此,阿波羅的配置中心就搭建完成了

SpringBoot整合Apollo客戶端實現從配置中心讀取配置信息及配置中心更改配置後通知客戶端實時更新

1.安裝apollo依賴到本地或私服

咱們的應用要想鏈接配置中西,須要引入apollo-clientapollo-core這兩個依賴,而這二者在中心倉庫是找不到的,所以咱們須要打包到本地。這一步雙擊apollo-master\scripts\build.bat構建腳本便可。(事先確保本地已安裝配置好了maven環境變量mvn

2.建立maven項目,引入SpringBoot和apollo客戶端依賴

複製如下代碼到你的pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.RC1</spring-cloud.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- apollo 攜程apollo配置中心框架 -->
    <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-core</artifactId>
        <version>1.0.0</version>
    </dependency>


    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>copy-conf</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <outputDirectory>${project.build.directory}/ext/conf</outputDirectory>
                        <resources>
                            <resource>
                                <directory>ext/conf</directory>
                                <includes>
                                    <include>logback.xml</include>
                                </includes>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.5.201505241946</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-prepare-agent-integration</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.3</version>
            <configuration>
                <imageName>hy_uav_gateway</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                        <include>ext/conf/logback.xml</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
複製代碼

3.在portal web中新建項目

訪問http://192.168.102.101:8070,可以使用默認提供的用戶名apollo和密碼admin來登陸,而後建立項目(這裏的項目就至關於對應一個微服務應用)

阿波羅會爲你的這個項目默認生成一個DEV環境,你能夠經過新增配置按鈕來新增配置項、經過發佈按鈕發佈新建的配置項。阿波羅客戶端啓動時會讀取發佈狀態的配置項,若是在應用運行時發佈修改過的或新增的配置項,應用也會經過tcp長鏈接監聽到並及時同步。

這裏我新增一個name的配置項

而後點擊發佈按鈕。

4.添加apollo默認讀取的配置文件

新建src/main/resources/META-INFO/app.properties

app.id=appId_1001	//新建項目時填寫的應用Id
複製代碼

新建src/main/resources/apollo-env.properties

local.meta=http://192.168.102.101:8080	//將IP更改成你虛擬機的IP
dev.meta=http://192.168.102.101:8080	//將IP更改成你虛擬機的IP
fat.meta=${fat_meta}
uat.meta=${uat_meta}
lpt.meta=${lpt_meta}
pro.meta=${pro_meta}
複製代碼

新建C:\opt\settings\server.properties用來指定讀取配置中心的哪一個環境

env=DEV
複製代碼

這個路徑和文件名不能有絲毫差錯,應用啓動時,阿波羅客戶端會讀取該文件

5.經過@EnableApolloConfig啓用apollo客戶端,經過@Value註解讀取配置信息

package top.zhenganwen.demo.apollo;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableApolloConfig
@RestController
@SpringBootApplication
public class Application {

    @Value("${name:test}")//讀取不到,默認賦值爲test,避免應用啓動報錯
    String name;

    @RequestMapping("apollo")
    public String apollo() {
        return name;
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

複製代碼

6.測試

  • 訪問http://localhost:8080/apollo,瀏覽器返回zhenganwen,應用從配置中心讀取配置信息成功。
  • protal web中編輯name配置項,更改valuezhangsan,並點擊發佈,再次訪問http://localhost:8080/apollo,返回zhangsan,熱更新測試成功
相關文章
相關標籤/搜索