Spring Cloud Tutorial - 7

In this series of previous article demonstrates how to use OpenFeign to simplify the development of the Rest client. This article will demonstrate how to use the configuration management service provided in the Spring Cloud framework

In this experiment, using git as the repository of the configuration file, you need to ensure that the git software has been installed on this machine.

Build configuration Repository

Create a directory as a configuration repository:

1
mkdir config-repo

Enter the directory and use the git command to initialize:

1
2
cd config-repo
git init .

The software creates two configuration files in the directory, namely:

config-client-development.properties:

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

config-client-production.properties:

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

Build configuration service

Create a new maven project:

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

Then modify pom.xml to:

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>

Then create a new startup class in the package: cn.com.hohistar.tutorial.springcloud.configserver: SpringcloudConfigServerApplication, the content is as follows:

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);
}
}

Create a configuration file for the project, create a new file named application.xml in src/main/resource:

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

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

Build and configure the client

Create a new maven project:

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

Then modify the pom.xml file:

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>

In the package: cn.com.hohistar.tutorial.springboot.springbootconfigclient.web create a new RestController, named: MessageRestController, the code is as follows:

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

}

Create a startup class: SpringbootConfigClientApplication, the code is as follows:

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

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

Add the configuration file in src/main/resource: bootstrap.properties, the content is as follows:

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

Now you can start the service and client for testing。

本文标题:Spring Cloud Tutorial - 7

文章作者:Morning Star

发布时间:2021年07月21日 - 07:07

最后更新:2021年07月22日 - 07:07

原始链接:https://www.mls-tech.info/microservice/spring-cloud/spring-cloud-tutorial-07_en/

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