SpringCloud之配置中心(config)的使用


配置中心的作用就在於可以在項目啟動時加載遠程或本地的配置文件,將配置文件集中管理

springboot版本:

2.1.6.RELEASE

springcloud版本:

Finchley.RELEASE

一、注冊中心

 1、依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

 2、配置文件

server.port=8090
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/

 配置說明

  • eureka.client.register-with-eureka:表示是否將自己注冊到Eureka Server,默認是true。
  • eureka.client.fetch-registry:表示是否從Eureka Server獲取注冊信息,默認為true。

  接下來只需要在主啟動類上加上@EnableEurekaServer注解就萬事大吉了

二、配置中心服務端:

 1、依賴:

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

 2、配置文件

server.port=8050
spring.application.name=config-server
# 注冊到服務注冊中心
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/
# github的倉庫地址
spring.cloud.config.server.git.uri=https://github.com/kurean/springconfig.git
# github的文件路徑
spring.cloud.config.server.git.searchPaths=repo
# github的分支,默認是master
spring.cloud.config.label=master

 最后只要在主啟動類上添加@EnableConfigServer@EnableEurekaClient后就可以了

說明:

  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訪問配置文件,如果訪問成功則表示配置中心搭建成功

三、配置中心客戶端

 1、依賴

<dependency>
  <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

 2、配置文件

  創建bootstrap.properties文件,這時項目啟動時會通過這個文件從配置中心服務端中設置的配置文件存放地址加載指定的配置文件,因為這個文件是優先於application.properties文件加載的,因此指定注冊中心的配置需要放在前者中,否則會造成啟動失敗

spring.cloud.config.name=configtest  #配置文件名稱
spring.cloud.config.profile=dev    #讀取后綴名稱
spring.cloud.config.label=master      #分支名稱
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/
spring.application.name=config-client
server.port=9006

當然,端口號和服務名字可以放在application.properties中

配置說明:

  • spring.cloud.config.name和spring.cloud.config.profile的值結合就是GitHub上上傳的配置文件名,中間用"-"隔開,至於是.properties還是.yml取決於你在本地項目中使用的配置文件是前者還是后者
  • 如果項目中沒有使用注冊中心,那么spring.cloud.config.discovery.serviceId和spring.cloud.config.discovery.enabled就要換成spring.cloud.config.uri來指定配置中心服務端的地址
  • 如果項目中使用了注冊中心,但是通過uri的方式訪問配置中心,那么為了防止本身服務不注冊進注冊中心,需要在主啟動類上加上@EnableDiscoveryClient或@EnableEurekaClient這一服務發現和注冊注解

這時就可以創建一個controller來試試效果了

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {

    @Value("${cs.name}")
    String name;
    @Value("${cs.age}")
    String age;

    @GetMapping(value = "/hi")
    public String hi(){
        return "我的名字是:"+name+",年齡是:"+age;
    }
}

github上的配置文件

 configtest-dev.properties

cs.name=kevin
cs.age=18

測試效果

配置動態刷新之手動版

修改配置中心客戶端

添加依賴

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

修改bootstrap.properties

spring.cloud.config.name=configtest
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
eureka.client.service-url.defaultZone=http://localhost:8090/eureka/
management.endpoints.web.exposure.include=refresh

在controller上添加@RefreshScope注解

這時當修改了GitHub上文件后,需要手動刷新客戶端配置,發送post請求“http://localhost:9006/actuator/refresh”,使用postman或curl命令(curl -X POST "http://localhost:9006/actuator/refresh")

此時客戶端配置文件已經同步修改,重新測試效果

最后,如果遇見Eureka Client啟動后就關閉日志打印“ Unregistering application xxx with eureka with status DOWN”,多半是因為本身項目作為web項目啟動時沒有導入web依賴所致,只要加入就好了

參考文章:https://blog.csdn.net/qazwsxpcm/article/details/88578076


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM