单位做网站需要多少钱,微信音乐音频怎么关闭,化工企业网站模板,网站标题psd#xff08;1#xff09;Filter#xff08;过滤器#xff09;#xff1a;
和Zuul的过滤器在概念上类似#xff0c;可以使用它拦截和修改请求#xff0c;并且对上游的响应#xff0c;进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的…1Filter过滤器
和Zuul的过滤器在概念上类似可以使用它拦截和修改请求并且对上游的响应进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。
2Route路由 网关配置的基本组成模块和Zuul的路由配置模块类似。一个Route模块由一个 ID一个目标 URI一组断言和一组过滤器定义。如果断言为真则路由匹配目标URI会被访问。
3Predicate断言
这是一个 Java 8 的 Predicate可以使用它来匹配来自 HTTP 请求的任何内容例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。
案例内容
gateway基本目录 bootstrap.yml
server:port: 8081
spring:profiles:active: devapplication:name: tt-sc-gatewaycloud:nacos:username: nacospassword: nacosconfig:server-addr: 192.168.xx.xxx:8848file-extension: yml#开启nacos作为配置中心默认值true
# enabled: true
# # 配置文件读取的nacos命名空间ID默认值public
# namespace: 7c6ef0e3-5384-499a-9357-25fa3b9c0a1e
# # 配置文件在nacos命名空间中的分组默认值DEFAULT_GROUP
# group: DEFAULT_GROUP
# # 配置文件的文件前缀配置文件名称默认值${spring.application.name}-${spring.profiles.active}
# prefix: ${spring.application.name}-${spring.profiles.active}
# # 配置文件的文件后缀文件类型默认值properties
# file-extension: yaml
# # 配置内容的编码方式默认值UTF-8
# encode: UTF-8
# # 获取配置的超时时间单位ms默认值3000
# timeout: 3000
# # 开启监听和自动刷新动态感知配置变化默认值true
# refresh-enabled: true################################################ 引入扩展配置同一分组或不同分组# extension-configs:# 配置支持共享的 Data Id# - data-id: global.yaml# 配置 Data Id 所在分组缺省默认 DEFAULT_GROUP# group: DEFAULT_GROUP# 配置Data Id 在配置变更时是否动态刷新缺省默认 false# refresh: truediscovery:server-addr: 192.168.xx.xxx:8848
application-dev.yml
spring:cloud:gateway:discovery:locator:# 是否与服务发现组件进行结合通过 serviceId 转发到具体的服务实例。默认为falseenabled: truelower-case-service-id: true #使用小写service-idroutes: # 网关路由配置- id: feignconsumer # 路由id,自定义,只要唯一即可# 目标服务地址uri地址请求转发后的地址# uri: http://127.0.0.1:8081 路由的目标地址http 就是固定地址uri的协议为lb表示启用Gateway的负载均衡功能。uri: lb://tt-sc-feign-consumer # 路由的目标地址lb就是负载均衡,后面跟服务名称predicates: # 路由断言也就是判断请求是否符合路由规则的条件;转发地址格式uri/archive- Path/user/** # 这个是按照路径匹配只要以 /user/ 开头就符合要求fitters:- RewritePath/user/?(?segment.*),/$\{segment}- id: feignprovideuri: lb://tt-sc-feign-providepredicates:- Path/provide/**# 在这个时间之后的请求才会被转发#- After2031-04-13T15:14:47.43308:00[Asia/Shanghai]globalcors: # 全局的跨域处理add-to-simple-url-handLer-mapping: true # 解决options 请求被拦截问题corsConfigurations:[/**]: # 指定 拦截哪些请求allowed0rigins: # 允许哪些网站的跨域请求- http:xxx.xxxx- http://www.you.comallowedMethods: # 允许的跨域 ajax 的请求方式- GET- POST- DELETE- PUT- OPTIONSallowedHeaders: * # 允许在请求中携带的头信息allowCredentials: true # 是否允许携带cookiemaxAge: 360000 # 这次跨域检测的有效
GatewayApplication.java
package com.tt.gateway;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;SpringBootApplication
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}
pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdmanage/artifactIdgroupIdcom.example/groupIdversion0.0.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdtt-sc-gateway/artifactIddependencies!-- nacos 客户端 作为 注册与发现--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactIdversion2.2.3.RELEASE/version/dependency!-- nacos 配置中心 --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactIdversion2.2.3.RELEASE/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactIdversion2.2.4.RELEASE/version!-- version2.2.2.RELEASE/version--exclusions!-- 排除web依赖--exclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/exclusion/exclusions/dependency/dependencies
/projectfeignconsumer\feignprovide可参照Feign的demo Spring Cloud 之 Feign 简介及简单DEMO的搭建
测试结果