Spring Cloud Config 快速入门

一、Spring Cloud Config概述

Spring Cloud Config作用是提供一个统一的配置中心,应用的配置从配置中心获取。

没有使用Spring Cloud Config前,配置信息存储在application.properties或application.yaml中,这些配置的属性,在应用中,可以通过Spring的Enviroment接口获取。

使用Spring Cloud Config后,应用配置从配置服务器获取,程序中同样通过Enviroment获取具体的属性值。
在这里插入图片描述

二、Spring Cloud Config Server

现在,搭建Spring Cloud Config服务端,客户端通过服务端获取配置信息。

2.1 创建git仓库

Spring Cloud Config 可从各种数据源获取配置,最常用的是git。
在这里插入图片描述
application.yaml:

pet:
 cat: Jucy
 dog: Tom

cfgClient.yaml:

otaku:
 name: ljf
 height: 178cm

具体两个文件代表什么配置后面再解释。

2.2 创建maven项目

maven依赖:
通过 https://start.spring.io/ 选择 Config Server,创建项目。pom.xml如下:

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>org.otaku</groupId>
	<artifactId>cfgServer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>cfgServer</name>
	<description>Spring Config Server,远程配置中心</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</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>

</project>

配置application.yaml,主要配置git仓库信息:

server:
 port: 8888
spring:
 cloud:
  config:
   server:
    git:
     #git信息
     uri: https://github.com/superOTAKU/config-repo.git
     username: <github account>
     password: <github password>

如果仓库公开,则username和password可不设置。

程序代码:
注意加上@EnableConfigServer注解。

package org.otaku.cfgServer;

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

@EnableConfigServer
@SpringBootApplication
public class CfgServerApplication {

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

}

启动程序,浏览器输入如下url:
http://localhost:8888/application-default.yaml
在这里插入图片描述
http://localhost:8888/cfgClient-dev.yaml
在这里插入图片描述
观察可知,application.yaml的属性匹配所有的应用,而cfgClient-dev.yaml则代表cfgClient应用的profile为dev的配置。
获取配置的url模式如下所示:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

三、Spring Cloud Config Client

maven依赖:

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>org.otaku</groupId>
	<artifactId>cfgClient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>cfgClient</name>
	<description>Spring Cloud App,从configServer获取配置</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<!-- 用于获取远程配置 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		
		<!-- web服务 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- 获取统计信息,例如列出所有的properties -->
		<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>
	</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>

</project>

Spring Cloud的应用,拥有两个ApplicationContext,分别是Bootstrap Context和Main Context。

Bootstrap Context会加载远程配置,并且优先级高于本地配置,使用bootstrap.yaml配置Bootstrap信息。

Main Context则使用application.yaml配置。

bootstrap.yaml:

spring:
 cloud:
  config:
   uri: http://localhost:8888
 application:
  #匹配名为cfgClient的配置
  name: cfgClient
 profiles:
  #结合name,匹配cfgClient-dev.yaml配置文件
  active: dev

application.yaml:

server:
 port: 8080

程序代码:

package org.otaku.cfgClient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class CfgClientApplication {
	
	@Value("${pet.cat}")
	private String cat;
	
	@Value("${pet.dog}")
	private String dog;
	
	@Value("${otaku.name}")
	private String name;
	
	@Value("${otaku.height}")
	private String height;
	
	@RequestMapping("/")
	public String home() {
		return String.format("pet.cat : %s, pet.dog : %s, otaku.name : %s, otaku.height : %s", cat, dog, name, height);
	}

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

}

访问 http://localhost:8080/ ,输出配置信息:
在这里插入图片描述
更详细的用法,参考Spring Cloud官方文档 https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html 。(但文档对于pom部分有误,而且localhost:8080/env并无法打印配置信息)。

猜你喜欢

转载自blog.csdn.net/qq_21508059/article/details/88639720