当前位置:网站首页>Gateway routing gateway
Gateway routing gateway
2022-07-30 03:32:00 【Leon_Jinhai_Sun】
Official website address: SpringCloud Gateway
Speaking of routing, the first thing you must think of is the router at home, so what role does the router in our home play?
We know that if we need to connect to the Internet, then we need to connect the mobile phone or computer to the router at home, and the router is connected to the optical modem, and the optical modem is then connected to the Internet through optical fiber, alsoThat is to say, the data sent from the Internet direction needs to go through the router to reach our device.The router acts as a data packet transfer station. All local area network devices cannot be directly connected to the Internet, but need to be transferred through the router. We generally say that the network under the router is the internal network, and the Internet end is the external network.

Our local area network devices cannot be directly accessed by other devices on the Internet, which can definitely guarantee security.And the data sent from the Internet needs to be parsed by the router to identify which device is the data packet, and then send it to the corresponding device.
The same is true for our microservices. In general, not all microservices may need to be directly exposed to external calls. At this time, we can use the routing mechanism to add a layer of protection to allowAll requests are forwarded to each microservice through routing, and forwarding to multiple instances of the same microservice can also achieve load balancing.

In the past, the implementation of routing generally used Zuul, but it has been stopped, and now there is a new Gateway route officially developed by SpringCloud, which not only has a certain performance improvement compared to Zuul, but alsoIt is officially launched, and the fit will be better, so we will mainly explain Gateway here.
Deploy Gateway
Now let's create a new project as our gateway, here we need to add two dependencies:
org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-netflix-eureka-client The first dependency is the gateway dependency, and the second one, like other microservices, needs to be registered with Eureka to take effect. Be careful not to add web dependencies and use the WebFlux framework.
Then let's improve the configuration file:
server:port: 8500eureka:client:service-url:defaultZone: http://localhost:8801/eureka, http://localhost:8802/eurekaspring:application:name: gatewayYou can now start:

But no routing function has been configured yet, we will configure the routing function:
spring:cloud:gateway:# Configure routing, note that this is a list, each item contains a lot of informationroutes:- id: borrow-service # route nameuri: lb://borrowservice # The address of the route, lb means to use load balancing to microservices, you can also use http to forward normallypredicates: # Routing rules, assert what requests will be routed- Path=/borrow/** # As long as this path is accessed, it will be routed to the service specified aboveA detailed list of routing rules (list of assertion factories) is here: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories, you can specify multiple types, including the specified time period, Cookie carrying status, Header carrying status, domain name address to access, access method, path, parameters, visitor IP, etc.Configuration classes can also be used for configuration, but direct configuration files are recommended to save trouble.
Then start the gateway, Mac computers with Arm architecture chips may encounter this problem:

This is because there is no dynamic link library for this architecture, it does not affect the use, just ignore it, I hope the future version can fix it.
As you can see, we can now access our service directly through the route:

Note that you can still access through the original service address at this time:

In this way, we can put all the microservices that do not need direct access from the external network to the internal network environment, and only rely on the gateway to negotiate with the outside world.
边栏推荐
猜你喜欢

SQL Server中如何在date类型中提取年、月、日数据

测试人员,除了测试还得会点什么

(RCE)远程代码/命令执行漏洞漏洞练习

SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)

状态空间表示

【科研工具的使用】A

Ansible introduction (detailed) features + advantages + design concept + application field + system architecture + working principle + task execution process

leetcode每天5题-Day01

【C补充】整数与字符串的转换

开放地址法哈希实现——线性探测法
随机推荐
联邦学习综述(二)——联邦学习的分类、框架及未来研究方向
护网行动基本介绍
YOLOv7的一些理解
QT based on the third day (3) widget, dialog and mainwindow
【Node访问MongoDB数据库】
Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!
解决谷歌浏览器跨域问题has been blocked by CORS policy: The request client is not a secure context and the resou
【GPU并行计算】利用OpenCL&OpenCLUtilty进行GPU并行计算
HCIP experiment (05) OSPF comprehensive experiment
还在用命令行看日志?快用Kibana吧,可视化日志分析YYDS
JUC (7): Thread Safety Analysis of Variables
sqlmap使用教程大全命令大全(图文)
OpenFeign实现负载均衡
un7.29: How to install and configure redis in Linux-centos?
Redis(十) - Redission原理与实践
微服务进阶 Cloud Alibaba
The relationship between the number of Oracle processes and the number of sessions
淘宝/天猫获取卖出的商品订单列表 API
状态空间表示
雪花是否一样问题