在微服务框中(Spring Cloud)存在很多服务,客户端逐个调用这些服务非常繁琐,Zuul提供了统一访问接口访问这些微服务。zuul的核心是一系列的filters, 其作用可以类比Servlet框架的Filter,或者AOP。
zuul的过滤器之间不能直接通信,它们之间通过一个RequestContext的静态类来进行数据传递的。RequestContext类中有ThreadLocal变量来记录每个Request所需要传递的数据。
Zuul有几种标准的过滤器类型
Zuul大部分功能都是通过过滤器来实现的。Zuul中定义了四种标准过滤器类型,这些过滤器类型对应于请求的典型生命周期。
(1) PRE:这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。
(2) ROUTING:这种过滤器将请求路由到微服务。这种过滤器用于构建发送给微服务的请求,并使用Apache HttpClient或Netfilx Ribbon请求微服务。
(3) POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。
(4) ERROR:在其他阶段发生错误时执行该过滤器。
Zuul的生命周期也就是对应的几种过滤器的类型。
Zuul提供的功能
设计注册中心
POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>eureka-server</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--
Eureka 服务依赖
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--
SpringBoot集成Spring Security
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8110
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8110/eureka/
security.user.name=chenqx
security.user.password=123
启动类
package com.test.eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EureKaServer
{
public static void main( String[] args )
{
SpringApplication.run(EureKaServer.class, args);
}
}
设计订单微服务
POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>eureka-service-order</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service-order Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=9100
spring.application.name=service-order
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true
OrderInfo实体类
package com.test.eureka_service_order;
import java.io.Serializable;
import java.sql.Date;
public class OrderInfo implements Serializable {
private static final long serialVersionUID = -7233238826463139634L;
private Long id;
private String name;
private String prdtName;
private Integer count;
private Integer amount;
private Date createDt = new Date(System.currentTimeMillis());
private String userId = null;
public OrderInfo() {
}
public OrderInfo(String name,String prdtName,Integer count,Integer amount,Date createDt,String userId) {
this.name = name;
this.prdtName = prdtName;
this.count = count;
this.amount = amount;
this.createDt = createDt;
this.userId = userId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrdtName() {
return prdtName;
}
public void setPrdtName(String prdtName) {
this.prdtName = prdtName;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
",prdtName=" + prdtName +
",count=" + count +
",amount=" + amount +
'}';
}
}
服务实现类
package com.test.eureka_service_order;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Date;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderServiceApiImpl{
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
public String hello(@RequestParam("name") String name) {
System.out.println("name="+name);
return "hello " + name;
}
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
public OrderInfo hello(@RequestParam("name") String name,
@RequestParam("prdtName") String prdtName) {
System.out.println("hello2 name="+name+",prdtName="+prdtName);
try {
name= URLDecoder.decode(name,"UTF-8");
prdtName= URLDecoder.decode(prdtName,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new OrderInfo(name, prdtName,1,1,null,null);
}
@RequestMapping(value = "/hello3", method = RequestMethod.GET)
public String hello(@RequestBody OrderInfo order) {
if (order == null) {
return "未知";
}
return order.toString();
}
}
启动类
package com.test.eureka_service_order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class OrderServer
{
public static void main( String[] args )
{
SpringApplication.run(OrderServer.class, args);
}
}
编写库存微服务
POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>eureka-service-order</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service-order Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=9101
spring.application.name=service-store
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true
服务实现类
package com.test.eureka_service_store;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Date;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StoreServiceApiImpl{
@RequestMapping(value = "/store", method = RequestMethod.GET)
public String hello(@RequestParam("name") String name) {
System.out.println("name="+name);
return "仓库名称= " + name;
}
}
启动类
package com.test.eureka_service_store;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class StoreServer
{
public static void main( String[] args )
{
SpringApplication.run(StoreServer.class, args);
}
}
编写Zuul代理
POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>eureka-service-order</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service-order Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=9300
spring.application.name=zuul-proxy
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true
启动类
package com.test.eureka_service_order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
public class ZuulServer
{
public static void main( String[] args )
{
SpringApplication.run(ZuulServer.class, args);
}
}
测试效果:
订单服务和库存服务都被Zuul统一代理访问
在浏览器中输入
http://localhost:9300/service-store/store?name=Java
访问库存服务
在浏览器中输入
http://localhost:9300/service-order/hello1?name=Java
访问订单服务