【springcloud开发教程】spring cloud config——分布式配置

什么是SpringCloud config分布式配置中心?

在这里插入图片描述

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

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

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

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

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

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

一、Git环境搭建

1.在gitee注册账号

2.新建一个仓库springcloud-config

3.安装git到本地 Git - Downloading Package

4.这里我们需要将刚刚新建的项目clone到本地,复制SSH那个路径,这里可能clone失败,我们需要配置一个SSH的公钥。

步骤:

01、$ git config --global user.name "diviuer"

02、$ git config --global user.email "1591382889@qq.com"

03、$ git config --list

04、$ ssh-keygen -t ed25519 -C "1591382889@qq.com"

05、$ cat ~/.ssh/id_ed25519.pub

06、复制得到的公钥,在gitee中,设置->安全设置->SSH公钥->添加公钥

07、$ git clone git@gitee.com:diviuer/springcloud-config.git

08、编写一个配置文件application.yml

08、$ cd springcloud-config  //进入到当前目录

09、$ git add .   //将整个项目添加

10、$ git status //查看状态

11、$ git commit -m "first commit"  //进行提交(后面部分表示提交一个消息)

12、$ git push origin master // 提交到远程gitee

13、查看gitee仓库中项目是否提交成功

 二、Git服务端配置

1.新建springcloud-config-server-3344模块导入pom.xml依赖

<dependencies>
        <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>
            <version>2.1.1.RELEASE</version>
        </dependency>

    </dependencies>

2.resource下创建application.yml配置文件,Spring Cloud Config服务器从git存储库(必须提供)为远程客户端提供配置:

server:
  port: 3344

spring:
  application:
    name: springcloud-config-server

  # 连接码云远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/diviuer/springcloud-config.git  # 注意是https的而不是ssh

# 通过 config-server可以连接到git,访问其中的资源以及配置~

3.编写主启动类开启spring cloud config server服务

@EnableConfigServer // 开启spring cloud config server服务
@SpringBootApplication
public class Config_server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_server_3344.class,args);
    }
}

 4.启动该启动类进行测试

测试访问http://localhost:3344/application-dev.yml

测试访问 http://localhost:3344/application/test/master

测试访问 http://localhost:3344/master/application-dev.yml

如果测试访问不存在的配置则不显示 如:http://localhost:3344/master/application-aaa.yml

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是饿梦啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值