一、概述
(1)背景
微服务意味着将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中出现大量的服务。由于每个服务都需要配置必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。Spring Cloud提供了ConfigServer来解决这个问题,我们每一个微服务自己都带着application.yml,上百个配置文件的管理……接下来你会疯的。
(2)Config是什么?
SpringCloud Config为微服务架构中的微服务提供了集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端和客户端:
服务端:也被称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
客户端:通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样有助于对环境配置进行版本管理,并且可以用过git客户端工具来方便的管理和访问配置内容。
二、SpringCloud Config服务端配置
(1)github新建仓库,名为:microservice-config
我这里已经建好地址:https://github.com/Simple-Coder/microservice-config
①根据ssh地址:[email protected]:Simple-Coder/microservice-config.git,在本地硬盘目录新建git仓库并clone
②在刚刚创建的本地仓库中新疆:application.yml,添加如下配置并通过git客户端推送到github
spring:
profiles:
active:
- dev
---
spring:
profiles: dev #开发环境
application: microservice-config-dev
---
spring:
profiles: test #测试环境
application:
name: microservice-config-test
#保存为utf-8格式
③新建Module模块:microservice-config3344(配置中心模块),Maven工程结构图如下:
④microservice-config模块:pom依赖、application.yml、启动类
<dependencies> <!--config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies>
server:
port: 3344
spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: [email protected]:Simple-Coder/microservice-config.git #github上的仓库地址
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
⑤启动microservice-config测试:
扫描二维码关注公众号,回复:
7340118 查看本文章