Springcloud-config分佈式配置中心的使用

隨着服務的擴展,配置文件也越來越多,這時候就需要一個集中修改配置文件的地方來管理配置文件
在自己的git上新建個項目用來測試
在這裏插入圖片描述
https://github.com/chill-zx/springcloud-config.git
新建三個yml文件用於測試:
在這裏插入圖片描述
在本地創建一個文件夾用於存放clone下來的yml文件
在這裏插入圖片描述
新建module:
添加pom

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zx.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

編寫yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri:  https://github.com/chill-zx/springcloud-config.git #填寫你自己的github路徑
          search-paths:
            - springcloud-config
      label: master
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

編寫主啓動類
在這裏插入圖片描述
修改hosts文件
在這裏插入圖片描述
在dev的環境添加信息用於測試並上傳git:
在這裏插入圖片描述
啓動服務並訪問:
在這裏插入圖片描述

可以看到是能夠讀取到的在這裏插入圖片描述
config分爲服務端和客服端,測試的3344就是服務端
在這裏插入圖片描述
新建一個客戶端3355:
在這裏插入圖片描述
添加pom依賴

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zx.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

config的客戶端用的是bootstrap.yml
application.yml是用戶級的系統資源
bootstrap.yml是系統級的,優先級更高
配置bootstrap.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

編寫主啓動類:
在這裏插入圖片描述
編寫controller並讀取配置:
在這裏插入圖片描述
啓動訪問:能看到已經讀取到了信息
在這裏插入圖片描述
動態刷新配置:
修改一下版本,把1.0改爲2.0
在這裏插入圖片描述
可以看到已經更改爲了2.0
在這裏插入圖片描述
訪問服務端:版本號改變了
在這裏插入圖片描述

再訪問3355客戶端發現沒有改變,還是1.0在這裏插入圖片描述
1 要實現動態加載首先需要添加監控的依賴
在這裏插入圖片描述
2 修改yml,暴露監控的端口
在這裏插入圖片描述

在controller類添加一個刷新的註解
在這裏插入圖片描述
再從新訪問就是2.0,說明動態配置設置成功了
在這裏插入圖片描述
配置過後動態訪問還是不能夠成功,所以需要”激活一下“
在這裏插入圖片描述
在這裏插入圖片描述
再訪問:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章