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

边栏推荐
- Usage of config in laravel
- Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
- Laravel changed the session from file saving to database saving
- Xcode Revoke certificate
- MySQL数据库基本操作-DQL-基本查询
- Record the migration process of a project
- Unity3d click events added to 3D objects in the scene
- Logback日志框架第三方jar包 免费获取
- Vs tool word highlight with margin
- The difference and working principle between compiler and interpreter
猜你喜欢

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."

AutoLISP series (3): function function 3

torch. Numel action

全网“追杀”钟薛高

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

spark调优(三):持久化减少二次查询

Statistical learning method -- perceptron

Unity3d click events added to 3D objects in the scene

PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()

Vs2019 configuration matrix library eigen
随机推荐
Three. JS series (1): API structure diagram-1
How to query the data of a certain day, a certain month, and a certain year in MySQL
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
torch. Numel action
How to determine whether the checkbox in JS is selected
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
Cesium(3):ThirdParty/zip. js
Laravel constructor and middleware execution order
二叉搜索树(基操篇)
Find tags in prefab in unity editing mode
Leetcode-136- number that appears only once (solve with XOR)
如何快速检查钢网开口面积比是否符合 IPC7525
Logback日志框架第三方jar包 免费获取
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
Notification uses full resolution
Laravel changed the session from file saving to database saving
AutoLISP series (3): function function 3
Laravel5.1 Routing - routing packets
Tragedy caused by deleting the console statement


