当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
asyncio 概念和用法
How can laravel get the public path
Personal notes of graphics (1)
hellogolang
IP地址和物理地址有什么区别
Pycharm terminal enables virtual environment
C语言进阶——函数指针
You Yuxi, coming!
Notification uses full resolution
【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
Leetcode-231-2的幂
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
01tire+链式前向星+dfs+贪心练习题.1
Three. JS series (3): porting shaders in shadertoy
How to implement backspace in shell
Xcode Revoke certificate
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
Find tags in prefab in unity editing mode
Spark Tuning (III): persistence reduces secondary queries
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()