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接口,这个接口中有一 ...
随机推荐
- a=”hello”和b=”世界”编码成bytes类型
a="hello" c=a.encode(encoding='utf-8') a = b'hello' b="世界" b = b.encode(encoding ...
- c++ 珊格画椭圆
#ifndef _TEST_H #define _TEST_H #include <iostream> #include <math.h> using namespace st ...
- zabbix(4)数据库表分区优化
一.zabbix 数据库存储 zabbix-server将采集到的数据存储在数据库(mysql.oracle等),而数据存储的大小与每秒处理的数量量有关,因此数据存储取决于以下两个因数: (1)Req ...
- 修改input输入框的样式
直接上代码 <style> .input{ -web-kit-appearance:none; -moz-appearance: none; font-size:1.4em; height ...
- 使用 Nexus3 Repository Manager 搭建 npm 私服
公司里一般都有自己的私服,用于管理封装的工具插件等,Nexus2主要是用于maven/gralde仓库的统一管理,Nexus3则添加了npm插件,可以对npm提供支持,其实用于npm仓库管理的还有一个 ...
- Vue进阶(Bus/作用域slot/动态组件)
一.Vue非父子组件传值(Bus/总线/发布订阅模式/观察者模式) 我们在之前已经知道了父子传值.父组件传递过来了的值,在子组件通过props接受,然后就可以使用了. 也学过了隔代传值,均是通过pro ...
- 深度解读Facebook刚开源的beringei时序数据库——数据压缩delta of delta+充分利用内存以提高性能
转自:https://yq.aliyun.com/topic/58?spm=5176.100239.blogcont69354.9.MLtp4T 摘要: Facebook最近开源了beringei时序 ...
- 走进JavaWeb技术世界9:Java日志系统的诞生与发展
本文转自[码农翻身] ## 一个著名的日志系统是怎么设计出来的? # 1前言 Java帝国在诞生之初就提供了集合.线程.IO.网络等常用功能,从C和C++领地那里吸引了大量程序员过来加盟,但是却有意无 ...
- postman测试API
首先创建环境变量 再次在请求参数中,可以应用环境变量,只需要在地址中引用环境变量即可 将返回的参数设置到环境变量中 如已经设置好环境变量,在认证中,选择Bearer Token,然后设置Token为环 ...
- 安卓APP在线升级
安卓APP在线升级 通过IDHTTP组件在线下载APP到手机中,然后自动安装这个APP程序. 1)在线下载APP程序 需引用单元: {$IFDEF ANDROID} FMX.Helpers.Andro ...