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 入门

填写好Group、Artifact。选择依赖的包有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

 

注意:ConfigServerApplication,Spring 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工程创建一样,依赖需要web,Config 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 入门


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...