Spring Cloud Config 入门
admin
2023-07-29 17:00:11
0

1.    简介

Spring Cloud Config 是用来为分布式系统中为微服务应用提供集中化的外部配置支持,主要分为Spring Cloud Config Server(服务器端)和Spring Cloud Config Client(客户端)。

2.    Spring Cloud Config Server

Spring Cloud Config Server为服务器端,它是一个单独的微服务应用,用来连接配置仓库(本文使用的是git仓库)并为客户端获取配置信息。

1.     首先,创建config server工程

打开http://start.spring.io/

Spring Cloud Config 入门

填写好GroupArtifact。选择依赖的包有Config Server

对应的pom.xml

<dependencies>

      <dependency>

          <groupId>org.springframework.cloudgroupId>

          <artifactId>spring-cloud-config-serverartifactId>

      dependency>

      <dependency>

          <groupId>org.springframework.bootgroupId>

          <artifactId>spring-boot-starter-webartifactId>

      dependency>

 

      <dependency>

          <groupId>org.springframework.bootgroupId>

          <artifactId>spring-boot-starter-testartifactId>

          <scope>testscope>

      dependency>

dependencies>

2.     将下载下来的项目导入Eclipse

目录结构如下,我这里面新增了bootstrap.yml

Spring Cloud Config 入门

3.     修改配置

application.yml中添加

       server:

      port: 8080

bootstrap.yml中添加

spring:

  cloud:

    config:

      server:

        git:

         uri: https://github.com/DevinXin/config-repo

 

注意:ConfigServerApplicationSpring Boot 启动类上需要添加@EnableConfigServer注解

Spring Cloud Config 入门

4.     启动configServer

通过访问http://localhost:8080/master/foobar-dev.properties可以读到git上的配置文件。

Spring Cloud Config 入门

3.    Spring Cloud Config Client

Spring Cloud Config Client为客户端,客户端通过配置连接服务器端,从服务器端加载配置信息。

1.     创建config client工程

config server工程创建一样,依赖需要webConfig Client

对应的pom.xml为:

<dependencies>

      <dependency>

          <groupId>org.springframework.cloudgroupId>

          <artifactId>spring-cloud-starter-configartifactId>

      dependency>

      <dependency>

          <groupId>org.springframework.bootgroupId>

          <artifactId>spring-boot-starter-webartifactId>

      dependency>

      <dependency>

          <groupId>org.springframework.bootgroupId>

          <artifactId>spring-boot-starter-testartifactId>

          <scope>testscope>

      dependency>

   dependencies>

2.     修改配置

application.yml配置为

     server:

    port: 8081

bootstrap.yml配置为

    spring:

      cloud:

        config:

          uri: http://localhost:8080/

          profile: dev

          label: master

      application:

       name: foobar

3.     写一个Controller

Spring Cloud Config 入门

4.     启动config Client

访问http://localhost:8081/configServer

可以从config Server中获取到配置文件中的值。

Spring Cloud Config 入门


相关内容

热门资讯

马未都回应“被盗佛像疑似现身观... 2000年,海口五公祠佛祖庙一尊宋末元初铸铜普庵祖师坐像失窃,下落不明。2026年7月1日,有网民发...
围山多年的泰山刀片刺网,是怎么... 铺设在山林中的刀片刺网。卷成滚筒状的带刀片铁丝网穿过泰山的树林,也扎痛了人心。它同样扎进了泰山周边村...
数十万伊朗民众涌入大清真寺,现... 为了应对葬礼期间的高温天气,伊朗当局在哈梅内伊的葬礼现场提前安装了喷淋装置,为前来悼念的民众消暑降温...
美前议员:以军暴行堪比纳粹德国 在今日俄罗斯电视台7月4日播出的采访中,美国前民主党籍众议员丹尼斯·库西尼奇(Dennis John...
伊朗称捣毁多个与美以有关的“恐... △伊朗首都德黑兰(资料图)总台记者当地时间4日获悉,伊朗情报部发表声明称,伊朗安全部门近日捣毁4个与...
按实际收入核定社保基数?多地律... 作者 | 第一财经 安然然今年以来,浙江、陕西、江苏、山东、天津等地陆续有律师反映,自己所在律所接到...
云南宾川1小时发生3次4级以上... 澎湃新闻记者 熊强据中国地震台网测定,7月4日17时许,云南省大理州宾川县在一个小时内接连发生3次四...
凤凰晚报丨河南女孩高考699分... 今日人物【浙江阿姨资助12年的河南女孩,高考699分!】近日,河南高考成绩公布,平顶山郏县实验高中的...
视频丨这轮高温还要持续多久?气... 在刚刚过去的6月,我国多地高温天数打破了当地的纪录,而进入7月的这三天,大范围的高温天气不仅无缝衔接...
开空调时屋里放盆水有用吗 这个答案是肯定的!在空调房间内放1到2盆水的话对整个房间的空气舒适度是会有到一定帮助的!因为空调在制...