Spring Cloud核心组件(六):Config

一. 分布式配置中心Config是什么?
分布式系统面临的配置问题.量变引起质变。
在这里插入图片描述
在这里插入图片描述

Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中性化的外部配置。

Spring Cloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用户连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

二. Spring Cloud Config能干什么?
在这里插入图片描述
在这里插入图片描述

三. Spring Cloud Config服务端与Github通信
构建步骤示例。
(1) 在GitHub上创建一个名为microservice-config的新的Repository
(2) git clone https://github.com/Go007/microservice-config.git到自己的硬盘目录上
(3) 创建如下的application.yml

Spring: 
  profiles: 
    active: 
    - dev
---
Spring: 
  profiles: dev #开发环境
  application: 
    name: microservice-config-hong-dev 
---
Spring: 
  profiles: test #测试环境
  application: 
    name: microservice-config-hong-test

#注意,一定要保存为UTF-8格式

(4) 提交上述文件
在这里插入图片描述

(5) 新建Module模块microservice-config-3344

@SpringBootApplication
@EnableConfigServer
public class Application_Config_3344
{
    public static void main( String[] args )
    {
        SpringApplication.run(Application_Config_3344.class, args);
    }
}
server: 
  port: 3344 
  
spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: git@github.com:Go007/microservice-config.git #GitHub上面的git仓库名字

(6) 通过Config微服务从GitHub上获取配置内容
在hosts文件中添加映射 127.0.0.1 config-3344.com
http://config-3344.com:3344/application-dev.yml
http://config-3344.com:3344/application-test.yml
http://config-3344.com:3344/application-xxx.yml (不存在的配置)

(7) 配置读取规则
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

四.Spring Cloud Config Client通过Config Server获得GitHub上de配置
(1)假设在GitHub仓库microservice-config/microservice-config-client.yml文件内容如下:

spring:
  profiles: 
    active:
    - dev
---
server:
  port: 8201
spring:
  profiles: dev #开发环境
  application: 
    name: microservice-config-client
eureka:
  client:
    service:
     defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
  port: 8202
spring:
  profiles: test
  application:
    name: microservice-config-client
eureka:
  client:
    service:
     defaultZone: http://eureka-test.com:7001/eureka/

#注意,一定要保存为UTF-8格式     

(2) 新建Spring Cloud Config客户端服务

 <!-- SpringCloud Config客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

bootstrap.xml

spring:
  cloud:
    config:
      name: microservice-config-client #需要从github上读取的资源名称,注意没有yml后缀名
      profile: test   #本次访问的配置项
      label: master   
      uri: http://config-3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址 
@RestController
public class ConfigClientRest
{

	@Value("${spring.application.name}")
	private String applicationName;

	@Value("${eureka.client.service-url.defaultZone}")
	private String eurekaServers;

	@Value("${server.port}")
	private String port;

	@RequestMapping("/config")
	public String getConfig()
	{
		String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
		System.out.println("******str: " + str);
		return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
	}
}

在hosts文件添加映射:127.0.0.1 client-config.com

(3)先启动之前创建的Spring Cloud Config Server服务,再启动Client服务
在这里插入图片描述
在这里插入图片描述

可以看到,我们的Client服务可以通过Server服务从GitHub仓库上获取相应的配置信息。

特别说明:
在这里插入图片描述

五. Spring Cloud Config配置演示与策略切换
做一个Eureka服务+产品微服务,将两个微服务的配置统一由GitHub获得实现统一配置分布式管理,完成多环境的变更。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值