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

边栏推荐
- Set the route and optimize the URL in thinkphp3.2.3
- 预测——灰色预测
- 应用程序和matlab的通信方式
- Communication mode between application program and MATLAB
- The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
- Detailed explanation of several ideas for implementing timed tasks in PHP
- hellogolang
- "The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
- Logback logging framework third-party jar package is available for free
- What about the pointer in neural network C language
猜你喜欢

torch. Numel action

Prediction - Grey Prediction

Record the migration process of a project

Horizontal and vertical centering method and compatibility

【Android -- 数据存储】使用 SQLite 存储数据

Opencv configuration 2019vs

Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench

pycharm 终端部启用虚拟环境

Spark Tuning (III): persistence reduces secondary queries

Enterprise log analysis system elk
随机推荐
[summary of knowledge] summary of notes on using SVN in PHP
Tidb cannot start after modifying the configuration file
HAVE FUN | “飞船计划”活动最新进展
AutoLISP series (2): function function 2
Tragedy caused by deleting the console statement
Find tags in prefab in unity editing mode
asyncio 概念和用法
分类模型评价标准(performance measure)
Usage of config in laravel
prometheus api删除某个指定job的所有数据
php 自带过滤和转义函数
目标跟踪常见训练数据集格式
laravel怎么获取到public路径
【PHP】PHP接口继承及接口多继承原理与实现方法
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
torch.numel作用
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
[Android -- data storage] use SQLite to store data
three. JS create cool snow effect
Cesium(3):ThirdParty/zip. js


