Spring Cloud Config 基礎示例 原 薦

Spring Cloud Config 簡介

什麼是Srping Cloud Config?

  • Spring Cloud Config 是一種分佈式配置中心框架, 爲分佈式系統中的外部化配置提供服務器和客戶端支持。(同類技術還有vaultzookeeperConsul)
  • 使用Config Server,可以在所有環境中管理應用程序的外部屬性。客戶端和服務器上的概念映射與Spring Environment和PropertySource抽象,因此它們非常適合Spring應用程序,但可以與任何語言運行的任何應用程序一起使用。當應用程序通過部署管道從開發到測試並進入生產時,您可以管理這些環境之間的配置,並確保應用程序具有遷移時需要運行的所有內容。服務器存儲後端的默認實現使用git,因此它可以輕鬆支持配置環境的標記版本,以及可用於管理內容的各種工具。
  • Spring Cloud Config也主要由兩部分組成:
  • Config-Server: 用於配置外部的資源文件,支持對屬性值進行加解密
  • Config-client:綁定到config server使用遠程配置文件初始化spring,支持對屬性值進行加解密

本文示例說明

  • 爲了更直觀的理解spring cloud config,本文通過server獲取整個配置,效果上與通過配置springboot的active 來切換環境一致;通過接口查詢數據庫以查看效果;
  • 採用高可用架構;此處使用eureka去做服務註冊發現(暫時也只會這個。。),eureka配置說明可以查看springCloud之eureka

新建ConfigServer

爲了減少文章長度,儘量乾貨,創建過程就不截說明了,建議使用Intellij idea進行創建;

  • POM文件:
 <?xml version="1.0" encoding="UTF-8"?>
 <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>

	<groupId>com.lc.springcloud</groupId>
	<artifactId>config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config-server</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.M2</spring-cloud.version>
	</properties>

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

		<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>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>
  • application.yml配置如
server:
	port: 8100
spring:
	application:
		name: config-server
	cloud:
    	config:
			server:
        		git:
          		 uri: https://github.com/lvchaogit/SpringCloud
eureka:
   client:
    service-url:
      defaultZone: http://localhost:8081/eureka/ # 服務註冊中心地址

通過配置不難理解,該配置是從git上獲取配置,更多配置後續詳解;

  • application.java
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

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

主要是加上** @EnableConfigServer**註解,開啓configserver功能

搭建 Config Client

  • POM文件
<?xml version="1.0" encoding="UTF-8"?>
<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>

	<groupId>com.lc.springcloud</groupId>
	<artifactId>config-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config-client</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.M2</spring-cloud.version>
	</properties>

	<dependencies>
		<!-- config starter begin-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- config starter end-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.5.0</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<!-- mybatis-plus begin -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>2.1.9</version>
			<exclusions>
				<exclusion>
					<artifactId>tomcat-jdbc</artifactId>
					<groupId>org.apache.tomcat</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<!--config監控模塊  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.13</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>
  • bootstrap.yml
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true
        service-id: config-server
server:
  port: 2001
eureka:
  client:
      register-with-eureka: false     #因此處只是消費,不提供服務,所以不需要向eureka server註冊
      service-url:
        defaultZone: http://localhost:8081/eureka/ # 服務註冊中心地址
  • 首先注意配置文件名稱爲:bootstartp.yml,並不是~~application.yml~~
  • 通過配置discovery,並設置enabled爲true,使client通過服務發現去獲取server,server-id爲註冊中心裏配置的服務名稱
  • label=git的標籤;profile=配置文件版本(類似於spring boot中的active)

常用configserver配置

採用URI佔位符

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lvchaogit/{application}
  • 使用{application} 和 {profile}(如果使用{label},請記住它是使用在git標籤中的)。因此你可以輕鬆的支持“一個應用一個倉庫”的原則

模式匹配和多倉庫

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lvchaogit/SpringCloud
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo
  • 在{application}和{profile}參數中使用模式匹配可以支持更多複雜的需求。模式的格式是一組逗號分隔的{application}/{profile},其中的參數可以使用通配符.
  • 如果{application}/{profile}沒有匹配到任何模式,它將使用默認的倉庫地址:spring.cloud.config.server.git.uri。上面的例子中,"simple"倉庫匹配的是“simple/”(它僅僅匹配一個倉庫simple,在所有的環境下)。"local"倉庫將匹配所有{application}的名字以“local”開頭的,並且也是在所有的環境下。“/”前綴自動添加到所有沒有設置{profile}的模式中。

匹配倉庫子目錄

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*
  • 在foo和以bar開頭的目錄中,搜索配置文件。

克隆遠程的倉庫

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          repos:
            team-a:
                pattern: team-a-*
                cloneOnStart: true
                uri: http://git/team-a/config-repo.git
            team-b:
                pattern: team-b-*
                cloneOnStart: false
                uri: http://git/team-b/config-repo.git
            team-c:
                pattern: team-c-*
                uri: http://git/team-a/config-repo.git
  • 服務器默認在第一次請求配置文件時克隆遠程的倉庫,也可以配置在啓動的時候克隆倉庫
  • team-a的倉庫將在服務端啓動時進行克隆,其他的倉庫將在第一次請求時克隆。

倉庫認證

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword
  • 輸入用戶名密碼

常用配置參考:configServer常用配置

文中示例代碼:SpringCloudConfig 示例

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