SpringCloud之自動化配置-config


編程開發的時候有沒有覺得很多配置文件需要維護,比如,修改了數據庫連接,所有用到該數據庫的服務配置都得替換,是不是超級的麻煩呢

下面,給大家介紹一下Spring提供的配置自動化組件-spring cloud config

config也就是配置的意思,springcloud也是想盡辦法幫助大家節約開發和發布成本,這也是為什么大家喜歡用他的原因,並且這個配置中心也是極其的簡單。

 

首先,所有的配置文件有兩部分組成,第一:文件名   第二:所屬的環境(即本地環境,測試環境或者線上環境,通常用dev,sit或者uat表示)

存儲配置文件需要有個存儲的地方,目前用的比較多的是數據庫和遠程倉庫git

 

這次我已git為例,首先看下目錄結構

是不是非常的簡單,一個目錄下,就只有配置文件,文件格式命名: {profile}-{env}.properties

這里我推薦用properties后綴(因為我用yml文件試了很多次,都沒有讀取到數據,有機會再試試吧,大家知道的話也可以下方評論留言哈!)

該配置文件目錄建好后,上傳提交到git

git地址為:

http://chengjq@192.168.129.200:8089/r/java/springConfig.git

新建配置中心服務config-server

還是先來看下目錄結構,仍然是最簡單的ConfigServerApplication.java , application.yml , pom.xml三個文件

1.pom.xml

<dependencies>
         <!--eureka server -->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-eureka</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-eureka-server</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-config-server</artifactId>
         </dependency>
         <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
         <!-- spring boot test-->
        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
 
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-dependencies</artifactId>
                 <version>Dalston.RC1</version>
                 <type>pom</type>
             <scope>import</scope>
             </dependency>
         </dependencies>
     </dependencyManagement>

config-server主要依賴於spring-cloud-starter-eureka-server,spring-cloud-starter-config這兩個包

2.application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: http://chengjq@192.168.129.200:8089/r/java/springConfig.git
          searchPaths: /**
          username: chengjq
          password: 123456
      label: master
       
  application:
    name: config-server

spring.cloud.config.server.git.uri:git的地址,即上面的  http://chengjq@192.168.129.200:8089/r/java/springConfig.git 

searchpath:目錄 /** 搜索所有

username:git的用戶名

password:git的密碼

當然,公開的git可以不填username和password

application.name:該應用的名字

3.ConfigServerApplication.java

@EnableEurekaServer
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
@EnableConfigServer:作為配置中心的標志

啟動該項目,發現在eureka中已經注冊成功,端口為配置文件中定義的8888

 

ok,至此,配置中心已經搭建成功

下面,我們來寫個例子,看下是否可以從git的配置文件中讀取到該屬性

配置客戶端 config-client

首先,還是看下目錄結構

結構還是很簡單,就這三個文件,ConfigClientApplication.java , application.yml , pom.xml

1.pom.xml

 <dependencies>
         <!--eureka server -->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-eureka</artifactId>
         </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.cloud</groupId>
           <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
         <!-- spring boot test-->
        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
 
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-dependencies</artifactId>
                 <version>Dalston.RC1</version>
                 <type>pom</type>
             <scope>import</scope>
             </dependency>
         </dependencies>
     </dependencyManagement>

想要使用配置中心,還是得有依賴:spring-cloud-starter-config

2. application.yml 

eureka:
    client:
         serviceUrl:
             defaultZone: http://localhost:8761/eureka/
             
spring:
  cloud:
    config:
      name: ceshi
      profile: dev
      label: master
      discovery: 
        enabled: true
        service-id: config-server

#      uri: http://localhost:8888/
       
  application:  
    name: config-client
    
server:
  port: 8089
  
management:
  security:
    enabled: false
      

這塊多了些參數

spring.cloud.config.name:文件名(我的文件名叫ceshi)

profile:適用的環境(我的是開發環境 簡稱dev)

label:分支(目前是master,即為主干)

uri:定義的config服務地址(我這里的是http://localhost:8888/,即為配置中心的地址)

discovery.enabled:服務發現(true or false )

discovery.service-id:服務名

正常情況下,uri和discovery.enabled,discovery.service-id任選其一就行,如果git會經常換地址,可以使用服務名

3.ConfigClientApplication.java

@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope
public class ConfigClientApplication {
    
    @Value("${name}")
    String name;
    
    @Value("${age}")
    String age;
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
    
    @RequestMapping(value = "/hi")
    public String hi(){
        return "我是"+name+",今年"+age+"歲";
    }
}

啟動該客戶端,看到應用已被注冊成功

瀏覽器輸入:http://localhost:8089/hi.發現獲取到了數據

至此,已經可以從配置中心獲取到數據了

但是,大家會發現,如果我去修改配置文件信息,並且提交到git后,沒法實施刷新頁面信息,無法做到自動化配置

當然,springcloud還是提供了解決方案

看config-client中的配置文件,發現有個依賴:spring-boot-starter-actuator  該以來就是提供實時刷新,在需要獲取配置數據的類上加上@RefreshScope

然后在postman或者其他api接口調用工具上使用刷新接口即可,注意是post請求,這應該算是半自動化,如果想實現全自動化,可以實施監控git的push操作。

再次去刷新地址,發現數據修改了

至此,半自動化的spring cloud config配置已完成,如有問題,請下方評論,謝謝!


免責聲明!

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



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