Spring Cloud入门教程-路由网关-Zuul

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27828675/article/details/83501982

注意:这里用到的项目都是在之前几篇文章讲解用到的项目工程基础上进行的,在这一系列博客写完后会提供源码地址。

项目源码及相关说明请查看此文:Spring Cloud入门教程-简介

前边几篇文章已经讲解了 Netflix的一系列组件,包括服务发现和注册组件 Eureka、负载均衡组件Ribbon、声明式调用组件 Feign和熔断器组件 Hystrix。本章讲解 Netflix构建微服务的另一个组件智能路由网关组件Zuul。Zuul作为微服务系统的网关组件,用于构建边界服务(Edge Service),致力于动态路由、过滤、监控、弹性伸缩和安全。

为什么需要Zuul

Zuul作为路由网关组件,在微服务架构中有着非常重要的作用,主要体现在以下6个方面.
a.zuul、 Ribbon以及 Eureka相结合,可以实现智能路由和负载均衡的功能,zuul能够将请求流量按某种策略分发到集群状态的多个服务实例
b.网关将所有服务的API接口统一聚合,并统一对外暴露。外界系统调用AP接口时,都是由网关对外暴露的API接口,外界系统不需要知道微服务系统中各服务相互调用的复杂性。微服务系统也保护了其内部微服务单元的API接口,防止其被外界直接调用,导致服务的敏感信息对外暴露。
c.网关服务可以做用户身份认证和权限认证,防止非法请求操作API接口,对服务器起到保护作用。
d.网关可以实现监控功能,实时日志输出,对请求进行记录
e.网关可以用来实现流量监控,在高流量的情况下,对服务进行降级
f.API接口从内部服务分离出来,方便做测试。

Zuul 的工作原理

Zuul的核心是一系列过滤器,可以在Htp请求的发起和
响应返回期间执行一系列的过滤器。Zuul包括以下4种过滤器。
A.PRE过滤器:它是在请求路由到具体的服务之前执行的,这种类型的过滤器可以做安全验证,例如身份验证、参数验证等
B. ROUTING过滤器:它用于将请求路由到具体的微服务实例。在默认情况下,它使用Http Client进行网络请求.
C.POST过滤器:它是在请求已被路由到微服务后执行的。一般情况下,用作收集统计信息、指标,以及将响应传输到客户端。
D. ERROR过滤器:它是在其他过滤器发生错误时执行的。

        当一个客户端 Request请求进入Zul网关服务时,网关先进入 pre filter”,进行一系列的验证、操作或者判断。然后交给“ routing filter”进行路由转发,转发到具体的服务实例进行逻辑处理、返回数据。当具体的服务处理完后,最后由“ post filter”进行处理,该类型的处理器处理完之后,将Response 信息fanh返回给客户端。

实例

搭建Zuul服务

创建新Mudule eureka-client-zuul

pom.xml 如下:

<?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>
	<parent>
		<groupId>com.springcloud.demo</groupId>
		<artifactId>springcloud-demo</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<artifactId>eureka-client-zuul</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-client-zuul</name>
	<description>Demo project for Spring Boot</description>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<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>



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


</project>

在启动类EurekaClientZuulApplication 添加注解@EnableZuulProxy:

@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
@ComponentScan("com.springcloud.demo.eurekaclientzuul")
public class EurekaClientZuulApplication {

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

添加配置:

server.port=5000
spring.application.name=eureka-client-zuul
eureka.client.service-url.defaultZone=http://localhost:8791/eureka/

zuul.routes.ribbon.path=/ribbon/**
zuul.routes.ribbon.serviceId=eureka-client-ribbon

zuul.routes.feign.path=/feign/**
zuul.routes.feign.serviceId=eureka-client-feign

配置好后,以/ribbon/**开头的请求会被分发到 eureka-client-ribbon 服务,以/feign/**开头的请求会被分发到 eureka-client-feign 服务,如下图:

启动eureka-server ,两个eureka-client 实例,启动eureka-client-feign,eureka-client-ribbon,eureka-client-zuul.

请求http://localhost:5000/feign/main,交替返回8792,8793

请求http://localhost:5000/ribbon/main,交替返回8792,8793

如果需要添加前缀如版本号可以配置:

zuul.prefix=v1

请求http://localhost:5000/v1/ribbon/main,交替返回8792,8793。

猜你喜欢

转载自blog.csdn.net/qq_27828675/article/details/83501982
今日推荐