SpringCloud(5) - Config分布式配置

一、概述

1)分布式系统面临的问题 - 配置文件的问题

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

2)什么是 SpringCloud-Config 分布式配置中心

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

SpringCloud Config 分为服务端客户端两部分:

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

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

3)SpringCloud config分布式配置中心能干嘛

● 集中管理配置文件

● 不同环境不同配置,动态化的配置更新,分环境部署,比如 dev/ test/ prod/ beta/ release

● 运行期间动态调整配置,不再需要每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。

● 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置

● 将配置信息以REST接口的形式暴露

扫描二维码关注公众号,回复: 11432538 查看本文章

4)SpirngCloud Config 分布式配置中心与 Github 整合

由于SpringCloud Config 默认使用 Git 来存储配置文件(也支持SVN和本地文件),但最推荐的还是 Git,而且使用 http / https 访问的形式。

Git环境搭建:

① 在码云新建一个仓库 springcloud-config 

创建成功后,选择Http方式克隆项目

② 在电脑中,某一个文件夹下,右键打开Git Bash Here,将我们刚才创建的仓库克隆下来,遇到问题,输入yes

③ 在克隆下来的项目目录下,新建一个文件 application.yaml,在该文件中,写一些简单的配置

spring:
  profiles:
    active: dev

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

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

④ 将我们刚写好的 application.yaml 提交到码云上

git add .
git commit -m "描述"
git push origin master

二、服务端连接Git配置

① 新建一个模块, springcloud-config-server-3344 

② 导入依赖

<!-- config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<!-- web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

③ 新建 application.yaml ,编写配置

server:
  port: 3344

spring:
  application:
    name: spring-cloud-config-server
  # 连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/Lin9207/springcloud-config.git

④ 编写主启动类,并添加注解 @EnableConfigServer 

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

⑤ 启动该模块,访问 http://localhost:3344/config-server-dev.yml 就可以访问到我们上传到码云中的dev配置了

说明:

1. 如果在GitHub上建立的仓库是私有的,那么还要加上spring.cloud.config.server.git.username和spring.cloud.config.server.git.password 这两个配置

2. springcloud config 的URL与配置文件的映射关系如下:

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

3. 如果github上建立的目录下的文件为configtest-dev.properties,那么当启动配置中心服务器端时,可以通过http://localhost:8050/configtest/dev/master访问配置文件,如果访问成功则表示配置中心搭建成功

三、客户端连接服务端远程访问

① 在我们克隆下来的项目目录下,新建一个文件 config-client.yaml ,写一些配置

spring:
  profiles:
    active: dev

---
server:
  port: 8201
# spring配置
spring:
  profiles: dev
  application:
    name: springcloud-provider-dept
# Eureka配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

---
server:
  port: 8202 
# spring配置
spring: 
  profiles: test
  application:
    name: springcloud-provider-dept
# Eureka配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

② 将该配置文件push到远程仓库

git add .
git commit -m "描述"
git push origin master

③ 新建一个模块 springcloud-config-client-3355 ,导入依赖

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

④ 新建配置文件 bootstrap.yaml ,这也是 springboot 的配置文件

bootstrap.yaml 和 application.yaml 的区别:

  • bootstrap.yaml:系统级的配置
  • application.yaml:用户级别的配置

● bootstrap.yaml

spring:
  cloud:
    config:
      name: config-client
      profile: dev               # 环境
      uri: http://localhost:3344 # 地址
      label: master              # 分支

● application.yaml

spring:
  application:
    name: spring-config-client-3355

⑤ 编写一个Controller类

@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 = " + applicationName + "\n,eurekaServer = " + eurekaServer + "\n,port = " + port;
    }
}

⑥ 创建主启动类

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

⑦ 启动测试

bootstrap配置了dev环境访问 http://localhost:8201/config 

配置test环境访问 http://localhost:8202/config 

猜你喜欢

转载自www.cnblogs.com/Dm920/p/13174070.html