分布式配置中心 Spring Cloud Config

   Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端和客户端两部分。服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口。而客户端则是微服务架构中的各个微服务应用或基础设施。

这里讲的是基于Eureka服务治理的配置。

构建配置中心(服务端)

创建一个基础的Spring Boot项目命名为config,

因为即是配置中心也是一个微服务在Eureka上注册,以供其他服务访问获取配置信息。

不会Eureka的可以看我这篇文章 Spring Cloud 分布式入门 使用 Spring Cloud Eureka 服务治理以及高可用

  1、在pom.xml中加入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>

  2、编写配置文件 端口这里设置的8099

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xxx/config-repo  # 远程git仓库地址(码云)
          username: xxx  #码云账户名
          password: xxx  #码云密码
          basedir: E:/work/springCloud/basedir # 本地git仓库
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
server:
  port: 8099

  3、启动类加注解 @EnableConfigServer

@SpringBootApplication
@EnableDiscoveryClient  // Eureka客户端注解
@EnableConfigServer // config server注解
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }

}

  4、这里使用的远程git仓库是码云,登录码云创建一个叫config-repo的仓库,并添加配置文件

  我们使用order-test.yml做测试, 这个文件里面就一行 env: test

服务端入门就写完了, 访问连接http://localhost:8099/order-test.yml  已经返回了数据

 打开配置的本地仓库看一下E:/work/springCloud/basedir

发现自动clone下来了远程仓库。原理是第一次访问连接获取配置文件时,config服务端会从远程git仓库使用git clone命令拉取仓库。后面再访问连接获取时使用git pull拉取最新的数据到本地仓库,然后从本地仓库获取信息返回给客户端。

在码云上修改下order-test.yml 改为 env: test123

然后访问连接发现获取的是新数据。因为config服务端使用git pull拉取的最新数据返回的。

访问路径规则是按这种格式

不带{label}分支信息,默认访问master分支.

    /{application}-{profile}.yml

    /{application}-{profile}.properties

带{label}分支信息,

    /{label}/{application}-{profile}.yml

    /{label}/{application}-{profile}.properties

    /{application}-{profile}[/{label}]

{application} 是项目名称

{profile} 是profile

{label} 是分支名称 默认是master

config客户端

 1、引入依赖

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

  2、删除application.yml  使用bootstrap.yml  原因是spring boot加载配置文件的优先级,bootstrap.yml优先级高,这里不细说了。

spring:
  application:
    name: order # 对应配置文件的{application}
  cloud:
    config:
      discovery:
        service-id: CONFIG  # config服务端在Eureka的名称
        enabled: true
      profile: test # 对应配置文件的{profile}
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

  3、因为使用了Eureka 在启动类上加上Eureka客户端注解 @EnableDiscoveryClient

  4、我们使用远程仓库的order-test.yml做配置文件,那就要在里面加点东西了。

env: test123
server:
  port: 8091
spring:
  application:
    name: order

为了测试是使用的远端仓库的配置, 我们创建一个Controller获取env的属性。

// 测试
@RestController
@RequestMapping("/env")
@RefreshScope   // 刷新env注解
public class EnvController {

    @Value("${env}")
    private String env;

    @RequestMapping(value = "/print")
    public String print() {
        return env;
    }
}

启动项目,发现端口使用的确实是远端配置的8091

访问 http://localhost:8091/env/print  也返回了远程仓库设置的数据

下面试一下更改远程仓库env的值, 然后再重新获取一下发现env的值没有改变,那这是怎么回事呢。

客户端动态刷新配置

   我们要使用actuator的/refresh接口来是用动态刷新配置功能

引入依赖

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

在客户端bootstrap.yml中配置refresh访问权限

// 配置访问
management:
  endpoints:
    web:
      exposure:
        include: refresh,health,info

然后post请求访问  http://localhost:8091/actuator/refresh  刷新,记住一定要使用post请求访问

 可以看出env值有更新, 访问 http://localhost:8091/env/print 可以看到已经返回新的值了。

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值