Spring Cloud(6):Zuul的基本使用
网关:API Gateway
系统对外唯一入口,介于客户端和服务端之间,处理非业务功能
提供路由请求,鉴权,监控,缓存,限流等功能
简单理解:小区门卫,防止非法人员入内,居民也可以问路
实际理解:假设我部署完成一个电商网站,网关的作用如下
1.前端发起的请求都会发送到网关,比如URL是/api/user,网关判断后跳转到用户服务模块的服务器
2.双十一等时期高并发访问,网关可以做负载均衡,或者对访问进行限流降级
3.对权限进行限制,比如用户不能修改商品库存,于是网关限制用户访问商品编辑模块服务器
Zuul不能凭空使用,至少要有服务模块以及Eureka Server
这里我就不重复写了,使用前文中提到的:
搭建Eureka Server和Product-Service:https://www.cnblogs.com/xuyiqing/p/10861541.html
使用Feign搭建Order-Service:https://www.cnblogs.com/xuyiqing/p/10869026.html
新建Zuul项目:
Project->New Project->Spring Initializr->命名为api-gateway
依赖Eureka和Zuul

然后是配置文件:前文都是yml格式的,感觉用不惯,还是继续用properties吧
主要是配置服务名称和Eureka Server地址
server.port=9000
spring.application.name=api-gateway
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
在启动类中加入注解:
package org.dreamtech.apigateway; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableZuulProxy
public class ApiGatewayApplication { public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
} }
依次启动Eureka Server->Product-Service->Order-Service->Api-Gateway项目
Eureka Server:

注册成功
Zuul的规则是:
zuul-host:zuul-port/service-name/**
我要访问的是Order-Service的/api/order/save路径
根据规则应该访问的是:
http://localhost:9000/order-service/api/order/save?user_id=1&product_id=1:
{"code":0,"data":{"id":0,"productName":"\"iPhone1 data from port=8771\"","tradeNo":"fe70c5d4-7467-43c6-902b-870bb8e763ed","price":1111,"createTime":"2019-05-18T03:16:32.165+0000","userId":1,"userName":null}}
成功
或者访问:http://localhost:9000/product-service/api/product/find?id=1
{"id":1,"name":"iPhone1 data from port=8771","price":1111,"store":10}
成功
一般情况下不会用/product-service和/order-service这种路径,于是想到自定义访问路径:
zuul.routes.order-service=/order/**
访问:http://localhost:9000/order/api/order/save?user_id=1&product_id=1即可
同时,原来的路径也没有失效
如果想让用户只能通过自定义路径访问,而不允许访问/product-service等路径
zuul.routes.order-service=/order/**
zuul.routes.product-service=/product/**
zuul.ignored-patterns=/*-service/**
如果不想开放某服务(不对某服务进行路由)
zuul.ignored-services=order-service
实际部署情况:各种服务通常在内网中,无法拿到IP,因此无法直接访问
用户通过公网IP只能访问到Api-Gateway,通过通过网关访问服务
就是下图这种方式

常见的问题解决:
1.多个服务的自定义路径不能设置成一样的
2.关于HTTP请求头的信息的问题
实验
@RequestMapping("/save")
@HystrixCommand(fallbackMethod = "saveOrderFail")
public Object save(@RequestParam("user_id") int userId, @RequestParam("product_id") int productId, HttpServletRequest request) {
String token = request.getHeader("token");
String cookie = request.getHeader("cookie");
System.out.println(token+" : "+cookie);
Map<String, Object> data = new HashMap<>();
data.put("code", 0);
data.put("data", productOrderService.save(userId, productId));
return data;
}
通过网关来访问Order-Service(Postman工具):

打印情况:
hrvboenoiqnjvbwo : null
可以得出结论:获取Cookie失败
原因:Zuul过滤了请求头中的Cookie信息
源码:
private Set<String> sensitiveHeaders = new LinkedHashSet(Arrays.asList("Cookie", "Set-Cookie", "Authorization"));
public void setSensitiveHeaders(Set<String> sensitiveHeaders) {
this.sensitiveHeaders = sensitiveHeaders;
}
处理:
zuul.sensitive-headers=
注意:不要感觉奇怪,这里就是这么写,看源码set方法置空即可
Spring Cloud(6):Zuul的基本使用的更多相关文章
- Spring Cloud Netflix Zuul 重试会自动跳过经常超时的服务实例的简单说明和分析
在使用E版本的Spring Cloud Netflix Zuul内置的Ribbon重试功能时,发现Ribbon有一个非常有用的特性: 如果某个服务的某个实例经常需要重试,Ribbon则会在自己维护的一 ...
- spring cloud(二) zuul
spring cloud 网关 zuul 搭建过程 1. 新建boot工程 pom引入依赖 <dependency> <groupId>org.springframework. ...
- spring cloud 通过zuul网关去请求的时候报404的几个原因。
spring cloud 中 zuul 网关的那些坑: 1.检查你的服务是否正常启动. 2.检查你的服务是否正常注册到注册中心. 3.zuul网关的路由规则是会把你注册在注册中心的serviceId ...
- spring cloud 配置zuul实用
在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin 技术背景 前面我们通过Ribbon或Feign实现了微服务之间的调用和负载均衡 ...
- Spring Cloud(五) --- zuul
微服务网关 在微服务架构中,后端服务往往不直接开放给调用端,而是通过一个API网关根据请求的url,路由到相应的服务.当添加API网关后,在第三方调用端和服务提供方之间就创建了一面墙,这面墙直接与调用 ...
- Spring Cloud (十三) Zuul:静态路由、静态过滤器与动态路由的实现
前言 本文起笔于2018-06-26周二,接了一个这周要完成的开发任务,需要先等其他人的接口,可能更新的会慢一些,还望大家见谅.这篇博客我们主要讲Spring Cloud Zuul.项目地址:我的gi ...
- spring cloud使用zuul实现反向代理和负载均衡
首先,这篇文章参考的是http://blog.didispace.com/springcloud5/这位大牛的博客.本人是通过这篇博客来学习zuul的,现在写的博客只是个人在学习时个人的一些感受和理解 ...
- Spring Cloud之Zuul网关集群
Nginx+Zuul 一主一备 或者 轮训多个 在微服务中,所有服务请求都会统一到Zuul网关上. Nginx 配置: #user nobody; worker_processes 1; #error ...
- Spring Cloud 之 Zuul基础.
一.概述 API 网关是一个更为智能的应用服务器,它的定义类似于面向对象设计模式中的 Facade 模式,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤 ...
- Spring Cloud之Zuul
API网关是一个更为智能的应用服务器,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤.它除了要实现请求路由.负载均衡.校验过滤等功能之外,还需要更多能力, ...
随机推荐
- SQL——视图、事务、锁、存储过程
https://www.bilibili.com/video/av15496406/?p=57 https://blog.csdn.net/u013630349/article/details/750 ...
- Node.js——body方式提交数据
引入核心模块 http,利用其 api(http.createServer) 返回一个 http.server 实例,这个实例是继承于net.Server,net.Server 也是通过net.cre ...
- 前端Unicode转码的好处
站长工具支持Unicode转码:http://tool.chinaz.com/Tools/Unicode.aspx (这是一个网页标题)转码后 ------>变为:\u8fd9\u662f\u4 ...
- vue2.0 路由传参(router-link传过去)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CentOS 7 挂载ntfs磁盘格式的U盘
因为CentOS 默认不识别NTFS的磁盘格式,所以我们要借助另外一个软件来挂载,那就是ntfs-3g了 自带的yum源没有这个软件,要用第三方的软件源,这里我用的是阿里的epel. 1. 切换到系统 ...
- 使用Caliburn.Micro系列1:新建项目并引入CM
一.WPF的几个MVVM模式实现 MVVMLight:小众的平民框架,实现简单粗暴. pass:最近更新在15年 官网: http://www.mvvmlight.net/ 最近一篇内容全面的好文: ...
- 卸载钩子 UnhookWindowsHookEx
The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindow ...
- php 阿里云短信验证码
阿里云短信服务:https://dysms.console.aliyun.com 1.准备 1.1.创建签名.模板 1.2.创建.使用阿里云秘钥 地址:https://usercenter.conso ...
- 02Hibernate基本配置
Hibernate基本配置 1.引入jar 2.建立项目 3.创建实体类 package com.sqlserver.domain; public class Customer { long cust ...
- JFinal怎么更改项目服务的端口
如图所示,运行时启动的端口是80,现在将它改成801: 可以在Debug configuration 或 Run configuration 弹出的窗口中配置,方法右击项目>properties ...