spring cloud config 搭建配置中心
简介:
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git存放配置文件。以git仓库为例做一套示例,与大家一起学习。
项目结构:
首先搭建一个服务注册中心,用来注册config server与config client
在pom.xml文件添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
application.yml配置文件配置如下:
server: port: 8761 spring: application: name: eureka-server eureka: instance: hostname: 127.0.0.1 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启始类中添加标签@EnableEurekaServer
@SpringBootApplication @EnableEurekaServer public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } }
服务注册中心搭建完成。
搭建config server:
pom.xml文件添加依赖:
<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> <version>2.1.1.RELEASE</version> </dependency>
bootstrap.yml配置文件的配置如下:
server: port: 8881 eureka: instance: prefer-ip-address: true client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: cloud-config-server cloud: config: server: git: #仓库地址 uri: https://github.com/BeiMingHFishNameK/paascloud-master-config-rep.git username: password: #仓库在本地的缓存地址 basedir: d:/paascloud-master-config-rep management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
注:关于这里以及接下来config client为什么用bootstrap.yml而不用application.yml或者application.properties,是因为bootstrap.yml优先于 application.yml或者application.properties 被加载,如果直接写在application.yml会导致项目启动时无法读取到配置中心配置文件导致报错。
关于Git仓库,可自行百度在GitHub创建一个或者引用原有的仓库,将仓库地址复制过来就行了,需参照本文配置的格式引入。
启始类中添加标签:@EnableConfigServer、@EnableEurekaClient、
@EnableDiscoveryClient
@SpringBootApplication @EnableConfigServer @EnableEurekaClient @EnableDiscoveryClient public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
配置中心搭建完成。
搭建config client:
pom.xml文件添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
bootstrap.yml配置文件配置如下:
server: port: 8801 eureka: instance: prefer-ip-address: true client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: config-client cloud: config: discovery: enabled: true #在这里指向配置中心,才能读取到配置中心的配置文件,只有配置中心向注册中心注册了才能这么写 service-id: cloud-config-server profile: dev bus: enabled: true trace: enabled: true management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
启始类中添加标签:@EnableEurekaClient
@SpringBootApplication @EnableEurekaClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
写一个控制器测试读取配置中心的配置:
@RestController public class ConfigClientController { @Value("${server.version}") private String version; @RequestMapping("/version") public String sayHi(){ return version; } }
config client 在配置中心的配置如下:
关于config client 在配置中心中的配置文件命名规则如下:
{application}-{profile}.yml:其中{application}对应客户端的应用名称(spring.application.name),{profile}对应客户端的spring.profiles.active
项目启动:
先启动eurekaserver,再启动config server,最后启动config client。
访问:http://localhost:8801/version
显示如图所示即为成功。
这样就可以将必要的配置放到配置中心,不能再去定位到某个项目中去修改。
- 配置中心搭建(spring-cloud-config-server)
- spring cloud config整合gitlab搭建分布式的配置中心
- 【SpringCloud】搭建高可用分布式配置中心(Spring Cloud Config)(二)全过程详解(自动刷新)解决webhooks 400错误
- Spring Cloud Config 配置中心搭建
- 配置中心搭建(spring-cloud-config-server)
- 【SpringCloud】搭建高可用分布式配置中心(Spring Cloud Config)(一)全过程详解(手动刷新)Spring Cloud 2.0.2.RELEASE
- SpringCloud Config-----1、搭建分布式配置中心
- SpringCloud实战之初级入门(三)— spring cloud config搭建git配置中心
- spring cloud config整合gitlab搭建分布式的配置中心
- 详解spring cloud config整合gitlab搭建分布式的配置中心
- 第六篇: 分布式配置中心(Spring Cloud Config)
- 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十三):配置中心(Config、Bus)
- Spring Cloud Learning | 第十篇:分布式配置中心(Config)
- 第七章:高可用的分布式配置中心(Spring Cloud Config)
- 一起来学SpringCloud之 - 分布式配置中心(Native - Config)
- SpringCloud之搭建配置中心
- SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)