springcloud微服务实战 学习笔记四 分布式配置中心

介绍

Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。

依赖

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
         
    
    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-config-server
        
      
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.SR1
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

配置文件

    spring.application.name=config-server
    server.port=1201

    # git仓库配置
    spring.cloud.config.server.git.uri=https://github.com/zhumeilu/springcloud-config-repo-demo/
    #spring.cloud.config.server.git.searchPaths=Chapter1-1-8/config-repo
    spring.cloud.config.server.git.username=
    spring.cloud.config.server.git.password=

Application.java

    @EnableConfigServer  //开启配置中心服务
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            new SpringApplicationBuilder(Application.class).web(true).run(args);
        }
    
    }
  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

{label}对应Git上不同的分支,默认为master,{application}为配置的文件名称,{profile}为环境
访问http://localhost:1201/config-client/dev/master就可以看到配置文件了

客户端

构建一个简单的springboot应用
配置文件

    spring.application.name=eureka-consumer
    server.port=4444
    eureka.instance.hostname=localhost
    #服务注册中心
    #eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
    spring.cloud.config.profile=dev
    spring.cloud.config.name=config-client
    spring.cloud.config.uri=http://localhost:1201/
    spring.cloud.config.label=master

在controller中添加

@Value("${from}")
String from;
@GetMapping("/getFrom")
public String getFrom(){
    return from;
}

访问http://localhost:4444/getForm
得到配置文件中的form的值

在客户端使用配置中心时,spring.cloud.config.name和spring.application.name都可以确定配置文件名称,最好使用spring.cloud.config.name=config-client来确定配置文件名称,不要使用spring.application.name=config-client来确定。如果都存在的话,默认使用spring.cloud.config.name

配置中心集群

将多个配置中心注册为服务,使其实现集群和负载均衡

服务端

依赖
在之前的基础上添加依赖

    
        org.springframework.cloud
        spring-cloud-starter-eureka
    

配置文件
在之前的基础上添加注册中心

# 配置服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

Application.java

添加注解@EnableDiscoveryClient //注解使其成为服务

客户端

配置文件
在原来的基础是添加

#开启通过服务来访问Config Server的功能
spring.cloud.config.discovery.enabled=true
#指定Config Server注册的服务名
spring.cloud.config.discovery.serviceId=config-server

当git上的配置文件更新时,可以在项目中添加spring-boot-starter-actuator模块,在需要刷新的controller上添加注解@RefreshScope然后访问/refresh,需要注意的是springboot中默认开启安全策略,无法通过fidder或者postman之类的工具调用/refresh,所以需要设置management.security.enabled= false

你可能感兴趣的:(springcloud微服务实战 学习笔记四 分布式配置中心)