SpringCloud Config 配置中心
一.SpringCloud Config 配置中心是什么?
SpringCloud Config 是微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供一个中心化的外部配置
.
二.怎么玩?
SpringCloud Config 分为服务端和客户端
- 服务端: 称为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并为客户端提供和获取配置信息,加密/解密信息等访问接口.
- 客户端: 是指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来储存配置信息,这样有助于对配置文件版本进行统一管理
三.能干嘛?
- 集中管理配置文件
- 不同环境不同配置,动态化配置更新,分环境部署比如dev/test/prod/beta/release
- 项目运行期间动态调整配置,不需要再每个服务节点上更改配置文件,服务会向配置中心获取统一配置
- 当配置发生变化时,服务中心不用重启便能感知到配置文件的变化,并应用新的配置.
- 可以使用POST/curl来进行访问刷新
四.与gitee整合
-
在自己的gitee新建一个空的仓库
-
复制自己仓库地址
https://gitee.com/javacse/springcloud-config.git
- 使用命令把仓库克隆到本地
git clone https://gitee.com/javacse/springcloud-config.git
-
在仓库中添加配置文件config-dev.yml
-
上传文件到git仓库
git add --all
git commit -m "提交注释"
git push origin master
五.新建配置中心服务模块
因为项目为模块化项目,需要提前构建好父级模块,这里就不依次构建,父级项目统一使用POM坐标如下配置
- 父级pom文件坐标
<modules>
<module>cloud-cheetos-config-center-3344</module>
<module>cloud-cheetos-config-client-3355</module>
<module>cloud-cheetos-config-client-3366</module>
</modules>
<packaging>pom</packaging>
<parent>
<!--spring boot 2.2.2-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<!-- 统一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
<dependencyManagement>
<dependencies>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
-
新建项目模块命名起名 cloud-cheetos-config-center-3344
-
引入相关子pom坐标
<dependencies>
//此pom为config-server配置文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 配置yml配置文件
server:
port: 3344 #项目端口号
spring:
application:
name: cloud-config-center #注册到Eureka微服务名
cloud: # config-server配置
config:
server:
git:
uri: 填写你自己的gith地址
search-paths:
- springcloud-config #分支路径
label: master #分支节点
eureka: #Eureka配置
client:
service-url:
defaultZone: http://localhost:7001/eureka
- 配置主启动类,@EnableConfigServer注解为开启配置中心注解
@SpringBootApplication
@EnableConfigServer
public class MainAppConfigCenter3344 {
public static void main(String[] args) {
SpringApplication.run(MainAppConfigCenter3344.class,args);
}
}
- 项目启动后访问如下地址,就会在浏览器的页面上显示配置文件信息
http://localhost:3344/master/config-dev.yml