当前位置:网站首页>统一网关Gateway
统一网关Gateway
2022-06-25 22:36:00 【自由梦想程序员】
Gateway
作用



快速入门

新建模块gateway,添加依赖
<dependencies>
<!--nacos服务注册发现依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--网关gateway依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
创建启动类
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}

PS:lb就是loadBalance(负载均衡)的缩写
修改配置
server:
port: 10010
logging:
level:
cn.itcast: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
spring:
application:
name: gateway
cloud:
nacos:
server-addr: nacos:8848 # nacos地址
gateway:
routes:
- id: user-service # 路由标示,必须唯一
uri: lb://userservice # 路由的目标地址
predicates: # 路由断言,判断请求是否符合规则
- Path=/user/** # 路径断言,判断路径是否是以/user开头,如果是则符合
- id: order-service
uri: lb://orderservice
predicates:
- Path=/order/**
运行gateway模块

路由断言工厂




路由的过滤器配置



修改usercontroller文件



全局过滤器


在gateway包下新建个类AuthorizeFilter
// @Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 1.获取请求参数
ServerHttpRequest request = exchange.getRequest();
MultiValueMap<String, String> params = request.getQueryParams();
// 2.获取参数中的 authorization 参数
String auth = params.getFirst("authorization");
// 3.判断参数值是否等于 admin
if ("admin".equals(auth)) {
// 4.是,放行
return chain.filter(exchange);
}
// 5.否,拦截
// 5.1.设置状态码
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
// 5.2.拦截请求
return exchange.getResponse().setComplete();
}
@Override
public int getOrder() {
return -1;
}
}
输入网址localhost:10010/user/1?authorization=admin才可以进入页面
过滤器链连接顺序


网关的cors跨域配置


边栏推荐
- No executorfactory found to execute the application
- Qt优秀开源项目之九:qTox
- Idea kotlin version upgrade
- Shenzhen Taipower: the way of "communication" of the United Nations
- Should group by be used whenever aggregate functions are used in SQL?
- 鼠标拖拽围绕某个物体旋转展示
- Research and development practice of Kwai real-time data warehouse support system
- Introduction to anchor free decision
- Send mail tool class
- Solution to component stele in SMT chip processing
猜你喜欢

CaMKIIa和GCaMP6f是一样的嘛?

STL tutorial 5-basic concepts of STL and the use of string and vector

Mysql5.7.31 user defined installation details

How to design the product roadmap?

Drag the mouse to rotate the display around an object

《SQL优化核心思想》

Daily question brushing record (IV)

What are the red lines of open source that should not be trodden on?

After being trapped by the sequelae of the new crown for 15 months, Stanford Xueba was forced to miss the graduation ceremony. Now he still needs to stay in bed for 16 hours every day: I should have e

The development context of Ba Kong Yuan universe industry
随机推荐
ciscn_2019_en_2
Flink reports error: a JNI error has occurred, please check your installation and try again
Openresty chapter 01 introduction and installation configuration
The development context of Ba Kong Yuan universe industry
Things / phenomena / things / things / situations / appearances
Qt优秀开源项目之九:qTox
【图像检测】基于高斯过程和Radon变换实现血管跟踪和直径估计附matlab代码
Why do we need to make panels and edges in PCB production
logstash丢弃没有精准匹配到文件名的日志数据
10.2.2、Kylin_ Kylin installation, uploading and decompressing, verifying environment variables, starting and accessing
Ffmpeg version switching
信号处理函数内必须使用可重入函数
SQL to retain the maximum value sorted by a field
Resolve thread concurrency security issues
SQL按某字段去重 保留按某个字段排序最大值
Ora-01153: incompatible media recovery activated
Redux workflow + complete code of small examples
Idea kotlin version upgrade
Should group by be used whenever aggregate functions are used in SQL?
4 key points to help the product manage the project well