这是 Zuul 1.x: 同步阻塞网关的实现
后来,Zuul 2.x(简称基于Gateway): 基于Gateway异步非阻塞网关的实现。因为Netflix后期不再维护Zuul 2.x而是采用Gateway,并且Gateway原理和Zuul 2.x相同,所以基于Zuul 2.x简称基于Gateway

main

2 services:

  1. one springboot application providing a service/URL;
  2. another application, i.e. Zuul gateway, configured to provide another service which is based on 1st service.

为什么用网关zuul? 因为在微服务架构模式下,后端服务的实例数一般是动态的,比如可能加入一个新的服务,这个可能就有一个影射关系, i.e. 新加入的服务跟之前的服务有一定的联系,而对客户端而言很难发现动态改变的服务实例的访问地址信息。这个时候就会基于zuul做一个跟nginx类似的一个反向代理,对应某一个path,把它转给某一个server ID. 同时我们在zuul上面也用了zuulset对授权服务,健全服务的集成。

zuul 常见的有这两种配置的方式,一个是这种直接对 path 的转发,比如某一个网站直接转过去;另外一个是对 server ID,比如所有注册中心的服务都可以通过它的 server ID 做一个对应的请求的转发。

下面的zuul网关的用法是直接对path的转发。

附: 我发现服务的写法,都是: add dependency > resource下的application.properties > java下的类

1st service

(normal springboot project)

pom.xml:

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...

application.properties:

1
2
3
spring.application.home=student
server.port=8090
...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RestController
@SpringBootApplication
public class SpringBootZuulGatewayProxyStudentServiceApplication {

@RequestMapping(value = "/echoStudentName/{name}")
public String echoStudentName(@PathVariable(name = "name") String name) {
return "hello" + name;
}

@RequestMapping(value = "/getStudentDetails/{name}")
public Student getStudentDetails(@PathVariable(name = "name") String name) {
return new Student(name, "sh", "www");
}

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

access via:

1
2
http://localhost:8090/echoStudentName/lei
http://localhost:8090/getStudentDetails/lei

1st service keeps running. Next, make a Zuul gateway.

2nd service

(in a 2nd project)

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
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
@SpringBootApplication
@EnableZuulProxy // this annotation
public class SpringBootZuulgatewayproxyApplication {

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

@Bean
public PreFilter preFilter() {
return new PreFilter();
}

@Bean
public PostFilter postFilter() {
return new PostFilter();
}

@Bean
public ErrorFilter errorFilter() {
return new ErrorFilter();
}

@Bean
public RouteFilter routeFilter() {
return new RouteFilter();
}

}
1
2
3
4
5
6
# Zuul routes. For /student path here, we are routing to localhost:8090 with extra path after that
zuul.routes.student.url=http://localhost:8090
# Ribbon is auto integrated with Zuul and for this exercise we are not using that
ribbon.eureka.enabled=false
# Will start gateway server @8080
server.port=8080
1
2
http://localhost:8080/student/echoStudentName/lei
http://localhost:8080/student/getStudentDetails/lei

服务运行在端口8090,网关开启运行在端口8080,通过网关实现了服务的转发.


基于 Gateway实现API网关: 可以基于java代码或者基于配置实现