SpringCloud-Config分布式配置

Spring Cloud Config 中文文档 参考手册 中文版

Config简介

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。spring cloud提供了configServer来解决这个问题,我们每一个微服务自己带着一个application.yml,那上百个的配置文件修改起来,令人头疼!

spring cloud config 为微服务架构中的微服务提供集中化的外部支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置

spring cloud config 分为服务端客户端两部分

服务端也称为 分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密,解密信息等访问接口。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理。并且可用通过git客户端工具来方便的管理和访问配置内容。

spring cloud config 分布式配置中心能干嘛?

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

spring cloud config 分布式配置中心与Gitee(GitHub)整合

由于spring cloud config 默认使用git来存储配置文件 (也有其他方式,比如自持SVN 和本地文件),但是最推荐的还是git ,而且使用的是 http / https 访问的形式。

所以首先,我们需要先上gitee上进行配置,新建一个仓库

gitee地址:https://gitee.com/

这里如果使用SSH的话后面操作需要配置密钥,HTTPS则可以不需要

 

然后在电脑上新建一个文件来保存我们的gitee仓库

右键选择Git Bash Here

配置名称和email

git clone + 刚才复制下来的地址(HTTPS)即可将仓库拉取下来

输入gitee的用户密码即可

此时就将我们的gitee的仓库就拉取下来了

 

上传文件至gitee

创建一个application.yml文件

文件内容

spring:
  profiles:
    active: test

---
spring:
  profiles: dev
  application:
    name: spring-config-dev

---
spring:
  profiles: test
  application:
    name: spring-config-test

将该文件push到我们gitee上

其实用Idea连接gitee可以更方便的实现这些操作,可以自己上网百度一下如何操作

构建SpringCloud config服务端

新建springcloud-config-3344模块

Maven依赖

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

application.yml

server:
  port: 3344

spring:
  application:
    name: springcloud-config-server
  #连接远程git仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/chenjz1/springcloud-config.git  #该地址就是gitee上HTTPS所复制的地址
          #仓库如果是私有属性则需要设置账户/密码
          # username: 
          # password:  

启动类

@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfig3344Application {

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

}

启动服务后即可通过下列方式来访问到我们gitee仓库上的配置文件内容

此时我们可以在创建一个文件上传至gitee上给我们的config客户端使用 

文件内容

spring:
  profiles:
    active: dev

---
server:
  port: 8201

spring:
  profiles: dev
  application:
    name: springcloud-provider-user

#Eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://Eureka-Server1:7001/eureka/,http://Eureka-Server2:7002/eureka/     #Eureka集群只需要用逗号分隔即可

---
server:
  port: 8202

spring:
  profiles: test
  application:
    name: springcloud-provider-user

#Eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://Eureka-Server1:7001/eureka/,http://Eureka-Server2:7002/eureka/     #Eureka集群只需要用逗号分隔即可

上传至gitee

上传了新文件的话,最好先使用config服务端测试下是否能够成功访问,因为我们config客户端是通过服务端来获取文件内容的,在文章最顶部的简介中也说明到了

构建SpringCloud config客户端

新建springcloud-config-client-3355模块

Maven依赖

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

bootstrap.yml

#系统级别的设置
spring:
  cloud:
    config:
      name: config-client  #需要从git上读取的资源名称
      profile: dev       
      label: master        #分支
      uri: http://localhost:3344   #config服务端地址

application.yml

#用户级别的设置
spring:
  application:
    name: springcloud-config-client-3355

ConfigClientController

新建一个controller来尝试远程获取到我们的配置文件内容 

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {
    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;
    @Value("${server.port}")
    private String port;

    @RequestMapping("/config")
    public String getConfig(){
        return applicationName + " " +
                eurekaServer + " " +
                port;
    }
}

启动类

@SpringBootApplication
public class SpringcloudConfigClient3355Application {

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

}

启动后访问以下地址,可以看到,端口是我们远程配置的端口,并且可以获取到我们远程的配置文件信息

其实我们的其他配置文件都可以参照config服务端进行配置,将配置文件全部统一放置gitee上进行管理

 

posted @ 2022-05-31 15:04  RFAA  阅读(51)  评论(0编辑  收藏  举报