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


边栏推荐
- ciscn_2019_en_2
- SQL按某字段去重 保留按某个字段排序最大值
- SSL unresponsive in postman test
- Mysql5.7.31 user defined installation details
- CaMKIIa和GCaMP6f是一樣的嘛?
- Some basic uses of mongodb
- Circuit board edge removal - precautions for V-CUT splitting machine
- Redux workflow + complete code of small examples
- 把控元宇宙产业的发展脉络
- Maintenance and key points of SMT Mounter
猜你喜欢

Is camkiia the same as gcamp6f?

Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases

Display unassigned virtual address after easyconnect connection

Flink reports error: a JNI error has occurred, please check your installation and try again

元宇宙中的法律与自我监管

Regular expression introduction and some syntax

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

When installing PSU /usr/bin/ld:warning: -z lazload ignore

从进程的角度来解释 输入URL后浏览器会发生什么?

Mining pit record of modified field information in Dameng database
随机推荐
Drag the mouse to rotate the display around an object
Machine vision: illuminating "intelligence" and creating a new "vision" world
STL tutorial 5-basic concepts of STL and the use of string and vector
mysql
鼠标拖拽围绕某个物体旋转展示
What are the red lines of open source that should not be trodden on?
Correct writing methods of case, number and punctuation in Chinese and English papers
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
idea设置mapper映射文件的模板
性能领跑云原生数据库市场!英特尔携腾讯共建云上技术生态
Login interceptor
No executorfactory found to execute the application
Apache基金会正式宣布Apache InLong成为顶级项目
mongodb
Explain from a process perspective what happens to the browser after entering a URL?
Middle order clue binary tree
How product managers control the progress of product development
leetcode. 14 --- longest public prefix
Logstash discards log data that does not match the file name exactly
Data synchronization