当前位置:网站首页>Unified gateway
Unified gateway
2022-06-26 00:47:00 【Free dream programmer】
Gateway
effect



Quick start

New module gateway, Add dependency
<dependencies>
<!--nacos Service registration found dependencies -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- gateway gateway rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
Create startup class
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}

PS:lb Namely loadBalance( Load balancing ) Abbreviation
Modify the configuration
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 Address
gateway:
routes:
- id: user-service # Route marking , Must be unique
uri: lb://userservice # The destination address of the route
predicates: # Route assertion , Determine whether the request meets the rules
- Path=/user/** # Path assertion , Determine whether the path is based on /user start , If yes, it meets
- id: order-service
uri: lb://orderservice
predicates:
- Path=/order/**
function gateway modular 

Route assertion factory




Filter configuration for routing



modify usercontroller file 



Global filter


stay gateway Create a new class under the package AuthorizeFilter
// @Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 1. Get request parameters
ServerHttpRequest request = exchange.getRequest();
MultiValueMap<String, String> params = request.getQueryParams();
// 2. Get... In the parameter authorization Parameters
String auth = params.getFirst("authorization");
// 3. Judge whether the parameter value is equal to admin
if ("admin".equals(auth)) {
// 4. yes , release
return chain.filter(exchange);
}
// 5. no , Intercept
// 5.1. Set status code
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
// 5.2. Intercept request
return exchange.getResponse().setComplete();
}
@Override
public int getOrder() {
return -1;
}
}
Enter url localhost:10010/user/1?authorization=admin You can enter the page 
Filter chain connection sequence


The gateway cors Cross domain configuration


边栏推荐
- DPVS fullnat mode deployment
- DPVS fullnat mode kept
- 1-9network configuration in VMWare
- 元宇宙中的法律与自我监管
- 【TSP问题】基于Hopfield神经网络求解旅行商问题附Matlab代码
- Tensorrt PB to UF problem
- Permission design = function permission + Data permission
- Chapter V exercises (124, 678, 15, 19, 22) [microcomputer principles] [exercises]
- SQL中只要用到聚合函数就一定要用到group by 吗?
- Atlas200dk brush machine
猜你喜欢

Research and development practice of Kwai real-time data warehouse support system

Web学习之TypeScript

The development context of Ba Kong Yuan universe industry

No executorfactory found to execute the application

【图像检测】基于高斯过程和Radon变换实现血管跟踪和直径估计附matlab代码

使用VS2022编译Telegram桌面端(tdesktop)

Apache基金会正式宣布Apache InLong成为顶级项目

CaMKIIa和GCaMP6f是一樣的嘛?

AD20(Altium Designer) PCB 高亮网络

Machine vision: illuminating "intelligence" and creating a new "vision" world
随机推荐
SQL中只要用到聚合函数就一定要用到group by 吗?
WordPress
Typescript for Web Learning
How to deliver a shelter hospital within 48 hours?
Login interceptor
1-11Vmware虚拟机常见的问题解决
Qt优秀开源项目之九:qTox
Binary sort tree
Setting up a cluster environment under Linux (2) -- installing MySQL under Linux
How to bypass SSL authentication
Analysis and comparison of common test methods for PCBA in SMT chip processing industry
Flink reports error: a JNI error has occurred, please check your installation and try again
原型和原型链的理解
1-9network configuration in VMWare
1-11solutions to common problems of VMware virtual machine
Is camkiia the same as gcamp6f?
Preorder and middle order traversal of forest
[image detection] vascular tracking and diameter estimation based on Gaussian process and Radon transform with matlab code
学习识别对话式问答中的后续问题
Apache foundation officially announced Apache inlong as a top-level project