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

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 company

If 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

 Insert picture description here
 Insert picture description here
 Insert picture description here

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 .
 Insert picture description here

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 :

  1. establish SpringBoot engineering gateway, Introducing gateway dependencies
  2. Write the startup class
  3. Write basic configuration and routing rules
  4. 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 .

 Insert picture description here

Summary of gateway entry

summary :

Gateway building steps :

  1. Create project , introduce nacos Service discovery and gateway rely on

  2. To configure application.yml, Including basic service information 、nacos Address 、 route

Routing configuration includes :

  1. route id: The unique identifier of the route

  2. Routing destination (uri): The destination address of the route ,http Represents a fixed address ,lb Represents load balancing based on service name

  3. Route assertion (predicates): Rules for judging routing ,

  4. 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 .

 Insert picture description here

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

 Insert picture description here

原网站

版权声明
本文为[Xiao Wei wants to learn from Zhu Lao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071417458740.html