Spring Cloud Config 实战练习
整体结构:
1.Spring Config Server端
1.创建Maven项目 --- CloudConfigServer
2.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>
<groupId>com.jay</groupId>
<artifactId>CloudConfigServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CloudConfigServer</name>
<url>http://maven.apache.org</url>
<!-- 继承自 Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.jay.CloudServerDemo</start-class>
<java.version>1.7</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>1.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency> -->
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring cloud config server -->
<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>
<build>
<plugins>
<!--mvn spring-boot:run 命令 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. resources下添加配置文件
主配置文件 application.yml
server:
port: 8888
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
git:
uri: https://github.com/jayfeihe/gittest1.git
searchPaths: jay
# username: username
# password: password
几个供客户端不同环境下使用的配置文件
config1.properties
from=local from config1
jay.properties
name=local name from jay
4.主启动类 --- CloudServerDemo1
package com.jay.demo1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class CloudServerDemo1 {
public static void main(String[] args) {
SpringApplication.run(CloudServerDemo1.class, args);
// new SpringApplicationBuilder(CloudServerDemo1.class).web(true).run(args);
}
}
2.Spring Config Client端
1.创建Maven项目 --- CloudConfigClient
2.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>
<groupId>com.jay</groupId>
<artifactId>CloudConfigClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CloudConfigClient</name>
<url>http://maven.apache.org</url>
<!-- 继承自 Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.jay.CloudServerDemo</start-class>
<java.version>1.7</java.version>
</properties>
<!-- Spring Cloud 依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring cloud config starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>CloudConfigClient</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.主配置文件信息
application.yml
server:
port: 8080
bootstrap.properties
spring.application.name=jay,config1
spring.cloud.config.uri=http://localhost:8888/
spring.cloud.config.profile=dev
4.启动类 --- CloudClientDemo1
package com.jay.demo1;
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;
@SpringBootApplication
public class CloudClientDemo1 {
public static void main(String[] args) {
SpringApplication.run(CloudClientDemo1.class, args);
}
}
5.属性访问类 --- ControllerDemo1
package com.jay.demo1.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
@RequestMapping("/demo1")
public class ControllerDemo1 {
@Value("${from}")
private String from = "World";
@Value("${name}")
private String name;
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String home() {
return "Hello World! "+ from +"\t"+name;
}
}
3.效果展示
先启动CloudConfigServer后启动CloudConfigClient,然后通过 http://localhost:8080/demo1/index 访问即可,效果如下:
特别注意:
1.
建议采用bootstrap+application组合形式,一般bootstrap配置工程不可变参数,且在启动时需要设定的内容,application配置可变参数!!!
2.CloudConfigServer中,相同配置文件的不同环境,通过profile区分,即:xx-{profile}.properties方式区分环境
3.CloudConfigClient中,spring.application.name指定需要的配置文件名称(与Server中的配置名称xx相同),spring.cloud.config.profile指定环境
eg:获取ConfigServer中jay和config1的配置,并使用其dev环境
spring.application.name=jay,config1
spring.cloud.config.profile=dev
源码下载:Spring Cloud Config 服务端和客户端项目搭建