1. 程式人生 > >Spring Cloud Config介紹+案例

Spring Cloud Config介紹+案例

測試中使用到的程式碼到在這裡https://download.csdn.net/download/zhou920786312/10853300

介紹

是一個解決分散式系統的配置管理方案

包含兩個部分

  • server提供配置檔案的儲存、以介面的形式將配置檔案的內容提供出去,
  • client通過介面獲取資料、並依據此資料初始化自己的應用。

Spring cloud使用git或svn存放配置檔案,預設情況下使用git

一個配置中心提供的核心功能

  • 提供服務端和客戶端支援
  • 集中管理各環境的配置檔案
  • 配置檔案修改之後,可以快速的生效
  • 可以進行版本管理
  • 支援大的併發查詢
  • 支援各種語言

圖解

案例

步驟

1在github上建立一個倉庫microservicecloud-config,

得到[email protected]:zhou920786312/microservicecloud-config.git

 

2複製倉庫到本地

 git clone [email protected]:zhou920786312/microservicecloud-config.git

 

3將配置檔案application.yml上傳到遠端倉庫

application.yml

spring: 
 profiles:
    active:
      - dev

---
spring: 
  profiles: dev  #開發環境
  application: 
    name: microservicecloud-config-dev
    
---
spring: 
  profiles: test  #測試環境
  application: 
    name: microservicecloud-config-test

 

$ git status
 
$ git add .
 
$ git commit -m "init file"
 
$ git push origin master

 

 

4建立microservicecloud-config-3344(相當於第一幅圖的config-server)

 

<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.atguigu.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservicecloud-config-3344</artifactId>


	<dependencies>
		<!-- springCloud Config -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- 避免Config的Git外掛報錯:org/eclipse/jgit/api/TransportConfigCallback -->
		<dependency>
			<groupId>org.eclipse.jgit</groupId>
			<artifactId>org.eclipse.jgit</artifactId>
			<version>4.10.0.201712302008-r</version>
		</dependency>
		<!-- 圖形化監控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 熔斷 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</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-test</artifactId>
		</dependency>
		<!-- 熱部署外掛 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

</project>

 

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: [email protected]:zhou920786312/microservicecloud-config.git #GitHub上面的git倉庫名字

 

 

package com.atguigu.springcloud;

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

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

host新增

127.0.0.1       config-3344.com

啟動microservicecloud-config-3344測試

http://config-3344.com:3344/application-dev.yml

讀取檔案的方式

 

 

5將配置檔案microservicecloud-config-client.yml上傳到遠端倉庫

spring: 
 profiles:
    active:
      - dev

---
server: 
 port: 8201
spring: 
  profiles: dev   
  application: 
    name: microservicecloud-config-client 
    
eureka:
  client: #客戶端註冊進eureka服務列表內
    service-url: 
      defaultZone: http://eureka-dev.com:7001/eureka/

---
server: 
 port: 8202
spring: 
  profiles: test   
  application: 
    name: microservicecloud-config-client 
    
eureka:
  client: #客戶端註冊進eureka服務列表內
    service-url: 
      defaultZone: http://eureka-test.com:7001/eureka/

 

6建立microservicecloud-config-client-3355(相當於第一幅圖的客戶端)

 

<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.atguigu.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservicecloud-config-client-3355</artifactId>


	<dependencies>
		<!-- SpringCloud Config客戶端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</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-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</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-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>
 

 

 

spring:
  application:
    name: microservicecloud-config-client

 

spring:
  cloud:
    config:
     #name實際內容是github的檔案:microservicecloud-config-client.yml
      name: microservicecloud-config-client #需要從github上讀取的資源名稱,注意沒有yml字尾名
      profile: test   #本次訪問的配置項
      label: master   
      uri: http://config-3344.com:3344  #本微服務啟動後先去找3344號服務,通過SpringCloudConfig獲取GitHub的服務地址
 

#上面的功能是:通過uri獲取配置中心的所有內容,通過name獲取具體的內容,通過profile獲取哪個環境的內容

Host配置

127.0.0.1       client-config.com

 

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

package com.atguigu.springcloud.rest;

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

@RestController
public class ConfigClientRest
{

	@Value("${spring.application.name}")
	private String applicationName;

	@Value("${eureka.client.service-url.defaultZone}")
	private String eurekaServers;

	@Value("${server.port}")
	private String port;

	@RequestMapping("/config")
	public String getConfig()
	{
		return "服務名稱: " + applicationName + "---- eureka服務名稱:" + eurekaServers + "---- 埠: " + port;
	}
}

測試

啟動3344

http://config-3344.com:3344/microservicecloud-config-client-test.yml

啟動3355

http://client-config.com:8202/config

 

注意:如果需要改配置,只要bootstrap.yml改

 

測試