spring cloud中代理服务器zuul的使用
spring cloud中代理服务器zuul的使用
主流网关:
zuul
kong 基于nginx的API Gateway
nginx+lua
1、新建项目,选择eureka discovery 和zuul
2、启动类中增加 @EnableZuulProxy
3、修改配置文件后缀名为yml,并在配置中增加端口号、应用名称和注册中心地址,如下:
server:
port: 9000
spring:
application:
name: api-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:7880/eureka #注册中心地址
4、访问
http://192.168.136.128:8651/api/v1/orderfeignhystrix/save?userId=2&productId=2
修改为
http://192.168.136.128:9000/orderfeignhystrix-service/api/v1/orderfeignhystrix/save?userId=2&productId=2
http://192.168.136.128:8765/api/v1/product/list
修改为
http://192.168.136.128:9000/product-service/api/v1/product/list
5、自定义路由转发
zuul:
routes:
product-service: /apigateway/**
访问
http://192.168.136.128:9000/apigateway/api/v1/product/list
6、环境隔离:
需求 :不想让默认的服务对外暴露接口
product-service/api/v1/product/list
配置:
zuul:
ignored-patterns:
- /*-service/api/v1/product/list
可以访问
http://192.168.136.128:9000/apigateway/api/v1/product/list
不可以访问
http://192.168.136.128:9000/product-service/api/v1/product/list
原生地址仍旧可以访问
http://192.168.136.128:8765/api/v1/product/list
7、三个注意事项:
1)、路由名称定义问题
路由映射重复覆盖问题
zuul:
routes:
product-service: /apigateway/**
orderfeignhystrix-service-service: /apigateway/**
这样会覆盖,product-service会无法访问
可以增加子目录来区分
zuul:
routes:
product-service: /apigateway/product/**
orderfeignhystrix-service-service: /apigateway/order/**
2)、Http请求头过滤问题(在 routers类中可以看到这个定义sensitiveHeaders)
默认session等请求是关闭的
"Cookie", "Set-Cookie", "Authorization"
在yml文件中增加zuul的routes属性sensitiveHeaders:
修改后为
zuul:
routes:
product-service: /apigateway/product/**
orderfeignhystrix-service: /apigateway/order/**
ignored-patterns:
- /*-service/api/v1/product/list
sensitiveHeaders:
测试方法:在order服务的controller的save中增加代码,并使用postman测试,增加如下代码
String token = httpServletRequest.getHeader("token");
String session=httpServletRequest.getHeader("session");
System.out.println("token is:"+token);
System.out.println("session is:"+session);
使用postman进行测试
http://192.168.136.128:9000/orderfeignhystrix-service/api/v1/orderfeignhystrix/save?userId=2&productId=2
增加header中的session和token属性
3)、过滤器执行顺序问题 ,过滤器的order值越小,越先执行
spring cloud中代理服务器zuul的使用的更多相关文章
- spring cloud 学习(6) - zuul 微服务网关
微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService.ProductService.UserService...,为了让调用更简单,一般会在这些服务前端再封装一层,类似下 ...
- Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)
Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...
- Spring Cloud中Feign如何统一设置验证token
代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...
- Spring Cloud 入门 之 Zuul 篇(五)
原文地址:Spring Cloud 入门 之 Zuul 篇(五) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口 ...
- Spring Cloud 服务网关Zuul
Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...
- Spring Cloud Gateway VS Zuul 比较,怎么选择?
Spring Cloud Gateway 是 Spring Cloud Finchley 版推出来的新组件,用来代替服务网关:Zuul. 那 Spring Cloud Gateway 和 Zuul 都 ...
- Spring Cloud 网关服务 zuul 三 动态路由
zuul动态路由 网关服务是流量的唯一入口.不能随便停服务.所以动态路由就显得尤为必要. 数据库动态路由基于事件刷新机制热修改zuul的路由属性. DiscoveryClientRouteLocato ...
- 详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失
在Spring Cloud中我们用Hystrix来实现断路器,Zuul中默认是用信号量(Hystrix默认是线程)来进行隔离的,我们可以通过配置使用线程方式隔离. 在使用线程隔离的时候,有个问题是必须 ...
- Spring Cloud中负载均衡器概览
在上篇文章中(RestTemplate的逆袭之路,从发送请求到负载均衡)我们完整的分析了RestTemplate的工作过程,在分析的过程中,我们遇到过一个ILoadBalancer接口,这个接口中有一 ...
随机推荐
- 20191214数组之四:数字不相同的完全平方数(关于数位上数字判断与sprintf)
sprintf用法参见之前的随笔;(以解决):
- IDEA 同时打开多个项目
打开IDEA Settings-->System Settings-->Open project in new wodow
- 【译】Solr in Action 第二章
2.1 2.2 2.3 基本废话 2.4 基本废话
- Android插件化(三):OpenAtlas的插件重建以及使用时安装
Android插件化(三):OpenAtlas的插件重建以及使用时安装 转 https://www.300168.com/yidong/show-2778.html 核心提示:在上一篇博客 An ...
- Linux给力的Shell命令
查看目录下面的所有目录 ls /dev/ -F |grep "/" bus/ disk/ input/ 说明: ls -F命令将文件和目录后面添加一个特定的符号; ...
- 复制粘贴引发的鸠占鹊巢——IDEA复制项目导致sources root复用了另一个项目
复制粘贴大法一向是程序猿的利器,但有时也会引发一些拎不清的麻烦关系来.比如我们现在想新建一个项目,为了快速而对原来的uis-gateway动用了复制粘贴大法,然后改改项目名就成了uis-applica ...
- python中pop(),popitem()的整理
在python中,列表,字典,有序字典的删除操作有些凌乱,所以决定记录下,以便以后用乱了. 列表: 列表删除有三种方式: l.pop() l.remove() del l[3:8] 已下面的code为 ...
- (二十)sql基础
sql基础 --单表查询 select * from student; select * from score; --投影查询 select * from student; --条件查询 select ...
- 微信小程序之对象转化为数组
对象转成数组方式一(数组里面是一个个number类型的元素) let dictObject= { , , , , }; // 对象转成数组方式一 var createArr = [] for (let ...
- UIwindow ---密码框
程序运行显示结果如下 : 验证密码输入错误显示如下: 代码如下 : 1> //// PasswordInputWindow.m// UIWindow--密码框//// Created by ...