Spring Cloud(六)Config配置中心

简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。

Spring Cloud Config致力于为分布式系统中的外部化配置提供支持 ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

一个配置中心提供的核心功能

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 配置文件修改之后,可以快速的生效
  • 可以进行版本管理
  • 支持大的并发查询
  • 支持各种语言

Spring Cloud Config可以完美的支持以上所有的需求。

使用Config实现的配置中心-配置文件存储在git

  • 创建配置中心的服务端

添加pom依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

主程序中添加注解@EnableConfigServer,启用Config Server作为配置中心

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
} 

 修改application.properties配置文件

spring.application.name=config-server
server.port=8888
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
spring.cloud.config.server.git.uri=https://github.com/xiaoyuer2012/springcloud/  // 配置git仓库地址
spring.cloud.config.server.git.searchPaths=config-data  // 配置查找配置的路径

测试

启动应用,访问http://localhost:8888/springcloud-test/dev,应能看到如下输出内容,说明服务启动成功。

{"name":"springcloud-test","profiles":["dev"],"label":null,"version":"d607a1236eb206221915cc3bdca470eef6f7b32d","state":null,"propertySources":[{"name":"https://github.com/xiaoyuer2012/springcloud//config-data/springcloud-test-dev.properties","source":{"hello":"hello,world!"}}]}
  • 集成配置中心的客户端

添加pom依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-client</artifactId>
</dependency>

主应用程序不需要做特殊的修改

修改application.properties

server.port=8082
spring.application.name=msa-weather-city-service
eureka.client.service-url.defaultZone = http://localhost:8761/eureka/

spring.cloud.config.profile = dev
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.name=springcloud-test

测试代码

@RestController
public class HelloController {
   @Value("${hello}")
   private String hello;  // 获取配置文件的hello变量值
   @RequestMapping("/hello")
   public String hello() {
       return hello;
   }
} 

启动客户端程序,访问 http://localhost:8082/hello,可看到如下截图,即为配置文件中配置的内容, config集成成功。

使用Config实现的配置中心-配置文件存储在本地

配置中心放置到本地与放置到服务器的搭建步骤是基本一致的,只是config-server配置中心服务端的配置有些区别,具体见下图。

spring.application.name=config-server
server.port=8888
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
# 配置文件存储在git
#spring.cloud.config.server.git.uri=https://github.com/xiaoyuer2012/springcloud/
#spring.cloud.config.server.git.searchPaths=config-data
# 配置文件存储在本地
# 本地文件
spring.profiles.active=native  
#本地文件访问路径
spring.cloud.config.server.native.searchLocations=classpath:/config 

将本地的配置放开,同时在相应的目录放置配置文件即可。修改完重启服务,访问http://localhost:8888/spring-test-dev.properties,可以看到下面的内容,即代表配置成功。

{"name":"springcloud-test","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/config/springcloud-test-dev.properties","source":{"hello":"hello,world!"}}]}

记得目录下面放置配置文件

思考 

今天,我们把Spring Cloud的Config组件进行了集成,到目前为止,我们的程序已经集成了Eureka服务注册与发现,Zuul服务网关以及Config配置中心,我们已经基本实现了分布式系统的负载均衡和配置的统一管理。但是如下的问题还是需要我们思考一下:

如果某个服务因为逻辑原因或者异常原因,导致长时间无返回怎么办。大量的请求积压过来,会不会导致服务的崩溃?

配置中心的配置如果发生了变化,我们就需要重启服务吗?

这些问题Spring Cloud都提供了相应的机制去解决这些问题。我们将在后面进行详细的描述。

本文源码地址:https://github.com/xiaoyuer2012/springcloud

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值