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


边栏推荐
- 信号处理函数内必须使用可重入函数
- Preorder and middle order traversal of forest
- What are the red lines of open source that should not be trodden on?
- mysql cluster
- AD20(Altium Designer) PCB 高亮网络
- 使用VS2022編譯Telegram桌面端(tdesktop)
- Idea set the template of mapper mapping file
- Apache基金会正式宣布Apache InLong成为顶级项目
- 1-11solutions to common problems of VMware virtual machine
- Regular expression introduction and some syntax
猜你喜欢

"Method not allowed", 405 problem analysis and solution

Post ordered clue binary tree
![Final review [machine learning]](/img/63/d617a95592b1499cff0161bd5c3e9e.png)
Final review [machine learning]

把控元宇宙产业的发展脉络

机器视觉:照亮“智”造新“视”界

leetcode.14 --- 最长公共前缀

Compiler Telegram Desktop end (tdesktop) en utilisant vs2022

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

leetcode. 14 --- longest public prefix

Why is it best to use equals for integer comparisons
随机推荐
10.2.2、Kylin_ Kylin installation, uploading and decompressing, verifying environment variables, starting and accessing
“Method Not Allowed“,405问题分析及解决
Installation and configuration of gradle environment
Mining pit record of modified field information in Dameng database
86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
STL tutorial 5-basic concepts of STL and the use of string and vector
Idea kotlin version upgrade
[TSP problem] solving traveling salesman problem based on Hopfield neural network with matlab code
Preordered clue binary tree
Graduation season | fitting the best self in continuous exploration
Why do we need to make panels and edges in PCB production
Some basic uses of mongodb
flink报错:No ExecutorFactory found to execute the application
No executorfactory found to execute the application
Redux workflow explanation + small examples
C IO stream (II) extension class_ Packer
What are AOI, X-ray and ICT in SMT industry? What does it do?
Blob
Idea view unit test coverage
What do SMT operators do? operating duty?