spring-cloud开发Greenwich版:服务网关(十三)

本文新建网关(gateway)微服务,利用网关实现路由映射以及过滤器、熔断器等功能
微服务’ralab-zuul’

  • New microservice ‘ralab-zuul’ and add the Maven pring-cloud-starter-netflix-zuul denpendencys
  • Config eureka server info to application.yml file
  • Add @EnableZuulProxy to the application class

其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
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ralab</groupId>
<artifactId>ralab-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ralab-zuul</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</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>

配置application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
instance:
# ip-address: 127.0.0.1
prefer-ip-address: true # 补充
client:
service-url:
defaultZone: http://localhost:8761/eureka/

management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always

RalabZuulApplication文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.ralab.ralabzuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

//@EnableEurekaClient
@SpringBootApplication
@EnableZuulProxy
public class RalabZuulApplication {

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

}

运行RalabZuulApplication,打开http://127.0.0.1:8040/actuator/routes,显示网关默认的路由

测试打开http://127.0.0.1:8040/nlp-service/nlp/api,路由成功

我们可以自定义路由规则,配置如下

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
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
instance:
# ip-address: 127.0.0.1
prefer-ip-address: true # 补充
client:
service-url:
defaultZone: http://localhost:8761/eureka/

management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always

zuul:
ignored-services: '*'
prefix: /api
routes:
nlp-service: /nlpservice/**

打开http://127.0.0.1:8040/actuator/routes

测试打开http://127.0.0.1:8040/nlp-service/nlp/api

坚持原创技术分享,您的支持是我前进的动力!