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


边栏推荐
- Redux workflow explanation + small examples
- 使用VS2022编译Telegram桌面端(tdesktop)
- Servlet response download file
- Things / phenomena / things / things / situations / appearances
- Cloud rendering and Intel jointly create the "core" era of cloud rendering
- Phoenix index
- Daily question brushing record (IV)
- Preorder and middle order traversal of forest
- 213. house raiding II
- Idea kotlin version upgrade
猜你喜欢

Typescript for Web Learning

leetcode. 14 --- longest public prefix

Daily question brushing record (IV)

Ssl/tls, symmetric and asymmetric encryption, and tlsv1.3

渲云携手英特尔,共创云渲染“芯”时代

Mining pit record of modified field information in Dameng database

【图像检测】基于高斯过程和Radon变换实现血管跟踪和直径估计附matlab代码
![[TSP problem] solving traveling salesman problem based on Hopfield neural network with matlab code](/img/a9/4fbe82fc77712f2e10119aacb99143.png)
[TSP problem] solving traveling salesman problem based on Hopfield neural network with matlab code

ciscn_2019_en_2

Stream data
随机推荐
Phoenix index
【超能云终端创领先机】如何在48小时内交付一座方舱医院?
mongodb
Compile the telegraph desktop side (tdesktop) using vs2022
SMT Mounter workflow
Maintenance and key points of SMT Mounter
元宇宙中的法律与自我监管
Send mail tool class
Apache基金会正式宣布Apache InLong成为顶级项目
Graduation season | fitting the best self in continuous exploration
事物/现象/事情/东西/情况/表象
Anaconda beginner's notes
Tensorrt PB to UF problem
Regular expression introduction and some syntax
Redisson 3.17.4 release
Function and principle of SPI solder paste inspection machine
Mysql5.7.31 user defined installation details
What are AOI, X-ray and ICT in SMT industry? What does it do?
11.1.1 overview of Flink_ Flink overview
[image detection] vascular tracking and diameter estimation based on Gaussian process and Radon transform with matlab code