美文网首页
springcloud 配置中心 入门

springcloud 配置中心 入门

作者: Martain | 来源:发表于2020-06-22 22:22 被阅读0次

分布式系统面临的问题

微服务意味着要将单体应用中的业务拆分成一个个子服务 ,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,比如每个微服务都需要⼀个配置⽂件,并且,如果有⼏个微服务都需要连接数据库那么就需要配4次数据库相关配置,并且当数据库发⽣改动,那么需要同时修改4个微服务的配置⽂件才可以,所以一套集中式的、动态的配置管理设施是必不可少的。

关于config

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

config流程图

springcloud config可以做到:

  • 1、不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 2、运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 3、当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 4、将配置信息以REST接口的形式暴露

更多介绍查看 官网

使用config

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式),这里我们使用的是git的方式来存放配置文件。

搭建配置服务中心

前提环境

  • git仓库

    ​ 首先我们需要建立一个新的配置文件的存放仓库,github或者gitee均可。

    ​ 我已经准备好了一个仓库,仓库地址是:https://github.com/martaintao/spring-config-center-demo.git,仓库的master分支中有config-dev.ymlconfig-test.ymlconfig-prod.yml三个配置文件。

  • 注册中心

    需要准备好一个注册中心,我这里使用的是Eureka。

依赖

<!--config server-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--eureka client-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

改yml

server:
  port: 7070
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/martaintao/spring-config-center-demo.git # 因为是public的仓库,所以不需要认证。
      label: master # 指定分支

# 注册中心
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8080/eureka/

配置启动类

@SpringBootApplication
@EnableEurekaClient     
@EnableConfigServer  // 启动配置服务中心
public class ConfigCenterApplication7070 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication7070.class,args);
    }
}

读取配置文件

springcloud提供给了我们http的方式去获取远程的配置文件信息。请求的api拼接有如下5种规则:

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

label表示分支,application一般是配置文件中的spring.config.name属性,profile就是环境标识,比如dev/test/prod

如上述项目配置完成启动了之后,我们可以访问http://localhost:7070/master/config-dev.yml(使用的是上面第三种规则的格式)就可以访问到config-dev.yml的内容。当我们改动文件的时候,刷新请求也会实时更新。

config客户端

配置依赖

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

建立yml

注意,这里是创建bootstrap.yml,这个配置⽂件的作⽤是,先到配置中⼼加载配置,然后加载到application.yml中。

application.yml是用户级的资源配置项。

bootstrap.yml是系统级的,优先级比application.yml更高。

server:
  port: 7001

spring:
  application:
    name: cloud-eureka-consumer-useradmin
  cloud:
    config:
      label: master # 分支名称
      name: config  # 配置文件名称 列如config-dev.yml 名称就是config
      profile: dev  # 环境(读取后缀)
      uri: http://127.0.0.1:7070 # 配置中心地址
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册消息,默认为true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8080/eureka/

编写测试接口

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/getConfigInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

启动类

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

服务启动之后,访问http://localhost:7001/getConfigInfo,如果返回了config-dev.yml中config.info的内容的话,便表示配置成功了。

但是发现客户端启动之后,如果配置文件更新了,客户端不重启的话,新的配置是不会生效的,所以我们需要配置能够动态刷新。

动态刷新

要实现动态刷新需要在客户端添加spring-boot-starter-actuator依赖。

客户端添加依赖

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

修改配置文件boostrap.yml

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

修改controller

在controller上添加@RefreshScope注解即可

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/getConfigInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

重启服务

到这里还不能自动动态刷新,当我们的配置文件更新了,我们只需要向客户端发送/actuator/refreshpost请求(比如向上面的客户端发起post请求:https://localhost:7001/actuator/refresh)即可无需重启可获取最新的配置文件。

问题

如果有许多客户端该怎么办?用脚本确实可以实现批量更新,但是这种方式显然不够优雅,可否广播一次,然后所以的客户端都可以自动刷新呢?当然可以,不过这里就需要用到springcloud的bus消息总线的知识了,这里因为我还没有学到bus总线,所以暂时就给出示例了,有兴趣可以自己去实现下

相关文章

网友评论

      本文标题:springcloud 配置中心 入门

      本文链接:https://www.haomeiwen.com/subject/dqqqfktx.html