Spring Cloud Config支持多种配置方式,包括本地文件系统,git等等。这次用git的方式。
git地址:https://github.com/chechengjiang/configDemo/tree/master/config
这里有三个配置文件,其中config-other-pj代表的是一个工程,config-single-client代表的是一个工程。
两个工程:config-other-pj,config-single-client
-dev表示开发环境,-prod表示正式环境。
这里的文件名是Spring Cloud Config要求的标准文件命名,工程名——环境.yml。如果不是这种命名方式,则找不到文件,稍后解释。
Config中心,其实就是建立一个以Spring Boot为基础的cloud服务端,所有要获取配置的客户端从他这里获取。
下面开始分别建立服务端和客户端:
Config 服务端:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--Spring Boot项目必须 注意版本和Maven的关系,有可能导致不兼容-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--Spring Cloud 配置必须-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--boot项目必须-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web项目必须-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config 服务端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!--Maven项目必须-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
按照这个配置,就生成了一个基本的cloud服务端。也是boot项目的基本配置。这样就可以运行了。
其中服务端专属:
<!-- spring cloud config 服务端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
在resource文件夹下有两个配置文件:
bootstrap.yml
spring:
application:
name:config-single-client,config-other-pj
cloud:
config:
server:
git:
uri: https://github.com/chechengjiang/configDemo
username: [email protected]
password: xxxxxxxxxxx
default-label: master
search-paths: config
功能就是连接git。注意,application.name下有两个工程,分别是config-single-client和config-other-pj,也就是说可以把很多个工程的配置文件放在git,不同的工程通过指定的application.name来获取不同的配置。这里的application.name是为了让client找到用的,对于服务端来说不影响从git上面获取配置信息。也就是说,即使去掉application.name,服务端依然可以从git获取配置信息,但是client就找不到对应的配置了。而在client的bootstrap.yml文件里,application.name必须与服务端的application.name匹配,并且要和git上的yml文件名匹配。
applicaiton.yml
server:
port: 8070
服务端的端口号。
@SpringBootApplication
@EnableConfigServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
启动类的话需要加上@EnableConfigServer的注解,表示这是一个服务端。
测试服务端是否好用,启动之后按照config的约定规则访问:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
直接在浏览器访问:http://localhost:8070/config-single-client/dev/master
上面说到,如果不采用工程名——环境名.yml的方式命名,那么在这里就获取不到信息,只能为{}。
Client客户端:
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 https://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>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud 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>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- @Data 不用写get set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency><!--页面模板依赖-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中客户端专属:
<!-- spring cloud config 客户端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring cloud config 自动刷新配置文件更新 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml:
spring:
profiles:
active: dev
---
spring:
profiles: prod
application:
name: config-single-client
cloud:
config:
uri: http://localhost:8070
label: master
profile: prod
---
spring:
profiles: dev
application:
name: config-other-pj
cloud:
config:
uri: http://localhost:8070
label: master
profile: dev
profiles.active代表当前的环境,spring.application.name表示要从config获取的工程,spring.profiles则为环境,uri为config地址。
application.yml:
server:
port: 8080
management:
endpoint:
shutdown:
enabled: false
endpoints:
web:
exposure:
include: "*"
data:
env: NaN
user:
username: NaN
password: NaN
其中 management 是关于 actuator 相关的,接下来自动刷新配置的时候需要使用。
data 部分是当无法读取配置中心的配置时,使用此配置,以免项目无法启动。
创建Java类接受配置信息:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.Data;
@Data
@Component
public class GitConfig {
@Value("${data.env}")
private String env;
@Value("${data.user.username}")
private String username;
@Value("${data.user.password}")
private String password;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
测试类:
package com.example.demo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class testMain {
@Autowired
private GitConfig gitConfig;
@RequestMapping(value = "show")
@ResponseBody
public Object show(){
return gitConfig;
}
}
如果GitConfig类省略了get,set方法,那么返回到页面的json数据为{}。
配置正确后启动:
相关连接: