SpringCloud之配置中心Config

Spring Cloud Config可以把一些配置信息配置在遠程服務器上,集中化管理集羣配置。可以使用Spring Cloud Config來獲取遠程服務器上的配置信息。
可以分爲兩個部分:

服務端: 配置服務端,服務管理配置信息
客戶端:客戶端調用server端暴露接口獲取配置信息

先來看看我github的目錄和要讀取的配置:裏面定義四個配置文件讀取。
這裏寫圖片描述

現在讀取第一個配置文件中的值:讀取的方式和配置名稱可以有關
這裏寫圖片描述

讀取項目可以分爲兩個部分:

服務端: 配置服務端,服務管理配置信息
客戶端:客戶端調用server端暴露接口獲取配置信息

先來看服務器:

<dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-config-server</artifactId>  
  </dependency> 
@SpringBootApplication
@EnableConfigServer
public class Applicationn {
    public static void main(String[] args){
        SpringApplication.run(Applicationn.class,args);
    }

}
server.port=8081
#spring.application.name=microservice-foo
spring.application.name=ok

# 遠程倉庫地址,具體到項目
spring.cloud.config.server.git.uri=https://github.com/meihuiziaaaaaa/firstTest
# 項目下的具體路徑可以配置多個  
spring.cloud.config.server.git.searchPaths=config/config

#spring.cloud.config.label=master

# 用戶名
#spring.cloud.config.server.git.username=
# 密碼
#spring.cloud.config.server.git.password=

服務器代碼就此結束:讀取方式如下:
啓動服務端之後,我們可以通過url的方式對配置文件進行訪問,訪問關係如下:

url:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
上面的url會映射{application}-{profile}.properties對應的配置文件,{label}對應git上不同的分支,默認爲master。

例如:訪問microservice-foo-dev.properties文件的方式
這裏寫圖片描述

在例如訪問microservice-foo-dev.properties和microservice-foo.properties
這裏寫圖片描述
這裏寫圖片描述

先開啓服務端,在啓動客戶端
現在客戶端的讀取方式:

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

@SpringBootApplication
public class Application {
       public static void main(String[] args){
           SpringApplication.run(Application.class,args);
       }
}

bootstrap.properties

需要配置在bootstrap中才能生效,而不是在application配置文件中,bootstrap配置文件會比application更加早的加載。

spring.application.name=microservice
spring.cloud.config.profile=foo-dev
spring.cloud.config.uri=http://localhost:8081/
#spring.cloud.config.label=master


@PropertySource(value = "classpath:bootstrap.properties")
@RestController

public class Test1 {
    @Value("${profile}")
    private String profile;

    @RequestMapping("/profile")
    public String profile(){
        System.out.println(this.profile+"....");
        return  this.profile;
    }
}

這裏寫圖片描述

配置刷新

在上面的配置中,客戶端是如何獲得配置中心的配置信息的呢?是在bootstrap配置文件中配置,在程序啓動時加載獲取的。顯然,在加載完成之後,如果配置中心中的信息發生變化的話,客戶端的信息是不會跟着變化的,這是實際開發過程中所不允許的。我們應該怎麼做呢?

我們需要增加一個監控模塊:spring-boot-starter-actuator

在客戶端中增加如下依賴:

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

並且在Controller中增加@RefreshScope註解。

最後,actuator給我們提供了一個/refresh接口,修改完git倉庫信息之後,向這個接口發送一個POST信息,就會更新配置信息了。不需要自己寫/refresh接口
修改github配置文件後 (bootstrap.properties: management.security.enabled=false)訪問http://localhost:8080/refresh,再一次http://localhost:8080/profile取得的是最新的數據

但是服務端和客戶端最終是要成爲微服務的,所以還得向eureka服務註冊

在服務端和客戶端都增加

    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-starter-eureka</artifactId>  
    </dependency>  
啓動類上加
@EnableDiscoveryClient

客戶端


eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.168.15.190:8888/eureka/

服務端


#服務註冊
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=configServier
eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.xxx.15.190:8888/eureka/

注意:

在客戶端上面實現了兩種方式讀取配置一種是url,另一種是通過註冊的服務名稱讀取

   第一種: spring.cloud.config.uri=http://localhost:8081/

   第二種:  spring.cloud.config.discovery.enabled=true
            spring.cloud.config.discovery.service-id=configServier

最終的代碼:

服務端:

pom.xml



-------

 <parent>

    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
  </parent>








  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

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





    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <!--  <scope>test</scope>-->
    </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>-->

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>


    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>


    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!-- Eureka 客戶端依賴-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>



  </dependencies>



  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Dalston.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

啓動類


@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class Applicationn {
    public static void main(String[] args){
        SpringApplication.run(Applicationn.class,args);
    }

}

application.properties

server.port=8081
#spring.application.name=microservice-foo
spring.application.name=configServier

# 遠程倉庫地址,具體到項目
spring.cloud.config.server.git.uri=https://github.com/meihuiziaaaaaa/firstTest
# 項目下的具體路徑可以配置多個  
spring.cloud.config.server.git.searchPaths=config/config

#spring.cloud.config.label=master

# 用戶名
#spring.cloud.config.server.git.username=
# 密碼
#spring.cloud.config.server.git.password=


eureka.client.serviceUrl.defaultZone=http://192.168.15.188:8888/eureka/,http://192.168.15.189:8888/eureka/,http://192.168.15.190:8888/eureka/

客戶端:

pom.xml

<parent>

    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
  </parent>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>


<!--

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
-->


    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <!--  <scope>test</scope>-->
    </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>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>


    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>


    <!-- Eureka 客戶端依賴-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

  </dependencies>

  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>


  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Dalston.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

啓動類:

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
       public static void main(String[] args){
           SpringApplication.run(Application.class,args);
       }
}

bootstrap.properties

server.port=8080
spring.application.name=microservice
spring.cloud.config.profile=foo-dev
#spring.cloud.config.uri=http://localhost:8081/

#spring.cloud.config.label=master
management.security.enabled=false

#服務註冊
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=configServier
eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.xxx.15.190:8888/eureka/


測試


@PropertySource(value = "classpath:bootstrap.properties")
@RestController
@RefreshScope
public class Test1 {
    @Value("${profile}")
    private String profile;

    @RequestMapping("/profile")
    public String profile(){
        System.out.println(this.profile+"....");
        return  this.profile;
    }


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