当前位置:网站首页>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


边栏推荐
- Post ordered clue binary tree
- Anaconda beginner's notes
- 1-9Vmware中网络配置
- jarvisoj_level2_x64
- 10.2.2、Kylin_ Kylin installation, uploading and decompressing, verifying environment variables, starting and accessing
- 原型和原型链的理解
- Explanation of chip processing manufacturer__ What is ICT? What is the main test? Advantages and disadvantages of ICT testing?
- 基于OpenVINOTM开发套件“无缝”部署PaddleNLP模型
- Run the test program using rknn-toolkit-lite2 for rk3568 development board
- Cloud rendering and Intel jointly create the "core" era of cloud rendering
猜你喜欢

Installation and configuration of gradle environment

Example: use C # Net to teach you how to develop wechat official account (21) -- using wechat to pay online collection: H5 method

删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?

统一网关Gateway

AD20(Altium Designer) PCB 高亮网络

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

Cloud rendering and Intel jointly create the "core" era of cloud rendering

Idea view unit test coverage

CaMKIIa和GCaMP6f是一樣的嘛?

Explain from a process perspective what happens to the browser after entering a URL?
随机推荐
Preordered clue binary tree
统一网关Gateway
WordPress
Wireshark's analysis of IMAP packet capturing
What do SMT operators do? operating duty?
QT custom QSlider with cursor
Regular expression introduction and some syntax
ciscn_2019_en_2
Redisson 3.17.4 release
1-11solutions to common problems of VMware virtual machine
Causes and solutions to the phenomenon of PCBA monument in SMT patch processing
元宇宙中的法律与自我监管
Learn to identify follow-up questions in dialogue Q & A
idea设置mapper映射文件的模板
Compile the telegraph desktop side (tdesktop) using vs2022
4 key points to help the product manage the project well
Flink报错:Error: A JNI error has occurred, please check your installation and try again
SQL按某字段去重 保留按某个字段排序最大值
【图像检测】基于高斯过程和Radon变换实现血管跟踪和直径估计附matlab代码
Send mail tool class