上海做展会的网站都有哪些,企业文化包括哪些方面内容,怎样简单做网站,擼擼擼做最好的导航网站目录 网关Gateway的概念#xff1a;
准备
使用
方式一
因为配置了网关所以可以直接通过gateway发送请求
方式二
修改配置前#xff1a;http://localhost:8082/provider/run
方式三(动态路由)
导入配置类 网关Gateway的概念#xff1a; Spring Cloud Gateway 是 Spri…目录 网关Gateway的概念
准备
使用
方式一
因为配置了网关所以可以直接通过gateway发送请求
方式二
修改配置前http://localhost:8082/provider/run
方式三(动态路由)
导入配置类 网关Gateway的概念 Spring Cloud Gateway 是 Spring 官方基于 Spring5.0 、 SpringBoot2.0 和 Project Reactor 等技术开发的网 关 旨在为微服务框架提供一种简单而有效的统一的 API 路由管理方式统一访问接口。 Spring Cloud Gateway 作为 Spring Cloud 生态体系中的网关目标是替代 Netflix 的 Zuul 其不仅提供统 一的路由方式并且基于 Filter 链的方式提供了网关基本的功能例如安全、监控 / 埋点和限流等等。 它是基于 Netty 的响应式开发模式。 1️⃣ 路由 route 路由是网关最基础的部分路由信息由一个 ID 一个目的 URL 、一组断言工厂和一 组 Filter 组成。如果断言为真则说明请求 URL 和配置的路由匹配。 2️⃣ 断言 Predicate Java8 中的断言函数 Spring Cloud Gateway 中的断言函数输入类型是 Spring5.0 框架中的 ServerWebExchange 。 Spring Cloud Gateway 中的断言函数允许开发者去定义匹配 来自 http Request 中的任何信息比如请求头和参数等。 3️⃣ 过滤器 Filter 一个标准的 Spring WebFilter Spring Cloud Gateway 中的 Filter 分为两种类型 Gateway Filter 和 Global Filter 。过滤器 Filter 可以对请求和响应进行处理。 准备
创建一个springboot项目命名为gateway通过该项目网关统一管理provider和consumer的所有请求 使用
将以下依赖导入到gateway项目
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactId
/dependency
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId
/dependency
方式一
配置gateway项目的yml文件
server:port: 8082
spring:application:name: gatewaycloud:nacos:server-addr: localhost:8848
gateway:
discovery:
locator:
#是否与服务发现组件进行结合,通过service-id(必须设置成大写)转发到具体的服务实例。默认
false
#为true代表开启基于服务发现的路由规则。
enabled: false
#配置之后访问时service-id无需大写
lower-case-service-id: true
因为配置了网关所以可以直接通过gateway发送请求 配置前http://localhost:8080/provider/run 配置后http://localhost:8082/provider/run 方式二
配置gateway项目的yml文件 server:port: 8082
spring:application:name: gatewaycloud:nacos:server-addr: localhost:8848
gateway:
discovery:
locator:
#是否与服务发现组件进行结合,通过service-id(必须设置成大写)转发到具体的服务实例。默认
false
#为true代表开启基于服务发现的路由规则。
enabled: false
#配置之后访问时service-id无需大写
lower-case-service-id: true
routes:
# 路由标识(id标识,具有唯一性)
配合读取类使用
- id: user-consumer-api
#目标服务地址(uri地址,请求转发后的地址),会自动从注册中心获得服务的IP,不需要手动写死
uri: lb://consumer
#优先级,越小越优先
#order: 999
#路由条件(predicates断言)
predicates:
# 路径匹配,
- Path/aa/**
filters:
#路径前缀删除示例请求/name/bar/foo,StripPrefix2,去除掉前面两个前缀之后,最后转
发到目标服务的路径为/foo
#前缀过滤,请求地址http://localhost:8084/usr/hello
#此处配置去掉1个路径前缀,再配置上面的 Path/usr/**,就将**转发到指定的微服务
#因为这个api相当于是服务名,只是为了方便以后nginx的代码加上去的,对于服务提供者
service-client来说,不需要这段地址,所以需要去掉
- StripPrefix1 修改配置前http://localhost:8082/provider/run 修改配置后http://localhost:8082/prov/run 方式三(动态路由) 配置gateway项目的yml文件 server:port: 8082
spring:application:name: gatewaycloud:nacos:server-addr: localhost:8848
gateway:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
# namespace: xxx-xx-xx-xx
data-id: dynamic-routing.json
group: DEFAULT_GROUP 使用动态路由需要配合读取类使用 dynamic-routing.json 文件
{refreshGatewayRoute: true,routeList: [{id: provider-api,predicates: [{name: Path,args: {_genkey_0: /ppp/**}}],filters: [{name: StripPrefix,args: {_genkey_0: 1}}],uri: lb://provider,order: 0}]
}
导入配置类
配置文件放在nacos后可以通过配置类解析出nacos中的json文件因为json文件配置了所有请求
上述代码只配置了provider的请求所以只能访问provider且通过 http://localhost:8082/ppp/run 进行访问。如果访问 http://localhost:8082/consumer/test01 会被网关拦截 配置类放在资源中有需要自取。