Spring Cloud Gateway网关
简介:
Spring Cloud Gateway是Spring官网基于Spring 5.0、 Spring Boot 2.0、Project Reactor等技术开发的网关服务
Spring Cloud Gateway基于Filter链提供网关基本功能:安全、监控/埋点、限流等
Spring Cloud Gateway为微服务架构提供简单、有效且统一的API路由管理方式
Spring Cloud Gateway是替代Netflix Zuul的一套解决方案
Spring Cloud Gateway组件的核心是一系列的过滤器,通过这些过滤器可以将客户端发送的请求转发(路由)到对 应的微服务。 Spring Cloud Gateway是加在整个微服务最前沿的防火墙和代理器,隐藏微服务结点IP端口信息, 从而加强安全保护。Spring Cloud Gateway本身也是一个微服务,需要注册到Eureka服务注册中心。
网关的核心功能是:过滤和路由
核心概念:
路由(route) 路由信息的组成:由一个ID、一个目的URL、一组断言工厂、一组Filter组成。如果路由断言 为真,说明请求URL和配置路由匹配。
断言(Predicate) Spring Cloud Gateway中的断言函数输入类型是Spring 5.0框架中的 ServerWebExchange。Spring Cloud Gateway的断言函数允许开发者去定义匹配来自于HTTP Request中的 任何信息比如请求头和参数。
过滤器(Filter) 一个标准的Spring WebFilter。 Spring Cloud Gateway中的Filter分为两种类型的Filter,分 别是Gateway Filter和Global Filter。过滤器Filter将会对请求和响应进行修改处理
具体实现:
新建工程,创建启动类,编写配置
cloud:
gateway:
routes:
- id: bill-service-route
#uri: http://127.0.0.1:9091
uri: lb://bill-service
predicates:
- Path=/qqq/bill/get/**
filters:
- StripPrefix=1 #忽略第一个前缀,即/api
# - PrefixPath=/bill/get #去除前缀相当于前端不用输入/bill/get
过滤器:
Gateway作为网关的其中一个重要功能,就是实现请求的鉴权。而这个动作往往是通过网关提供的过滤器来实现的。
常见的如下:
AddRequestHeader 对匹配上的请求加上Header
AddRequestParameters 对匹配上的请求路由添加参数
AddResponseHeader 对从网关返回的响应添加Header
StripPrefix 对匹配上的请求路径去除前缀
执行的生命周期:
Spring Cloud Gateway 的 Filter 的生命周期也类似Spring MVC的拦截器有两个:“pre” 和 “post”。“pre”和 “post” 分别会在请求被执行前调用和被执行后调用
请求鉴权:一般 GatewayFilterChain 执行filter方法前,如果发现没有访问权限,直接就返回空
异常处理:一般 GatewayFilterChain 执行filter方法后,记录异常并返回。
服务调用时长统计: GatewayFilterChain 执行filter方法前后根据时间统计。
跨域配置:
一般网关都是所有微服务的统一入口,必然在被调用的时候会出现跨域问题。
跨域:在js请求访问中,如果访问的地址与当前服务器的域名、ip或者端口号不一致则称为跨域请求。若不解决则 不能获取到对应地址的返回结果。
如:从在http://localhost:9090中的js访问 http://localhost:9000的数据,因为端口不同,所以也是跨域请求。
globalcors:
cors-configurations:
'[/**]':
allow-credentials: true
allowed-origins:
- "http://localhost:8080"
- "http://localhost:9091"
allowed-headers: "*"
allowed-methods:
- OPTIONS
- GET
- POST
Spring Cloud Config分布式配置中心
简介:
在分布式系统中,由于服务数量非常多,配置文件分散在不同的微服务项目中,管理不方便。为了方便配置文件集 中管理,需要分布式配置中心组件。在Spring Cloud中,提供了Spring Cloud Config,它支持配置文件放在配置服 务的本地,也支持放在远程Git仓库(GitHub、码云)
这里以码云为例,首先创建仓库
如何创建配置文件
配置文件的命名方式:{application}-{profile}.yml 或 {application}-{profile}.properties
application为应用名称
profile用于区分开发环境,测试环境、生产环境等
如user-dev.yml,表示用户微服务开发环境下使用的配置文件。
然后搭建配置中心微服务
启动类添加@EnableConfigServer //开启配置服务
配置文件如下:
server:
port: 12000
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/X3374/my-config.git
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
management:
endpoint:
web:
exposure:
include: bus-refresh
接下来是获取配置中心的配置
改造bill-consumer
添加jar包
删除application.yml
添加bootstrap.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password:
url: jdbc:mysql://localhost:3306/bill-manager?useUnicode=true&characterEncoding=UTF-8
thymeleaf:cache: false
cloud:
config:
name: bill
profile: consumer
label: master
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
bootstrap.yml文件也是Spring Boot的默认配置文件,而且其加载的时间相比于application.yml更早。 application.yml和bootstrap.yml虽然都是Spring Boot的默认配置文件,但是定位却不相同。bootstrap.yml 可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。application.yml 可以用来定义应用级别 的参数,如果搭配 spring cloud config 使用,application.yml 里面定义的文件可以实现动态替换。 总结就是,bootstrap.yml文件相当于项目启动时的引导文件,内容相对固定。application.yml文件是微服务 的一些常规配置参数,变化比较频繁。需要注意的是数据库配置需要放在bootstrap.yml,不然可能会报错
Spring Cloud Bus服务总线
Spring Cloud Bus是用轻量的消息代理将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管 理。也就是消息总线可以为微服务做监控,也可以实现应用程序之间相互通信。 Spring Cloud Bus可选的消息代理 有RabbitMQ和Kafka。
安装otp和rabbitmq
config配置文件:
# 配置rabbitmq信息;如果是都与默认值一致则不需要配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoint:
web:
exposure:
include: bus-refresh
biill-consumer配置文件:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
在Controller层添加注释:@RefreshScope//刷新配置