我的博客: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
,热更新测试成功