SpringCloud學習筆記(7):使用Spring Cloud Config配置中心


簡介

Spring Cloud Config為分布式系統中的外部化配置提供了服務器端和客戶端支持,服務器端統一管理所有配置文件,客戶端在啟動時從服務端獲取配置信息。服務器端有多種配置方式,如將配置文件存儲在本地或者存儲在遠程Git倉庫等等,並且在配置文件被更改時,可以通過多種途徑如actuator的/refresh端點或者Spring Cloud Bus來動態刷新客戶端的配置,而無需重新啟動客戶端。

項目介紹

  1. sc-parent,父模塊(請參照SpringCloud學習筆記(1):Eureka注冊中心)
  2. sc-eureka,注冊中心(請參照SpringCloud學習筆記(1):Eureka注冊中心)
  3. sc-config-client,訪問配置中心的客戶端
  4. sc-config-server,本地配置中心
  5. sc-config-server-git,遠程配置中心

創建訪問配置中心的客戶端

1.在父模塊下創建子模塊項目sc-config-client,pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-config-client</artifactId>
  
  <dependencies>
  	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
  </dependencies>
</project>

2.創建啟動類configclient.ConfigClientApplication:

package configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

3.創建configclient.controller.ConfigClientController:

package configclient.controller;

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

@RestController
@RequestMapping("/client")
public class ConfigClientController {
	@Value("${nickName}")
	private String nickName;
	
	@GetMapping("/hello")
	public String hello(){
		return "hello," + nickName;
	}
}

4.創建bootstrap.yml:

spring:
  application:
    name: sc-config-client
  profiles:
    active: dev
  cloud:
    config:
      uri:  http://localhost:9003
      fail-fast: true

server:
  port: 9002

spring.cloud.config.uri:指定配置中心地址
spring.cloud.config.fail-fase:當連接不上配置中心服務器時,是否使當前客戶端異常停止,而不是以默認配置啟動。

使用本地配置中心

1.在父模塊下創建子模塊項目sc-config-server,pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-config-server</artifactId>
  
  <dependencies>
  	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
  </dependencies>
</project>

2.創建啟動類configserver.ConfigServerApplication:

package configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

3.創建application.yml:

server:
  port: 9003
      
spring:
  application:
    name: sc-config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: classpath:/conf    

spring.profiles.active:配置文件的獲取方式
spring.cloud.config.server.native.search-locations:本地配置文件的存放路徑

4.創建/src/main/resources/conf/sc-config-client-dev.yml文件:

nickName: Luke 

該文件內容為客戶端需要從服務端獲取的配置信息,文件名稱和客戶端配置是相對應的,如sc-config-client-dev.yml=【spring.application.name】-【 spring.profiles.active】.yml

5.測試

啟動本地配置中心sc-config-server成功后再啟動客戶端sc-config-client,訪問http://localhost:9002/client/hello,客戶端成功從配置中心獲取nickName的配置:

使用遠程配置中心

1.在父模塊下創建子模塊項目sc-config-server-git,pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-config-server-git</artifactId>
  
  <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>
  </dependencies>
</project>

2.創建啟動類configserver.GitConfigServerApplication:

package configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

3.創建application.yml:

server:
  port: 9005

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ 

spring:
  application:
    name: sc-config-server-git
  cloud:
    config:
      server:
        git:
          uri: https://github.com/yinchao3577/test777.git
          username: xxxxx
          password: xxxxx
      label: master    
      fail-fast: true       

spring.cloud.config.server.git.uri:git存儲庫地址
spring.cloud.config.server.git.username:用戶名
spring.cloud.config.server.git.password:密碼
spring.cloud.config.server.git.searchPaths:配置文件所在目錄,若在根目錄則無需配置
spring.cloud.config.label:Git Repository的分支,默認為master

4.更改客戶端配置

pom.xml添加Eureka依賴:

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

bootstrap.yml改為:

spring:
  application:
    name: sc-config-client
  cloud:
    config:
      name: myconfig2
      label: master
      discovery:
        enabled: true
        service-id: sc-config-server-git #使用Eureka注冊中心來發現Config配置中心服務

server:
  port: 9002
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

spring.cloud.config.name:遠程倉庫中配置文件的名稱
spring.cloud.config.discovery.enabled:是否開啟配置中心服務發現
spring.cloud.config.discovery.service-id:配置中心服務名稱

5.測試

遠程倉庫myconfig2.yml值為:

nickName: Grace

依次啟動注冊中心sc-eureka、遠程配置中心sc-config-server-git,sc-config-server-git啟動成功后再啟動客戶端sc-config-client,訪問http://localhost:9002/client/hello,客戶端成功從配置中心獲取nickName的配置:

手動刷新配置

actuator中包含一個/refresh的端點,用於配置的刷新。對該端點的調用實質是對RefreshScope類的調用,RefreshScope是上下文中的一個bean,它包含一個公共refreshAll()方法和refresh(String)方法,分別用來刷新范圍內的所有bean或者指定的單個bean。

1.更改客戶端配置

pom.xml添加actuator依賴:

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

bootstrap.yml添加開啟refresh節點的配置:

management:
  endpoints:
    web:
      exposure:
        include: 'refresh'

2.在ConfigClientController類上添加@RefreshScope注解,表示該類可以在運行時刷新配置,當調用完/actuator/refresh端點后,在下一次訪問該Controller時,該Controller會重新初始化以及注入到容器中,初始化時會重新加載配置,所以在訪問時將會訪問到最新配置的值。

3.按之前測試的步驟進行啟動和訪問,修改github中配置的值后,重新訪問客戶端后值並沒有更改,通過Postman工具發送POST請求到http://localhost:9002/actuator/refresh后,再重新訪問客戶端,值已經被更新成最新配置的值。


免責聲明!

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



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