我的博客:zhenganwen.topjava
Apollo配置中心介紹mysql
配置中心包括server sevice
、admin service
、portal web
三個項目。其中apollo portal
是一個web
應用,運維人員可經過它進行配置的編輯和發佈;server
默認暴露8090
爲apollo portal
提供服務;config server
則經過8080
端口爲apollo client
(也即應用)提供服務(如定時拉取配置信息、監聽apollo portal
的發佈消息)。git
server service
和admin service
都須要鏈接數據庫。github
centos6.7 desktop
web
jdk1.8+
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-master
docker
下載apollo client
依賴包apollo-master
shell
將apollo-master\scripts\sql
下的兩個sql
文件在mysql
中執行一遍,會生成兩個數據庫數據庫
將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
是裝在本地的,而本地的vmnet8
的ip
配置爲了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
複製代碼
./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 service
、admin service
、portal web
),所以建議將內存給到2G若是你的
mysql
安裝在本地,而虛擬機的阿波羅配置中心再啓動時須要鏈接該數據庫,虛擬機防火牆、mysql
訪問權限等緣由會致使鏈接失敗,進而致使啓動失敗防火牆關閉命令:
service iptables stop
若是你啓動成功,就能夠經過http://192.168.102.101:8070
來訪問portal web
了(個人虛擬機IP爲192.168.102.101
)
至此,阿波羅的配置中心就搭建完成了
咱們的應用要想鏈接配置中西,須要引入apollo-client
和apollo-core
這兩個依賴,而這二者在中心倉庫是找不到的,所以咱們須要打包到本地。這一步雙擊apollo-master\scripts\build.bat
構建腳本便可。(事先確保本地已安裝配置好了maven
環境變量mvn
)
複製如下代碼到你的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>
複製代碼
訪問http://192.168.102.101:8070
,可以使用默認提供的用戶名apollo
和密碼admin
來登陸,而後建立項目(這裏的項目就至關於對應一個微服務應用)
阿波羅會爲你的這個項目默認生成一個DEV
環境,你能夠經過新增配置按鈕
來新增配置項、經過發佈
按鈕發佈新建的配置項。阿波羅客戶端啓動時會讀取發佈狀態的配置項,若是在應用運行時發佈修改過的或新增的配置項,應用也會經過tcp
長鏈接監聽到並及時同步。
這裏我新增一個name
的配置項
而後點擊發佈
按鈕。
新建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
複製代碼
這個路徑和文件名不能有絲毫差錯,應用啓動時,阿波羅客戶端會讀取該文件
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);
}
}
複製代碼
http://localhost:8080/apollo
,瀏覽器返回zhenganwen
,應用從配置中心讀取配置信息成功。protal web
中編輯name
配置項,更改value
爲zhangsan
,並點擊發佈
,再次訪問http://localhost:8080/apollo
,返回zhangsan
,熱更新測試成功