Spring Cloud Config配置中心的使用

Spring Boot项目代码开发过程中有这样一个原则:“约定大于配置”,SpringBoot为我们提供了properties和yml类型的文件供我们编写配置文件,而这些配置文件的编写是要遵循约定,这样一来,就有了一个统一的规范,使得我们在使用任何第三方组件时,都能按照规则配置文件,减少耦合。

因此,我们在微服务开发时,会接触到大量的配置,其中有的配置是基础配置,也就是项目要跑起来所需的配置,或者第三方组件要用到的默认配置;同时也有些配置是我们业务需要的,是我们程序员自己定义的。基础配置一般在项目运行之后不会轻易修改,但是自定义配置可能会因为业务的需要而进行修改,例如:一个打折促销业务,在配置文件中配置了一个【折扣】属性,需要在某个节日提高折扣力度,更改折扣数值。基于项目在运行过程中不能轻易重启这个前提下,想要修改配置则是一个难题。然而Spring Cloud Config帮我们解决了这个问题,它可以实现配置文件的热更新。

Spring Cloud Config原理

各个微服务不用在自己的项目代码中维护配置文件,而是将配置文件统一存储到第三方,一般是用git存储,但也可以是svn、本地文件等。配置中心服务(服务端,Spring Cloud Config所定义的一个拉取配置文件的服务)会从这些地方拉取最新的配置,而其他微服务(客户端)只需从配置中心服务拉取最新配置即可

Spring Cloud Config入门

最简单的配置中心,就是仅仅启动一个服务作为配置中心服务端。这里我选择git来存储配置文件

首先需要在git上创建一个仓库,保存各种配置文件,如图:
在这里插入图片描述

其中config-client-dev.yml的内容如下:

user:
  username: config-dev
  password: 0

这里的配置文件名称是有规律的,去掉后缀"-dev"前面的内容是配置中心客户端的服务名

创建配置中心服务端

1.新建项目,引入依赖

新建一个Spring Boot微服务config-center,这个微服务需要被Spring cloud管理,需根据需要设定版本,希望读者对Spring Cloud的使用已经有了基本的了解。引入依赖:

<!-- spring cloud config 服务端包 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2.编写配置文件

这里与之前创建微服务不同,我们需要创建一个bootstrap.yml文件,bootstrap.yml在Spring Boot 启动时会优先于application.yml被执行,它在程序引导时执行,应用于更加早期配置信息读取。内容如下:

spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ithushuai/config-center-demo #配置文件所在仓库
          username: git用户名
          password: git密码
          default-label: master #配置文件分支
          search-paths: config  #配置文件所在根目录
          timeout: 10 # 超时时间,单位s,可以设置长一点,国内访问github比较慢,极容易出现访问超时

再创建application.yml文件,内容如下:

server:
  port: 8084

3.编写启动类,加上@EnableConfigServer注解

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

4.启动服务,访问git仓库中的配置文件。有如下一些访问规则:

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

application:服务名

profile:配置环境(dev、prod、test…)

label:git分支,默认master

例如想要访问config-single-client-dev.yml,就可以按规则访问如下接口:

http://localhost:8084/config-client-dev.yml

在浏览器中访问该接口,能够获取配置文件内容,就说明配置中心服务端已经搭建好了
在这里插入图片描述

创建配置中心客户端

上述步骤走完,说明配置中心服务端已经搭建好,并且可用,现在需要搭建一个客户端服务来测试能否从服务端拉取配置

1.创建项目,引入依赖

<!-- spring cloud config 客户端包 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.编写配置文件

bootstrap.yml:

# 必须使用bootstrap.yml定义这些配置,cloud config会默认加载这个配置文件中的配置
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8084
      label: master
      profile: dev
---
spring:
  profiles: prod
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8084
      label: master
      profile: prod

application.yml:

server:
  port: 8085

3.编写启动类,常规Spring Boot项目启动类

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

4.编写一个Controller类,并注入自定义配置,来测试能否获取配置文件属性

@RestController
public class GitController {
    @Value("${user.password}")
    private String password;

    @RequestMapping("/show")
    public String getGitConfig(){
        return password;
    }
}

5.启动项目,在浏览器中访问http://localhost:8085/show,页面显示“0”,则表明可以正常获取配置属性
在这里插入图片描述

自动刷新配置

如果我们修改git仓库中配置文件的内容,比如将password改为1,此时浏览器访问:http://localhost:8084/config-client-dev.yml

会发现结果更新为“1”,然而我们再访问:http://localhost:8086/show,结果仍然为“0”。也就是说当git仓库中的配置文件被修改后,配置中心客户端并不能察觉出变化,这并没有达到我们的初衷。要实现客户端刷新配置文件,则需要actuator的支持,前面客户端引入的spring-boot-starter-actuator就是为了使用actuator。做法如下:

1.在注入配置文件属性的类上加上@RefreshScope注解

Controller类中注入了配置文件,因此在GitController类上加上@RefreshScope注解

@RestController
@RefreshScope //开启刷新功能,在使用到配置的类上加。不使用bus时,使用当前服务地址,单独访问http://localhost:8085/actuator/refresh,即可实现本服务配置刷新
public class GitController {
    @Value("${user.password}")
    private String password;

    @RequestMapping("/show")
    public String getGitConfig(){
        return password;
    }
}

2.在application.yml中引入management配置文件,暴露actuator的接口

server:
  port: 8085
management:
  endpoints:
    web:
      exposure:
        include: "*"

加了以上配置,我们就可以使用actuator给我们提供的一些接口,例如/actuator/refresh

我们用postman测试http://localhost:8085/actuator/refresh接口,使用POST请求:
在这里插入图片描述

如图,一旦配置文件发生变化,接口返回值中就会有相应的提示,上述内容表明config-client的版本有更新,发生变化的属性是user.password

此时浏览器再访问http://localhost:8086/show,结果已经更新为1
在这里插入图片描述

上述自动刷新的弊端

实际生产环境中,会有很多个微服务,假如涉及到多个服务的配置修改,又或者某个服务部署了在了多个服务器上进行负载均衡,这时就需要手动为刷新每个服务,因为只刷新某一个服务,其他服务是不会实现自动刷新的。

配合Spring Cloud Bus实现自动刷新所有服务

Spring Cloud Bus的核心原理就是利用消息队列做广播,可以支持RabbitMQ以及Kafka。这里使用RabbitMQ做消息中间件,前提需要安装好RabbitMQ。

1.在配置中心服务端增加bus依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.在application.yml中加入rabbitmq的配置

spring:
  rabbitmq:
    host: 192.168.18.130
    username: admin
    password: admin

3.在客户端服务中同样增加bus依赖和rabbitmq的配置

4.启动两个客户端服务,分别设置启动端口为8085,8086

可以在IDEA工具中设置启动参数,步骤如下:

1)在右上角点击Edit Configurations
在这里插入图片描述

2)设置VM options:
在这里插入图片描述

3)当前页面左上角点击复制按钮,复制一个启动参数,并设置端口为8086
在这里插入图片描述
5.启动两个客户端服务,在浏览器中分别访问 http://localhost:8085/show,http://localhost:8086/show,显示的结果与github上一致。此时,修改config-client-dev.yml中password的值为2,postman访问配置中心服务的bus-refresh接口,注意这里是bus-refresh:http://localhost:8084/actuator/bus-refresh,请求方式为POST,请求成功postman不显示任何内容

6.再次访问两个客户端的接口,发现数据都做了刷新

在Eureka的管理下使用Spring Cloud Config

Spring cloud Config + Spring Cloud Bus已经可以实现配置文件的集中化管理,并且能够实现配置文件自动刷新。但是基于Spring Cloud的分布式服务往往是由Eureka来对各个微服务进行管理,那么如何将Spring Cloud Config集成到Eureka注册中心中去呢?以下步骤建立在Eureka注册中心已经搭建好的基础上

1.在配置中心服务中增加Euraka客户端的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.在启动类上加上@EnableDiscoveryClient注解

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

3.配置文件中添加Eureka的配置

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

4.客户端服务中增加Eureka客户端依赖,启动类上加上@EnableDiscoveryClient注解,在bootstrap.yml中加上Eureka配置,并修改config的配置,如下

eureka: # 这里eureka配置必须放在bootstrap.yml中,因为在项目启动时,就需要注册到注册中心,然后去配置中心拉取配置,有先后顺序
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      discovery: # 当config server注册到eureka时配置
        enabled: true
        service-id: config-center # config server 在注册中心的注册名
---
spring:
  profiles: prod
  application:
    name: config-client
  cloud:
    config:
#      uri: http://localhost:8084 # 当不使用eureka注册中心时,用uri配置config server的地址,当config server注册到eureka时,无需该配置
      label: master
      profile: prod
      discovery: # 当config server注册到eureka时配置
        enabled: true # 开启从注册中心查找服务功能
        service-id: config-center # config server 在注册中心的注册名

至此,在Spring Cloud中使用Config配置中心,以及配合bus实现自动刷新已经全部完成。如需参考完整代码,可以访问https://github.com/ithushuai/cloud

1.git仓库中的配置文件的名称要与客户端服务的application name对应

2.bootstrap.yml先于application.yml加载,因此拉取配置文件功能相关的配置要在bootstrap.yml中配置,例如config的设置,Eureka的相关配置,application name的配置等等

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值