zoukankan      html  css  js  c++  java
  • Spring Cloud:Config基础知识

    背景

    微服务意味着要将单体应用拆分成一个个自服务,每个服务的力度相对较小,因此系统中会存在大量服务,每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
    Spring Cloud Config为微服务架构中的微服务提供集中化外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

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

    服务端搭建

    在git上创建一个仓库,并且拉取到本地,git clone git@github.com:JGZY/springcloud-config.git,这里不再赘述详细过程。
    hosts文件增加映射

    127.0.0.1 config3344.com
    

    pom部分依赖

            <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>
            </dependency>
    

    yml配置:

    server:
      port: 3344
    
    eureka:
      client:
        #是否将自己注册到Eureka Server 默认为true
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置true才能配合ribbon做负载均衡
        fetch-registry: true
        service-url:
          #设置eureka server交互的地址查询服务和注册服务都需要依赖这个地址
          defaultZone: http://localhost:7001/eureka
          #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
      instance:
        instance-id: config3344
        #访问路径可以显示IP地址
        prefer-ip-address: true
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              uri: git@github.com:JGZY/springcloud-config.git
              #搜索目录
              search-paths:
                - springcloud-config
          #读取分支
          label: master
    

    springboot主启动类,加上@EnableConfigServer注解

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

    启动后,我们直接访问http://config3344.com:3344/master/config-dev.yml

    说明可以访问到我在git中的配置文件。
    配置文件有以下几种访问方式:(官网)

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties
    

    客户端配置

    部分pom依赖:

            <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>
            </dependency>
    

    创建bootstrap.yml
    说明:
    application.yml是用户级的资源配置项,bootstrap.yml是系统级的,优先级更高。
    Spring Cloud会创建一个 bootstrap context,作为Spring应用的Application context的上下文,初始化的时候,bootstrap context负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的environment。
    bootstrap属性具有高优先级,默认情况下,他们不会被本地配置覆盖。bootstrap context和application context有着不同的约定,所以新增一个bootstrap.yml文件,保证bootstrap context和application context配置的分离。

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          name: config
          profile: dev #上述三个综合,master分支上config-dev.yml的配置文件被读取  http://localhost:3344/master/config-dev.yml
          uri: http://localhost:3344
    
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    

    controller测试配置是否可以读取到:

    @RestController
    public class ConfigClientController {
    
        @Value("${config.info}")
        private String info;
    
        @GetMapping("/config/client/info")
        public String getInfo(){
            return info;
        }
    }
    

    访问http://localhost:3355/config/client/info,发现git上的配置可以成功的读取到。

    config动态刷新

    当我们修改了git上的配置后,发现config配置中心端配置立马就能读取到改变,但是client端不能,读取的还是旧配置。
    下面介绍config动态刷新
    1.client端需要引入actuator依赖。

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    

    2.client暴露监控端点(yml配置)

    management:
      endpoints:
        web:
          exposure:
            include: "*"
    

    3.在业务类上面加上@RefreshScope注解

    @RefreshScope
    @RestController
    public class ConfigClientController {
        @Value("${config.info}")
        private String info;
        @GetMapping("/config/client/info")
        public String getInfo(){
            return info;
        }
    }
    

    4.然后每一次修改git上的配置后,执行下面命令,手动刷新配置,然后客户端就能读取到最新配置了。这样就避免重启服务的麻烦了。

    curl -X POST "http://localhost:3355/actuator/refresh"
    

    但是又有了新的麻烦。现在只有一个config的客户端,假如又有一堆config客户端,每个微服务都执行一次post请求,手动刷新,也比较麻烦。由此引入Spring Cloud Bus消息总线。

  • 相关阅读:
    FJNU 1151 Fat Brother And Geometry(胖哥与几何)
    FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)
    FJNU 1159 Fat Brother’s new way(胖哥的新姿势)
    HDU 3549 Flow Problem(最大流)
    HDU 1005 Number Sequence(数列)
    Tickets(基础DP)
    免费馅饼(基础DP)
    Super Jumping! Jumping! Jumping!(基础DP)
    Ignatius and the Princess IV(基础DP)
    Keywords Search(AC自动机)
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/13623937.html
Copyright © 2011-2022 走看看