当前位置:网站首页>Introduction and use of gateway
Introduction and use of gateway
2022-07-07 16:35:00 【Xiao Wei wants to learn from Zhu Lao】
Preface :
Recently, I am learning about micro Services , Read the relevant courses of dark horse , Will relate Gateway The knowledge of has summed up some , I hope I can help you guys and deepen your impression
If there is anything to be improved in the article, please give me more advice
Xiao Wei first thanked everyone for their support 
Personal home page : Xiao Wei wants to learn from the guys
🧑 Personal profile : Hello everyone , I'm Xiao Wei , A man who wants to make progress with everyone
Current status : I'm a sophomore now , Internship in a satisfactory companyIf the boss is preparing for an interview , You can use the artifact I used to brush questions before my internship Brush the artifact and click here
Welcome to : Here is CSDN, Where I sum up my knowledge , Welcome to my blog , My dear boss
Use feedback of Niuke part , I feel pretty good , Helped me find my favorite company , I hope you can also improve a lot through it
List of articles
Gateway introduction
gateway (Gateway) also called Internetwork connector 、 Protocol converter . The default gateway realizes network interconnection above the network layer , Is the most complex network interconnection device , It is only used for interconnection of two high-level protocols . The structure of the gateway is similar to that of the router , The difference is the interconnection layer . Gateway can be used for WAN interconnection , Can also be used for LAN interconnection .
Gateway is a kind of computer system or device that acts as the task of transformation . Use in different communication protocols 、 Data format or language , Even between two systems with completely different architectures , The gateway is a translator . It's different from a bridge that simply conveys information , The gateway should repackage the received information , To meet the needs of the target system . Same layer – application layer .
So to sum up :
gateway (Gateway) It is a device that connects two network segments using different protocols .
The gateway is simultaneous interpreting and translating data from two network segments using different transport protocols. .
Gateway Gateway is the gatekeeper of our service , A unified portal for all microservices .
Gateway implementation
stay SpringCloud The implementation of gateway in mainly includes two kinds :gateway and zuul
Zuul Is based on Servlet The implementation of the , It belongs to blocking programming . and SpringCloudGateway Is based on Spring5 Provided in WebFlux, It belongs to the implementation of responsive programming , Better performance .
Gateway Use
Gateway The use steps of the gateway are as follows :
- establish SpringBoot engineering gateway, Introducing gateway dependencies
- Write the startup class
- Write basic configuration and routing rules
- Start the gateway service for testing
First step , Introduce dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos Our services depend on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
The second step , Write the startup class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
The third step , stay ymal Write configurations and rules
server:
port: 10010 # Gateway port
spring:
application:
name: gateway # Name of service
cloud:
nacos:
server-addr: localhost:8848 # nacos The address of
gateway:
routes: # Gateway routing configuration
- id: user-service # route id, Customize , As long as it's the only one
# uri: http://127.0.0.1:8081 # The destination address of the route http It's a fixed address
uri: lb://userservice # The destination address of the route lb It's load balancing , Followed by the service name
predicates: # Route assertion , That is, judge whether the request meets the conditions of routing rules
- Path=/user/** # This is matched according to the path , As long as /user/ Meet the requirements at the beginning
We will conform to Path All requests of the rules , All agents arrive at uri The address specified by the parameter .

Summary of gateway entry
summary :
Gateway building steps :
Create project , introduce nacos Service discovery and gateway rely on
To configure application.yml, Including basic service information 、nacos Address 、 route
Routing configuration includes :
route id: The unique identifier of the route
Routing destination (uri): The destination address of the route ,http Represents a fixed address ,lb Represents load balancing based on service name
Route assertion (predicates): Rules for judging routing ,
Routing filter (filters): To process a request or response
The factory
Spring Cloud Gateway Many routing assertion factories are built in , It can be used directly through configuration , You can also combine multiple routing assertion factories .
for example :
Path Route assertion factory
Path The routing assertion factory receives a parameter , according to Path Well defined rules to determine the access URI match .
```yaml
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://example.test
predicates:
- Path=/blog/detail/{
segment}
Method Route assertion factory
Method The routing assertion factory receives a parameter , That is to match HTTP Method .
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://baidu.com
predicates:
- Method=GET
Cookie Route assertion factory
adopt cookie And a regular expression as a routing factory for assertion conditions , As long as this condition is met, you can access the address .
spring:
cloud:
gateway:
routes:
- id: between_route # route Id, only
uri: https://example.org # The goal is URI, The address routed to the microservice
predicates:
- Cookie=chocolate, ch.p # Use Cookie Route assertion factory , To configure cookie, Regular expressions ( There can be no )
Configuration interpretation : This route will match with a named chocolate Of cookie Request , The cookie The value of matches ch.p Regular expressions
Of course, there are more than a dozen factories , We just need to master Path This routing project is OK .
There are also other factories , Our next article will discuss .

This is the end of the article , If you have any questions, please point out , All the guys discuss together
Finally, I want to give you a group of customers of Amway again , Click on Brush Title artifact
Registered Niuke , Come and brush the questions with the blogger Thank you again for your support

边栏推荐
- 【DesignMode】享元模式(Flyweight Pattern)
- Notification uses full resolution
- 修改配置文件后tidb无法启动
- Prediction - Grey Prediction
- Communication mode between application program and MATLAB
- 删除 console 语句引发的惨案
- You Yuxi, coming!
- Laravel post shows an exception when submitting data
- Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
- SqlServer2014+: 创建表的同时创建索引
猜你喜欢

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)

C语言进阶——函数指针

二叉搜索树(特性篇)
![[vulnhub range] thales:1](/img/fb/721d08697afe9b26c94fede628c4d1.png)
[vulnhub range] thales:1

Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation

What are compiled languages and interpreted languages?

Pycharm terminal enables virtual environment

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

Personal notes of graphics (4)

Opencv personal notes
随机推荐
Laravel post shows an exception when submitting data
作为Android开发程序员,android高级面试
Usage of config in laravel
统计学习方法——感知机
AutoLISP series (2): function function 2
How to determine whether the checkbox in JS is selected
laravel怎么获取到public路径
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
Laravel constructor and middleware execution order
What else can an ordinary person do besides working in a factory to make money?
AutoLISP series (1): function function 1
Detailed explanation of several ideas for implementing timed tasks in PHP
markdown公式编辑教程
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
二叉搜索树(基操篇)
spark调优(三):持久化减少二次查询
Leetcode-136- number that appears only once (solve with XOR)
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
Odoo集成Plausible埋码监控平台
How to query the data of a certain day, a certain month, and a certain year in MySQL


