本文基于:spring-boot-2.3.12-RELEASE、spring-cloud-Hoxton.SR12(即2.2.9-RELEASE)
逻辑架构图
- Spring Cloud Config的逻辑图如下:
Config Server搭建(以git仓库为例子)
- pom文件,引入关键的模块 spring-cloud-config-server
<?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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.uu</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>configserver-8009</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
- 启动类
package com.xx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
}
- 服务端配置文件
server:
port: 8009
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/aqu415/twboot.git # 配置文件所在的仓库,这里以码云为例子
search-paths: readme # 如果配置文件在子目录里,可以在这里指定目录名称
我在仓库里readme目录下放了一个config.yaml配置文件,内容如下
spring:
profiles:
active: dev
---
spring:
profiles: dev
info:
sub: g
---
spring:
profiles: test
info:
sub: g2
说明下:yaml文件里的 — 间的内容可以理解为一个新的yaml文件,— 间的内容不互相影响;上面的内容就可以理解成 config.yaml config-dev.yaml config-test.yaml三个文件
-
server端项目结构如下:
-
启动服务ConfigServerApp,在地址栏输入:
http://localhost:8009/config-test.yaml 和
http://localhost:8009/config-dev.yaml
访问的格式可以是:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
名称 | 含义 |
---|---|
application | 配置文件名称 |
profile | 生效的profile 名称,如:dev test |
label | 配置文件所属的分支,如:master |
客户端服务搭建
- pom文件,关键是 spring-boot-starter-web 和 spring-cloud-starter-config
<?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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.uu</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>configclient-8010</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
-
配置文件。client端的配置文件需要注意,需要新建一个bootstrap.yaml的文件,需要将配置相关的写在bootstrap.yaml里;application.yaml是spring-boot的配置文件,spring cloud有的配置是可以写在这个里面,但是有的又不行,还没有去整理之间的区别;
- bootstrap.yaml
spring: cloud: config: profile: dev uri: http://localhost:8009 # 这里写config server的地址 label: master # 分支 name: config # 配置文件名称,好像也可以不用写,就会获取全量的yaml文件
- application.yaml
server: port: 8010 spring: application: name: config-client
-
启动类 ConfigClientApp
package com.xx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApp {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApp.class, args);
}
}
- controller ConfigClientController
package com.xx.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${info.sub}")
private String info;
@GetMapping("/config")
public String getConfig() {
return this.info;
}
}
- 启动服务,访问地址 http://localhost:8010/config
成功获取到数据,over~
代码可以在【码云】下载 https://gitee.com/aqu415/demo/tree/master/springcloud