Spring Cloud Tutorial - 2

In this series of previous article, we practiced how to build a registration and discovery service. Now, we are going to practice registering a business service.

In this experiment, we use an example built on Spring Boot to serve as a developed business segment. For example construction, please refer to Spring Boot 构建Rest服务实验手册(一)

To use the Spring Boot application as a registerable service of Spring Cloud, you only need to simply change the following three places in the project:

Add dependency library

In the project’s pom.xml file:

  1. Change the version number of the project’s parent element spring-boot-starter-parent to: 2.0.5.RELEASE, the modified version is as follows:
1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
  1. Add the following dependency libraries and dependency management items:
1
2
3
4
5
6
7
8
  <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Modify the startup class

Add @EnableEurekaClient annotation to the startup class of Spring Boot application, the code is as follows:

1
2
3
4
5
6
7
8
@EnableEurekaClient
@SpringBootApplication
public class TodoServiceApplication {

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

Delete test class

Delete the test class in the test/java directory.

Modify the application.yaml configuration file

Add the following content in the project configuration file: application.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:
application:
name: cloud-simple-service

eureka:
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
healthcheck:
enabled: true
lease:
duration: 5

Start the program and verify

Start the application and visit the registration server to see if the current application has been registered:

1
http://localhost:8761

Next

In next step, we will practice a Gateway in Spring Cloud.

本文标题:Spring Cloud Tutorial - 2

文章作者:Morning Star

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

最后更新:2022年01月03日 - 11:01

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

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