Spring Cloud Config

概述:统一管理配置的一个服务,可以解决因为配置需要更新而重启服务的问题,可以方便的统一集中管理各个服务的配置,
      可以见配置储存至本地,也可以存储在远程服务器上,git、svn等。
      可支持http地址请求,但不建议,不利于负载均衡和高可用集群,所以一般使用Eureka通信。

    spring cloud config分为两个部分:
        服务端: 配置服务端,服务管理配置信息
        客户端:客户端调用server端暴露接口获取配置信息

服务端:
    1、pom.xml
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-config-server
        

    2、application.yml
        1.配置文件在git等环境下
            server:
              port: 8040
            spring:
              application:
                name: config-server
              cloud:
                config:
                  server:
                    git:
                      uri: https://github.com/paomoopt/cloudConfigProperties #仓库地址
                      search-paths: respo #仓库路径
                      default-label: master #仓库的分支
                      username: root #仓库用户名,如果是公开仓库则不需要
                      password: root #仓库密码,如果是公开仓库则不需要
            eureka:
              client:
                serviceUrl:
                  defaultZone: http://192.168.1.200:8070/eureka/
        2.配置文件在本地
            server:
              port: 8040
            spring:
              application:
                name: config-server
              profiles:
                active: native #标记配置文件在本地
              cloud:
                config:
                  server:
                    native:
                      search-locations: classpath:/config #各个客户端的配置文件存放路径在 根目录下的config文件夹中
            eureka:
              client:
                serviceUrl:
                  defaultZone: http://192.168.1.200:8070/eureka/
              instance:
                preferIpAddress: true
                instance-id: ${spring.cloud.client.ipAddress}:${server.port}
            注:本地存放配置文件,同样需要以 “服务名-环境.yml” 命名,例如:“girl-dev.yml”

    3、spring boot启动类
        @EnableConfigServer
        @EnableEurekaClient

    4、访问测试
        1.访问地址模型:
            /{application}/{profile}[/{label}]
            /{application}-{profile}.yml 或 /{application}-{profile}.properties
            /{label}/{application}-{profile}.yml 或 /{label}/{application}-{profile}.properties
        2.实例
            http://localhost:8040/girl-dev.properties
        3.说明
            {application} —— 即 spring.application.name(客户端服务名称)
            {profile} —— 即 spring.cloud.config.profile(客户端调用环境)
            {label} —— 即 spring.cloud.config.label(对应git的分支。如果配置中心使用的是本地存储,则该参数无用)


客户端:
    1、pom.xml
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-config
            1.4.0.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

    2、bootstrap.yml(详见最后《application和bootstrap相关备注》)
        eureka:
          client:
            serviceUrl:
              defaultZone: http://192.168.1.200:8070/eureka/
        spring:
          cloud:
            config:
              discovery:
                enabled: true #是从配置中心读取文件
                serviceId: config-server #配置中心的servieId,即服务名
              profile: dev #调用环境
              label: master #仓库分支

    3、application.yml
        server:
          port: 8041
        spring:
          application:
            name: girl

    4、spring boot启动类
        @EnableEurekaClient

    5、测试
        @RestController
        @RefreshScope
        public class ConfigController {

            @Value("${paomo}")
            String temp;

            @RequestMapping(value = "/hello")
            public String hello(){
                return temp; //返回“zcv”,对应访问http://192.168.1.200:8040/master/girl-dev.properties
            }
        }

    6、@RefreshScope
        如果服务启动之后,再更改仓库中的配置,这时客户端获取到的配置信息是不会跟着变化的。
        1、所以我们需要在pom.xml中引入 spring-boot-starter-actuator 监控模块
            
                org.springframework.boot
                spring-boot-starter-actuator
            
        2、并在需要获取配置的controller中添加 @RefreshScope 注解,
        3、但我们更改了仓库中的配置,然后调用 actuator 给我们提供的 /refresh 接口即可,接口是post请求,返回被更新的key值数组





============================= application和bootstrap相关备注 ===============================
与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,
而bootstrap.properties的加载也是先于application.properties。例如defaultZone如果不配置,则找不到serviceId,会导致启动失败。

1、Spring Cloud会创建一个`Bootstrap Context`,作为Spring应用的`Application Context`的父上下文。
2、项目初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。
3、`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。
4、`Bootstrap context`和`Application Context`有着不同的约定,所以新增了一个`bootstrap.yml`文件,而不是使用`application.yml` (或者`application.properties`)。保证`Bootstrap Context`和`Application Context`配置的分离。
   注: 推荐在`bootstrap.yml` or `application.yml`里面配置`spring.application.name`. 你可以通过设置`spring.cloud.bootstrap.enabled=false`来禁用`bootstrap`。
附链接:https://www.cnblogs.com/BlogNetSpace/p/8469033.html


参考链接

https://blog.csdn.net/forezp/article/details/81041028

https://blog.csdn.net/a60782885/article/details/69415527

https://www.jianshu.com/p/757a46dc57e5

https://www.cnblogs.com/BlogNetSpace/p/8469033.html

你可能感兴趣的:(从练习小题切入)