Gateway
In our usual development process, if it is a microservice-related project, we must configure its request path for each microservice, which is very unfavorable to our development process, and it is not friendly enough for front-end personnel.
Gateway provides a solution to the above problems, for each service, we can forward it according to specific routing rules.
For the Gateway service, we can expose a public IP for users to use, and for other services, we can provide a private IP address, and all external requests can be mapped to the corresponding service according to the routing rules through the gateway.
在我们平时的开发过程当中,如果是微服务相关的项目,对于每一个微服务我们都必须配置其的请求路径,这样对于我们的开发过程十分不利,而且对于前端的人员也不够友好,对于这个问题我们思考如何解决:能否配置一个统一的入口,使得按照规定的请求路径能够被正确路由到相应的服务上面?
Gateway提供了一种解决上述问题的方案,对于每一个服务,我们可以按照特定的路由规则对其进行转发
对于Gateway服务我们可以暴露一个公网IP供用户使用,对于其他的服务,我们提供内网IP即可,所有外部的请求经过网关按照路由规则映射到对应的服务。
Router-configurations:
routes:
- id: pms-front
uri: lb://pms-front
predicates:
- Path=/sinopec/front/**
filters:
- StripPrefix=2
This part defined a router rule:
id:
pms-front: config an unique symbol for this router.
uri:
lb://pms-front: lb(load balance) which means we trans allowed request to load-balanced service "pms-front".
predicates :
The routing rule is defined, and the path matching rule is specified here /sinopec/front/**.
filters:
this part defined the filter operation.StripPrefix=2 means we delete two path in the request path.so when we trans request,it delete "/sinopec/front/".
这部分定义了一个路由器规则:
id:pms-front 服务Id
URI:lb://pms-front:lb(负载平衡),这意味着我们允许对负载均衡服务“pms-front”的请求,在未利用nacos进行服务注册时可以填写服务地址
predicates:定义了路由规则,此处指定了路径匹配规则/sinopec/front/,表示通配符
filters:这部分定义了过滤器操作。StripPrefix=2 表示我们删除请求中的两个路径,当我们转发请求时,它会删除“/sinopec/front/”。
The purpose of this configuration is to forward requests that match the /sinopec/front/** path to the load-balanced "pms-front" service, and to strip the path prefix "/sinopec/front/" when forwarding, while allowing cross-origin requests.
此配置的目的是将匹配 /sinopec/front/** 路径的请求转发到负载均衡的 “pms-front” 服务,并在转发时去除路径前缀 /sinopec/front/,同时允许跨域请求。
The configuration of the gateway is a few different from other dependencies, and it is best not to inherit from the parent pom project: Spring Cloud Gateway requires the Netty runtime environment provided by Spring Boot and Spring Webflux, so it cannot be packaged as a war package, nor can it be run in a traditional servlet container (such as tomcat). Therefore, Spring Cloud Gateway projects cannot rely on
网关的配置和其他的依赖配置有一些不一样的地方,它最好不要继承自父pom工程:Spring Cloud Gateway需要Spring Boot和Spring Webflux提供的Netty的运行时环境,因此,它不可以打包成war包,也不可以在传统的Servlet容器(比如tomcat)中运行。所以Spring Cloud Gateway项目中不能依赖
spring-boot-starter-web ,要不然会报错。
对于它的配置有如下的要求:
1.添加 spring-cloud.version 属性:
<spring-cloud.version>2021.0.5</spring-cloud.version> 这样可以统一管理 Spring Cloud 的版本。
2.在 dependencyManagement 部分添加 spring-cloud-dependencies 的管理:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
//这样可以确保所有 Spring Cloud 相关的依赖使用相同的版本。
3.修改 spring-cloud-gateway 依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
使用 spring-cloud-starter-gateway 而不是直接使用 spring-cloud-gateway,确保使用正确的启动器依赖。
这些修改确保了 spring-cloud-gateway 的依赖和 Spring Boot 版本的兼容性。
Cross-Origin Resource Sharing(CORS):In a origin we can not request other origin's resource in the development.when we want the different website origin can interact with each other in the broswer,we can allow cross-origin Resource Sharing.
we also can use Nginx to deal with this problem in most common situation.
In order to config Cross-Origin Resource Sharing,we can set some fields of HTTP
response header so the broswer can allow some special Cross-Origin request.
跨源资源共享(CORS):我们不能在开发中请求其他源的资源,当我们希望不同的网站源在浏览器中可以相互交互时,我们可以允许跨源资源共享。
在最常见的情况下,我们也可以使用 Nginx 来处理这个问题。
为了配置跨域资源共享,我们可以设置一些HTTP字段
响应标头,以便浏览器可以允许一些特殊的跨域请求。
The most common fields includes:
Access-Control-Allow-Origin: which origin can request resource.
Access-Control-Allow-Methods: which method can request resource.(such as post、get)
Access-Control-Allow-Headers: which headers can request resource.
最常见的字段包括:
Access-Control-Allow-Origin:哪个源可以请求资源。
Access-Control-Allow-Methods:可以请求资源的方法。(如 post、get)
Access-Control-Allow-Headers:哪些标头可以请求资源。
globalcors-configurations:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedMethods:
- GET
- POST
- DELETE
- PUT
- OPTION
This part define the globalcors-configurations.It allow all origins can request
resource and we can use GET、POST、DELETE、PUT、OPTION method.
这部分定义了 globalcors-configurations。它允许所有来源可以请求资源,我们可以使用GET、POST、DELETE、PUT、OPTION方法。
本文作者:PostMan_Zc
本文链接:https://www.cnblogs.com/zhangcheng1234/p/17994585
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步