SpringCloud学习笔记(6):使用Zuul构建服务网关

摘要:
简介Zuul是Netflix提供的开源API网关服务器。Spring Cloud集成并增强了Zuul。服务网关Zuul聚合了所有微服务接口,并将它们统一对外公开。外部客户端只需要与服务网关交互。项目简介sc parent,父模块sc eureka,注册表sc provider,provider sc gateway。服务网关使用Zuul构建服务网关。1.创建子模块项目sc gateway,pom。xml:4.0.0com.cfsc parent0.0.1-SNAPSHOT˃sc gatewayorg.springframework.cloudspringfloud starter netflix eureka client˂/artifactId=org.springfframework.cloud˃˂artifactId=s”Spring cloud starter netflix zuul2.创建启动类网关。网关应用程序:packagegateway;importorg.springframework.boot。SpringApplication;importorg.springframework.boot.au配置。SpringBootApplication;importorg.springframework.cloud.netflix.zuul。EnableZuulProxy;@SpringBootApplication@EnableZuulProxypublicclassGatewayApplication{publicstaticvoidmain{SpringApplication.run;}}@EnableZuulProxyh和@EnableZuulServer:@EnableZuulProxy是@EnableZuul服务器的超集。@EnableZuulProxy包含@EnableZuulServer@EnableZuulProxy使用反向代理导入的所有筛选器,而@EnableZuurServer不使用任何代理。

简介

Zuul是Netflix提供的一个开源的API网关服务器,SpringCloud对Zuul进行了整合和增强。服务网关Zuul聚合了所有微服务接口,并统一对外暴露,外部客户端只需与服务网关交互即可。相对于内部服务而言,能够防止其被外部客户端直接访问而暴露服务的敏感信息,起到了保护作用。除此之外,Zuul还可以实现身份认证、数据监控、动态路由等功能。

项目介绍

  1. sc-parent,父模块(请参照SpringCloud学习笔记(1):Eureka注册中心)
  2. sc-eureka,注册中心(请参照SpringCloud学习笔记(1):Eureka注册中心)
  3. sc-provider,提供者(请参照SpringCloud学习笔记(1):Eureka注册中心)
  4. sc-gateway,服务网关

使用Zuul构建服务网关

1.在父模块下创建子模块项目sc-gateway,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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-gateway</artifactId>
  
  <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>
  </dependencies>
</project>

2.创建启动类gateway.GatewayApplication:

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

@SpringBootApplication
@EnableZuulProxy
public class GatewayApplication {
	public static void main(String[] args) {
		SpringApplication.run(GatewayApplication.class, args);
	}
}

@EnableZuulProxyh和@EnableZuulServer:

  1. @EnableZuulProxy是@EnableZuulServer的超集,@EnableZuulProxy包含@EnableZuulServer导入的所有过滤器。

  2. @EnableZuulProxy使用反向代理,@EnableZuulServer不使用任何代理。

3.创建application.yml:

server:
  port: 8088

spring:
  application:
    name: sc-gateway
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/  

zuul:
  routes:
    sc-provider: /sp/** #将serviceId为sc-provider的服务映射到/sp/**路径

4.测试

依次启动注册中心sc-eureka、提供者sc-provider、网关sc-gateway,以下是通过Zuul访问提供者和直接访问提供者的结果:
SpringCloud学习笔记(6):使用Zuul构建服务网关第1张

其他常用配置

1.忽略指定服务

zuul:
  ignored-services: serviceId1,serviceId2 #忽略服务serviceId1,serviceId2

2.忽略所有服务,只代理指定的服务

zuul:
  ignored-services: '*' #*为忽略所有服务,只代理sc-provider服务
  routes:
    sc-provider: /sp/** 

3.指定访问路径前缀,设置之后只能通过带前缀访问

zuul:
  prefix: /yc 
  routes:
    sc-provider: /sp/** 

4.指定服务的url

zuul:
  routes:
    sc-provider:
      path: /sp/**
      url: http://localhost:8081 #指定服务sc-provider的url,不从Eureka注册中心获取。

这种配置方式不会作为HystrixCommand执行,也不会使用Ribbon来平衡多个url的负载。要实现这些目标,可以使用静态服务器列表(listOfServers)或者指定serviceId。

5.指定敏感Header,防止敏感Header外泄

zuul:
  routes:
    sc-provider:
      path: /sp/**
	  sensitiveHeaders: Cookie,Set-Cookie,Authorization #将会覆盖全局zuul.sensitiveHeaders的值
      url: http://localhost:8081

如果要设置全局的敏感Header可以设置zuul.sensitiveHeaders的值。Cookie,Set-Cookie,Authorization为sensitiveHeaders的默认值,如果不想设置敏感header,可以把sensitiveHeaders设置成空列表:

zuul:
  routes:
    sc-provider:
      path: /sp/**
	  sensitiveHeaders: 
      url: http://localhost:8081

6.忽略Header

zuul:
  ignoredHeaders: Header1, Header2 #Header1和Header2将不会传播到其他的微服务中

Zuul的路由端点

当@EnableZuulProxy和Spring Boot Actuator配合使用时,Zuul会暴露一个路由管理端点/routes,通过这个路由端点可以查看到Zuul当前映射的路由列表信息。

1.修改sc-gateway的pom.xml,新增Spring Boot Actuator依赖:

	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

2.修改sc-gateway的application.yml,开启/routes端点的访问:

management:
  endpoints:
    web:
      exposure:
        include: 'routes'

3.访问/routes端点

依次启动注册中心sc-eureka、提供者sc-provider、网关sc-gateway,然后访问http://localhost:8088/actuator/routes/details,显示路由列表信息如下:

SpringCloud学习笔记(6):使用Zuul构建服务网关第2张

Zuul过滤器

Zuul的核心是一系列过滤器,它们能够在HTTP请求和响应路由期间执行一系列操作。Zuul提供了一个框架来动态读取、编译和运行这些过滤器,过滤器之间不直接通信,它们通过对每个请求惟一的RequestContext共享数据。

1.Zuul过滤器类型

  • PRE Filters:在请求路由到具体的服务之前执行。
  • ROUTING Filters:用于将请求路由到微服务。
  • POST Filters:在请求路由到微服务之后执行。
  • ERROR Filters:在其他阶段发生错误时执行。

2.Zuul过滤器特性

  • Type:Zuul过滤器的类型,决定过滤器在请求的哪个阶段起作用。
  • Execution Order:规定过滤器的执行顺序,值越小,越先执行。
  • Criteria:Filter执行所需的条件。
  • Action:如果满足条件,则执行的操作。

3.Zuul请求生命周期图

SpringCloud学习笔记(6):使用Zuul构建服务网关第3张

4.自定义Zuul过滤器

新建类gateway.filter.MyZuulFilter:

package gateway.filter;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

public class MyZuulFilter extends ZuulFilter{
	
	/**
	 * 具体执行逻辑
	 */
	@Override
	public Object run() throws ZuulException {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		if (request.getParameter("name") != null) {
			System.out.println("你好," + request.getParameter("name"));
    	}
		return null;
	}

	/**
	 * 判断你该过滤器是否要执行
	 */
	@Override
	public boolean shouldFilter() {
		return true;
	}

	/**
	 * 过滤器执行顺序
	 */
	@Override
	public int filterOrder() {
		return 1;
	}

	/**
	 * 过滤器类型
	 */
	@Override
	public String filterType() {
		return "pre";
	}
}

启动类GatewayApplication中添加配置:

	@Bean
	public MyZuulFilter MyZuulFilter(){
		return new MyZuulFilter();
	}

依次启动注册中心sc-eureka、提供者sc-provider、网关sc-gateway,然后访问http://localhost:8088/sp/book/list?name=小明,MyZuulFilter过滤器将会执行,控制台输出:你好,小明。

免责声明:文章转载自《SpringCloud学习笔记(6):使用Zuul构建服务网关》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇线性电源与开关电源的区别vue项目加载前空白的动画过渡效果下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringCloud微服务实战-Zuul-APIGateway(十)

本文转自:http://blog.csdn.net/qq_22841811/article/details/67637786#准备工作 1 API Gateway 2 Zuul介绍 2.1 zuul的功能 Routing in an integral part of a microservice architecture. For example, /...

systemctl java jar 添加jar文件开机启动项

转载自:https://blog.csdn.net/slqgenius/article/details/85778578 一,原由 因为使用 nohup 和 & 这种方式启动jar包的话,只会放在后台里面执行,如果某天,人为或机器故障等原因,触发重启了。那样程序就会关闭,不清楚系统的人,可能都不知道这个需要启动。 所以,这里使用Centos7系统...

九、网关(Gateway)

上一代网关zuul 1.X:https://github.com/Netflix/zuul/wiki 当前网关gateway:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/ 一、概述简介   Gateway是在...

Springcloud之Zuul网关入门

Spring Cloud实现微服务的架构基本成型:   使用Spring Cloud Netflix中的Eureka实现了服务注册中心以及服务注册与发现;而服务间通过Ribbon或Feign实现服务的消费以及均衡负载。   为了使得服务集群更为健壮,使用Hystrix的融断机制来避免在微服务架构中个别服务出现异常时引起的故障蔓延。 Zuul是Netflix...

SpringCloud zuul 网关限流分析

最近项目中 spring cloud zuul 运用到限流功能,打算配置一下就直接使用,不过在压测与调优过程中遇到一些没有预测到的问题,附上排查与解析结果 yml、pom配置 强烈推荐,按最新github上的文档配,可以避免搜到一些已经废弃不用的配置方式! https://github.com/marcosbarbero/spring-cloud-zuul...

为服务中网关的作用

什么是网关 随着互联网的快速发展,当前以步入移动互联、物联网时代。用户访问系统入口也变得多种方式,由原来单一的PC客户端,变化到PC客户端、各种浏览器、手机移动端及智能终端等。同时系统之间大部分都不是单独运行,经常会涉及与其他系统对接、共享数据的需求。所以系统需要升级框架满足日新月异需求变化,支持业务发展,并将框架升级为微服务架构。“API网关”核心组件是...