当前位置:网站首页>12. Gateway new generation gateway
12. Gateway new generation gateway
2022-07-01 10:14:00 【Bitter candy】
1. General introduction
1.1 What is it? ?


Spring Cloud Gateway The use of Webflux Medium reactor-netty Responsive programming components , The bottom layer uses Netty Communication framework
gateway Official website :
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
zuul github:
https://github.com/Netflix/zuul/wiki
1.2 What can I do? ?
Reverse proxy
authentication
flow control
Fuse
Log monitoring
Where is the gateway in the microservice architecture ?
1.3 With Zuul Why did you come out again gateway
1.3.1.neflix Not very reliable ,zuul2.0 Keep skipping tickets , Delayed release

1.3.2.SpringCloud Gateway Has the following characteristics

1.3.3 SpringCloud Gateway And Zuul The difference between
Zuul1.x Model



1.3.4 Three core concepts


1.3.5Gateway Workflow
Core logic : Routing and forwarding + Execute the filter chain


2. newly build Module cloud-gateway-gateway9527
2.1 Change pom
<dependencies>
<!-- newly added gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.2 Change yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true # Enable the function of dynamically creating routes from the registry , Routing with microservice name
routes:
- id: payment_routh # The routing ID, There are no fixed rules, but the requirements are unique , Suggested service name
#uri: http://localhost:8001 # The routing address of the service after matching
uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/** # Assertion , Route with matching path
- id: payment_routh2
#uri: http://localhost:8001 # The routing address of the service after matching
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/** # Assertion , Route with matching path
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
Gateway Gateway routing can be configured in two ways :
a: In profile yml Middle configuration
b: Inject... Into the code RouteLocator Of Bean ( Not recommended )
2.3 Main startup class
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
SpringApplication.run( GateWayMain9527.class,args);
}
}
2.4 test
We don't want to expose 8001 port , Hope that in 8001 Put a layer on the outside 9527
start-up 7001, start-up 8001, start-up 9527 gateway
Before adding a gateway :http://localhost:8001/payment/get/31
After adding the gateway :http://localhost:9527/payment/get/31
2.5 Dynamic routing through microservice name
By default Gateway According to the service list of the registry , Create a dynamic route with the micro service name on the registry as the path to forward , So as to realize the function of dynamic routing . There is no need to ip The address is written in the configuration , Just write the service name .
It should be noted that uri The agreement is lb, Means to enable Gateway Load balancing function of .
lb://serviceName yes spring cloud gateway Load balancing created for us automatically in microservices uri
One eureka7001+ Two service providers 8001/8002,http://localhost:9527/payment/lb 8001/8002 Switch between two ports

3.Predicate Use
3.1 What is it? ?



3.2 Predicate Use
1-3 The method to get the expression in :
public class TimeUtil {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
}
}
2022-06-30T23:06:41.697+08:00[Asia/Shanghai]
- Path: The most common way to match the request path , Others are rarely used
summary :Predicate Just to implement a set of matching rules , Let the request come and find the corresponding Route To deal with . If it doesn't match , You can't access .
4.Filter Use

The self-contained filter is generally not very useful , All are custom global GlobalFilter
Implement these two interfaces :GlobalFilter ,Ordered
spring Of order in . The smaller the value , The higher the priority , The higher the value, the lower the priority , Priority first .
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter,Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("*********come in MyLogGateWayFilter: "+new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("username");
if(StringUtils.isEmpty(uname )){
log.info("***** The user is called Null Illegal users ,(┬_┬)");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);// Give someone a response
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
http://localhost:9527/payment/lb?username=z3 Normal visit . Only with username For normal access .
边栏推荐
- 客户端如何请求数据库?
- Button button clear border
- CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
- Which securities company has a low, safe and reliable Commission for stock trading and account opening
- Common penetration tools -goby
- How do clients request databases?
- 678. 有效的括号字符串
- Does anyone know the logic of limit statement execution in Clickhouse? In the picture, the SQL above can be executed successfully
- tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
- 年薪100万,在北上广深买的起房子吗?
猜你喜欢

TC8:UDP_USER_INTERFACE_01-08

What a high commission! The new programmer's partner plan is coming. Everyone can participate!

bash: ln: command not found

Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years

架构实战营 模块九:设计电商秒杀系统
![C [byte array] and [hexadecimal string] mutual conversion - codeplus series](/img/d2/dad88f53701c7cd7638bd4983cbb4b.png)
C [byte array] and [hexadecimal string] mutual conversion - codeplus series

SQL SERVER2014删除数据库失败,报错偏移量0x0000...

渗透常用工具-Goby

IDEA运行报错Command line is too long. Shorten command line for...

数据库的增删改查问题
随机推荐
Ssh server rejects password, try again; Permitrootlogin yes invalid problem
The programmer was beaten.
Postgraduate entrance examination vocabulary 2023 sharing (1)
年薪100万,在北上广深买的起房子吗?
Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录
SQL Server列一相同的情况下,如何取列二的最大值,并重新生成表
It is interesting to understand MMAP in this way!
CCNP Part XII BGP (IV)
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
Configure load balancing
Eat a rich woman's melon...
mysql cdc能把能把op字段拿出来吗
leetcode:111. 二叉树的最小深度
7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
venv: venv 的目录结构
机器学习之线性回归详解
C one line code calculates the MD5 value of the file - codeplus series
Who's still buying three squirrels
Strange, why is the ArrayList initialization capacity size 10?