疯狂Spring Cloud连载(22)Web项目中使用Zuul
本文节选自《疯狂Spring Cloud微服务架构实战》
京东购买地址::https://item.jd.com/12256011.html
当当网购买地址::http://product.dangdang.com/25201393.html
Spring Cloud教学视频:http://blog.****.net/boxiong86/article/details/78399104
Spring Cloud电子书:http://blog.****.net/boxiong86/article/details/78488226
Web项目中使用Zuul
关于Zuul
SpringCloud集群提供了多个组件,用于进行集群内部的通信,例如服务管理组件Eureka,负载均衡组件Ribbon。如果集群提供了API或者Web服务,需要与外部进行通信,比较好的方式是添加一个网关,将集群的服务都隐藏到网关后面。这样的做法,对于外部客户端来说,无需关心集群的内部结构,只需关心网关的位置等信息;对于SpringCloud集群来说,不必过多的暴露服务,提升了集群的安全性。
代理层作为应用集群的大门,在技术选取上尤为重要,很多传统的解决方案,在软件上选择了Nginx、Apache等服务器。Netflix提供了自己的解决方案:Zuul。Zuul是Netflix的一个子项目,SpringCloud将Zuul做进一步的实现与封装,整合到spring-netflix项目中,为微服务集群提供过代理、过滤、路由等功能。
Web项目整合Zuul
新建一个名称为“first-router”的Maven项目,项目使用的依赖如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
需要加入“spring-cloud-starter-zuul”的依赖,由于Zuul底层使用了HttpClient,因此还要加入相应的依赖。为了能让Web项目开启对Zuul的支持,在应用类中加入@EnableZuulProxy注解,请见代码清单7-1。
代码清单7-1:codes\07\02\first-router\src\main\java\org\crazyit\cloud\GatewayApplication.java
@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(GatewayApplication.class).properties(
"server.port=8080").run(args);
}
}
注意该项目的启动端口为8080。完成以上工作后,一个拥有Zuul功能的Web项目已经建立,接下来,将测试它的路由功能。
测试路由功能
前一小节已经建立了路由器项目,接下来建立源服务的项目,测试例子的结构请见图7-1所示。
图7-1测试例子结构图
新建名称为“book-server”的Maven项目,该项目是一个最普通的Spring Boot项目,使用以下的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
为“book-server”添加一个“/hello”的服务,项目的启动类以及控制器,请见代码清单7-2。
代码清单7-2:codes\07\02\book-server\src\main\java\org\crazyit\cloud\BookApplication.java
@SpringBootApplication
@RestController
public class BookApplication {
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET) public String hello(@PathVariable String name) {
return "hello " + name;
}
public static void main(String[] args) {
new SpringApplicationBuilder(BookApplication.class).properties(
"server.port=8090").run(args);
}
}
为了简单起见,本例将启动类与控制器写到一起,注意“book-server”的端口为8090。在控制器中,建立了一个“/hello/{name}”的服务,成功调用后,会返回相应的字符串。接下来,修改“first-router”项目的配置文件,让其进行转发工作。
修改“first-router”项目的application.yml文件,加入以下内容:
zuul:
routes:
books:
url: http://localhost:8090
加入以上配置后,发送给http://localhost:8080/books的所有请求,都会被转发到8090端口,也就是访问“first-router”项目,实际上最终会调用“book-server”的服务。启动两个应用,在浏览器中输入以下地址:http://localhost:8080/books/hello/crazyit,可以看到浏览器输出如下:
hello crazyit
根据输出结果可知,发送的请求已经被转发到“book-server”进行处理。
过滤器运行机制
前面例子的路由项目,我们使用了@EnableZuulProxy注解,开启该注解后,在Spring容器初始化时,会将Zuul的相关配置初始化,其中包含一个Spring Boot的bean:ServletRegistrationBean,该类主要用于注册Servlet。Zuul提供了一个ZuulServlet类,在Servlet的service方法中,执行各种Zuul过滤器(ZuulFilter)。图7-2为HTTP请求在ZuulServlet中的生命周期。
图7-2 HTTP请求的生命周期
ZuulServlet的service方法接收到请求后,会执行“pre”阶段的过滤器,再执行“routing”阶段的过滤器,最后执行“post”阶段的过滤器。其中“routing”的过滤器,会将请求转发到“源服务”,源服务可以是第三方的Web服务,也可以是Spring Cloud的集群服务。在执行pre和routing阶段的过滤器时,如果出现异常,则会执行“error”过滤器。整个过程的HTTP请求、HTTP响应、状态等数据,都会封装到一个RequestContext的对象中,这将在后面章节中讲述。
大致了解了Zuul的运行机制后,下面将开始讲解Spring Cloud中使用Zuul。
本文节选自《疯狂Spring Cloud微服务架构实战》
Spring Cloud教学视频:http://blog.****.net/boxiong86/article/details/78399104
Spring Cloud电子书:http://blog.****.net/boxiong86/article/details/78488226