SpringBoot -- 服务网关APIGateway
APIGateway
- 对外提供服务接口
- 对内根据逻辑调用内部多个接口,进行信息聚合返回给调用者
- 异步调用无需等待反馈的服务
使用场景
- 商品详情: 需要调用商品基础信息、推荐信息、评价、排名接口
- 登录+积分:调用登录、积分规则链等接口
- 鉴权
- … …
Zuul
创建APIGateway module,引入spring-cloud-starter-zuul
build.gradle
apply plugin: 'org.springframework.boot'dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersionmavenBom "org.springframework.boot:spring-boot-dependencies:"+ springBootVersionmavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion}
}dependencies {compile('org.springframework.cloud:spring-cloud-starter-config')compile('org.springframework.cloud:spring-cloud-starter-eureka')compile('org.springframework.cloud:spring-cloud-starter-zuul')compile('org.springframework.cloud:spring-cloud-netflix-sidecar')compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')compile('org.springframework.cloud:spring-cloud-stream')compile('org.xerial.snappy:snappy-java:' + snappyVersion)compile ('org.springframework.boot:spring-boot-starter-web')compile('org.springframework.boot:spring-boot-starter-log4j2')compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)testCompile('org.springframework.boot:spring-boot-starter-test')testCompile group: 'junit', name: 'junit', version: '4.11'}
configurations {all*.exclude module: 'spring-boot-starter-logging'all*.exclude module: 'logback-classic'all*.exclude module: 'log4j-over-slf4j'all*.exclude module: 'snappy-java'
}
sourceSets {main {resources.srcDirs = ['src/main/resources', 'src/main/java']resources.includes = ['**/*.xml', '**/*.yml']}
}
jar {baseName = 'apigateway-bootcwenao'
}
配置application.yml 启用zuul
- /servers/** 为通过apigateway访问时此服务对外提供的path
- FEIGNSERVER 调用的服务在Eureka上注册的ServiceId,此为Feign server时的写的module
- 也可以采用Url的方式而不使用serviceid
zuul:routes:servers:path: /servers/**serviceId: FEIGNSERVER
设置zuul connect-timeout
zuul:max:host:connections: 200host:socket-timeout-millis: 60000connect-timeout-millis: 60000
hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 10000
Application
@SpringCloudApplication
@EnableSidecar
public class ApiGatewayBootcwenaoApplication {public static void main(String[] args) {SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args);}
}
测试
- 依次启动 discovery、configserver、feignserver、ribbonserver
- 访问apigateway,加上需要访问的server path
http://localhost:10002/servers/testFeign?content=Hello%20World
测试结果: Hello World for Springboot
Filter
- 与SpringMvc中的过滤器功能一样
- 例如检查请求来源
- 自定义Filter需要extend ZuulFilter
- 设置 filterType, 重写run内的业务逻辑
创建 AccessSignFilter
public class AccessSignFilter extends ZuulFilter {}
filterType类型
- pre:可以在请求被路由之前调用
- routing:在路由请求时候被调用
- post:在routing和error过滤器之后被调用
- error:处理请求时发生错误时被调用
在Application中启用
@SpringCloudApplication
@EnableZuulProxy
@EnableSidecar
public class ApiGatewayBootcwenaoApplication {public static void main(String[] args) {SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args);}@Beanpublic AccessSignFilter accessSignFilter(){return new AccessSignFilter();}}
代码
代码请移步 Github参考地址
如有疑问请加公众号(K171),如果觉得对您有帮助请 github start
SpringBoot -- 服务网关APIGateway相关推荐
- springboot+Zuul网关服务
springboot+Zuul网关服务 点关注不迷路,欢迎再来! 精简博客内容,尽量已专业术语来分享. 努力做到对每一位认可自己的读者负责. 帮助别人的同时更是丰富自己的良机. 网关服务有效的降低维护 ...
- Api-gateway服务网关gravitee.io的探索之路(一)
没有具体场景的技术都是耍流氓. 先说下公司场景,公司是一家金融公司,各种应用也有十几个,都是这些年积累下来的,团队也是五花八门,本土的,外包的,互相之间经常要对接,调用,每次大家都感觉特别麻烦.说实话 ...
- 带你入门SpringCloud 之 服务网关 Zuul
前言 服务端提供 RestFul API ,客户端如果想要使用某个服务直接调用服务的API 即可.但是在微服务环境中会有很多的具体服务,而客户端在需要使用众多的服务时在和具体的服务打交道这样虽然可以实 ...
- Spring Cloud入门-Gateway服务网关(Hoxton版本)
文章目录 Spring Cloud入门系列汇总 摘要 Gateway 简介 相关概念 创建 api-gateway模块 在pom.xml中添加相关依赖 两种不同的配置路由方式 使用yml配置 使用Ja ...
- 什么是微服务网关?SpringCloud Gateway保姆级入门教程
什么是微服务网关 SpringCloud Gateway是Spring全家桶中一个比较新的项目,Spring社区是这么介绍它的: 该项目借助Spring WebFlux的能力,打造了一个API网关.旨 ...
- Spring Cloud(六)服务网关 zuul 快速入门
服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...
- 微服务网关解决方案调研和使用总结 专题
一.什么是网关 1.1 什么是网关 API Gateway(APIGW / API 网关),顾名思义,是出现在系统边界上的一个面向API的.串行集中式的强管控服务,这里的边界是企业IT系统的边界,可以 ...
- 微服务之间调用经过网关吗_微服务网关入门
一.什么是服务网关服务网关 = 路由转发 + 过滤器 1.路由转发:接收一切外界请求,转发到后端的微服务上去: 2.过滤器:在服务网关中可以完成一系列的横切功能,例如权限校验.限流以及监控等,这些都可 ...
- Spring Cloud构建微服务架构-服务网关
通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: 愿意了解源码的朋友直接求求交流分享技术 一零三八七七四六二六 我们使用Sp ...
最新文章
- hdu2087 剪花布条 暴力/KMP
- java打开文件出错_打开文件错误太多,java.io.FileNotFoundException
- DOM的利用冒泡做的一个小程序
- 微服务架构实战篇(四):Spring boot2.0 + Mybatis +Druid监控数据库访问性能
- 深入理解c++之struct构造函数
- java file.length 单位_Java File length()用法及代码示例
- java stl分解_stl文件格式解析代码--java版
- 记录:MI 10 反复重启的原因之一
- vue路由传参的三种方式/含页面刷新参数丢失解决方案(详细)
- 瑞萨e2studio(6)----编译调试
- 黑客搜索大法(Google Hacking)
- 产品概念之2/4:三层次理论 —— 生产者主导视角的产品概念
- 火狐flash debug配置
- 网站流量统计分析系统-01
- 《Kubernetes故障篇:Kubernetes Node节点DiskPressure异常处理》
- 关于【商品计量单位以及这些计量单位换算】的设计
- 蝴蝶效应,鳄鱼法则,罗森塔尔效应,帕金森定律,手表定律,破窗理论,晕轮效应 ,霍桑效应,二八定律,木桶理论,马太效应,踢猫效应…………
- outlook仅限于此计算机如何解决,Outlook2013中IMAP方式已发送邮件、已删除邮件等文件夹注册失败...
- 爬取网站时返回的html是乱码问题解决
- VC++钩子使用之全局键盘钩子
热门文章
- 883.三维形体投影面积
- AJax 中的 xhr.states 和 xhr.status
- 土地利用转移矩阵分析与制图(以沮漳河流域为例)
- 【PASCALVOC】The Pascal Visual Object Classes Challenge: A Retrospective
- 女大学生被骗死亡,各国如何应对电话诈骗?
- PMD相位提取及相位展开简述
- perl 常用模块使用例子
- 技术分享 | UUID 很火但性能不佳?今天我们细聊一聊
- JavaSE基础加强-学习黑马程序员Java基础视频教程(P93开始)
- OpenCV-Python图形图像处理:split通道拆分和数组矩阵访问通道