Spring Cloud Tutorial - 3

In the previous article of this series, we transformed an ordinary Spring Boot project into a service in the Spring Cloud framework. In this article, we will implement a Gateway service in order to call the modified Todo service.

Build the Gateway project

Build a simple Maven project in IDEA, name the groupId: cn.com.hohistar.tutorial, and the artifactId: springcloud-gateway-srv, 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
68
69
70
71
72
73
74
75
76
77
78
79
<?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.hohistgar.tutorial</groupId>
<artifactId>springcloud-gateway-srv</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springcloud-gateway-srv</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-netflix-zuul</artifactId>
</dependency>

<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

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

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

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


</project>

As you can see, we will use two components of Spring Cloud: netflix-zuul and netflix-eureka-client

New startup class

Create a new package in the src/main/java directory: cn.com.hohistgar.tutorial.springcloudgatewaysrv, and then create a new class in the package, named: SpringcloudGatewaySrvApplication, copy the following code in the content:

1
2
3
4
5
6
7
8
9
10
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudGatewaySrvApplication {

public static void main(String[] args) {

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

In fact, SpringcloudGatewaySrvApplication is a SpringBoot startup class, but two annotations have been added: @EnableZuulProxy, @EnableEurekaClient.

@EnableEurekaClient-used to register itself (the current application service) to the registration and discovery service

@EnableZuulProxy-Enable the function of Gateway

Define configuration parameters

Create a new file named: application.yaml in the src/main/resources directory, and copy the following content into the 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
server:
port: 9098

spring:
application:
name: gateway

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


zuul:
prefix: /api
routes:
todo-by-service:
path: /todo/**
serviceId: cloud-todo-service

As you can see, the entire configuration file is divided into four sections. The first and second sections are common Spring Boot project settings, indicating the port and project name of the project. The third paragraph is the client configuration of the registration service. Note that the defaultZone is replaced with the address and port of the registration service actually running in the environment. The last part is the configuration of the Gateway. In the current example, we only configure the routing to forward the access to the todo path to the cloud-todo-service.

Run and verify

Note: Before running Gateway, make sure that the future services in manuals one and two have been running

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

1
http://localhost:8761

You can see that the todo service and gateway service have been registered in the registration server。

Then open it in the browser:

1
http://localhost:9098/api/todo/todo

If it is normal, you should get the to-do list returned by cloud-todo-service, which is a json array.

Next

In next step, we will add a current limiting function to the Gateway.

本文标题:Spring Cloud Tutorial - 3

文章作者:Morning Star

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

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

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

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