Spring Cloud Tutorial - 1

This tutorial aims to help readers complete the construction and use of the SpringCloud microservice system to help readers understand the concept of microservices in practice.

As the first part of the tutorial, we first build the registration and discovery service.

Build the registration and service discovery service projects

Build a simple Spring Boot project in IDEA, name the groupId: cn.com.hohistar.tutorial, and the artifactId: springcloud-discovery-server, and then replace the content in the original pom.xml file with the following:

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
<?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-discovery-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springcloud-discovery-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/>
</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-netflix-eureka-server</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-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>

Custom startup class

Add @EnableEurekaServer annotation to the original startup class. The modified code is as follows:

1
2
3
4
5
6
7
8
9
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudDiscoveryServerApplication {

public static void main(String[] args) {

new SpringApplicationBuilder(SpringcloudDiscoveryServerApplication.class).web(true).run(args);
}
}

Add configuration file

Rename the file application.properties in src/main/resouces to: application.yml. And fill in the following content:

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

eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0

Start the app

Start the application in IDEA, and then open it in the browser

1
http://localhost:8761

You can see that the registration service has been started.

Next

In next step, we will practice how to change an existing Spring Boot application into a registerable service.

本文标题:Spring Cloud Tutorial - 1

文章作者:Morning Star

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

最后更新:2021年07月21日 - 08:07

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

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