Spring Cloud课题实验手册(七)

在本系列的 上一篇文章 演示如何使用 OpenFeign 简化 Rest 客户端的开发。本文将演示如何使用 Spring Cloud 框架中提供的配置管理服务

在本实验中,使用 git 作为配置文件的仓库,需要确保在本机已经安装了 git 软件。

构建配置仓库

建立一个目录作为配置仓库:

1
mkdir config-repo

进入该目录,并使用 git 命令进行初始化:

1
2
cd config-repo
git init .

软件在目录中新建两个配置文件,分别是:

config-client-development.properties, 内容为:

1
2
user.role=Developer
server.port=9091

config-client-production.properties, 内容为:

1
2
user.role=User
server.port=8081

构建配置服务

新建一个 maven 项目:

groupId: cn.com.hohistar.tutorial
artifactId: springcloud-config-server

然后将 pom.xml 修改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?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>cn.com.hohistar.tutorial</groupId>
<artifactId>springcloud-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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>Finchley.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>

然后在包: cn.com.hohistar.tutorial.springcloud.configserver 中新建一个启动类: SpringcloudConfigServerApplication, 内容如下:

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigServerApplication {

public static void main(String[] args) {

SpringApplication.run(SpringcloudConfigServerApplication.class, args);
}
}

为项目建立配置文件, 在 src/main/resource 中新建名为 application.xml 的文件:

1
2
3
4
5
6
7
8
9
server:
port: 8888

spring:
cloud:
config:
server:
git:
uri: file://${user.home}/config-repo

建立配置客户端

新建一个 maven 项目:

groupId: cn.com.hohistar.tutorial.springboot
artifactId: springboot-config-client

然后修改 pom.xml 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?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>cn.com.hohistar.tutorial.springboot</groupId>
<artifactId>springboot-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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>Finchley.SR1</spring-cloud.version>
</properties>

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

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

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

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

在包: cn.com.hohistar.tutorial.springboot.springbootconfigclient.web 新建一个 RestController, 名为: MessageRestController, 代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
@RefreshScope
@RestController
public class MessageRestController {

@Value("${user.role}")
private String message;

@RequestMapping("/msg")
String getMessage() {
return this.message;
}

}

建立启动类: SpringbootConfigClientApplication, 代码如下:

1
2
3
4
5
6
7
@SpringBootApplication
public class SpringbootConfigClientApplication {

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

在 src/main/resource 中加入配置文件: bootstrap.properties, 内容如下:

1
2
3
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888

现在,可以启动服务和客户端进行测试了。

本文标题:Spring Cloud课题实验手册(七)

文章作者:Morning Star

发布时间:2019年11月30日 - 07:11

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/microservice/spring-cloud/springcloud-practise-manual-07/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。