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接口,这个接口中有一 ...
随机推荐
- 服务挂后Dump日志
JAVA_HOME=/usr/java OUTPUT_HOME=~/output DEPLOY_HOME=`dirname $0` HOST_NAME=`hostname` DUMP_PIDS=`ps ...
- Java 签名(SHA1WithRSA、SHA256WithRSA、SHA256withECDSA)
RSA1.RSA256 签名 public static String MakeSign(String Data) { try { byte[] data = Data.getBytes(); byt ...
- OpenJudge计算概论-成绩判断
/*========================================================= 成绩判断 总时间限制: 1000ms 内存限制: 6000kB 描述 输入一个0 ...
- myadmin不需要路劲提权之法
//觉得很典型的案例,就转载过来了.很多时候phpmyadmin都需要路劲才能写shell.否则拿到了myadmin也是无奈. 查看mysql版本 select version(); 版本是5.1 大 ...
- removeEntry
void VCGantt::removeEntry() { QModelIndexList selectedIndexes = ganttViewCommon->selectionModel() ...
- Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数
原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...
- python 中的一些基础算法:递归/冒泡/选择/插入
递归算法 如果一个函数包含了对自己的调用,那么这个函数就是递归的. 比如我们计算下1-7乘法的计算: def func(n): if n ==1 : return 1 return n*func(n- ...
- python中 将数字转化为人民币的形式
def fn(args): """ 将金额转化为人民币模式,带逗号分隔,保留小数点两位,四舍五入 :param args: :return: ""&q ...
- ABAP ole操作1
转自:https://www.cnblogs.com/jxzhu/p/8708686.html OLE整理: 1.定义,分别对应EXCEL,workbook(工作簿),sheet(页),单元格 DAT ...
- Redis学习笔记——Redis的基本操作
之前介绍过如何在ubuntu安装Redis服务器:https://www.cnblogs.com/zifeiy/p/9062738.html 接下来,我们在Redis上进行一些基本的操作. 所县使用命 ...