> 文章列表 > GateWay微服务网关的搭建

GateWay微服务网关的搭建

GateWay微服务网关的搭建

服务网关

没有服务网关

问题:地址太多|安全性|管理问题

访问商品服务

http://ip地址:9001/goods/findAll

访问广告服务

http://ip地址:9002/brand/findAll

访问用户服务

http://ip地址:9003/user/findAll

在有网关的情况下,我们配置网关端口号为4006,在访问服务是我们可以通过访问网关进而访问服务,此时端口号访问时都为4006,方便了很多,尤其是前端做跨域配置

访问商品服务

http://ip地址:4006/goods/findAll

访问广告服务

http://ip地址:4006/brand/findAll

访问用户服务

http://ip地址:4006/user/findAll

Spring Cloud Gateway

Spring Cloud Gateway

Spring Cloud Gateway 是 Spring Cloud生态系统中的网关,它是基于Spring 5.0、SpringBoot 2.0和Project Reactor等技术开发的,旨在为微服务架构提供一种简单有效的、统一的API路由管理方式,并为微服务架构提供安全、监控、指标和弹性等功能。其目标是替代Zuul

路由(Route)

是网关的基本构建块。由一个ID一个目标URI一组断言和一组过滤器定义。断言为真,则路由匹配

断言(predicate)

输入类型是一个ServerWebExchange。我们可以使用它来匹配来自HTTP请求的任何内容

过滤(filter)

可以在请求被路由前或者之后对请求进行修改。

搭建网关

在父项目中,创建网关模块,创建的是maven项目

pom.xml依赖配置

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>books</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>books-gateway</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--网关--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!--eureka客户端--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--配置中心客户端--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency><!--默认加载bootstrap--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><target>11</target><source>11</source></configuration></plugin></plugins></build></project>

主启动类

@SpringBootApplication
public class ServiceGateway4006 {public static void main(String[] args) {SpringApplication.run(ServiceGateway4006.class,args);}
}

bootstartp.yml

server:port: 4006
spring:application:name: books-gatewaycloud:gateway:discovery:locator:enabled: false    #不开启服务注册和发现的功能lower-case-service-id: true #请求路径上的服务名称转换为小写#路由配置routes:#管理员访问接口路由- id: adminuri: lb://books-adminpredicates:- Path=/sys/**#书籍访问接口路由- id: booksuri: lb://books-bookspredicates:- Path=/books/**#读者访问接口路由- id: readeruri: lb://books-readerpredicates:- Path=/reader/**#阅览室访问接口路由- id: readRoomuri: lb://books-readRoompredicates:- Path=/readRoom/**#书商访问接口路由- id: booksSelleruri: lb://books-booksSellerpredicates:- Path=/booksSeller/**#规则制度访问接口路由- id: rulesuri: lb://books-rulespredicates:- Path=/rules/**#藏书馆访问接口路由- id: collectionLocationuri: lb://books-collectionLocationpredicates:- Path=/collectionLocation/**globalcors:# 解决options请求被拦截问题add-to-simple-url-handler-mapping: truecors-configurations:# 拦截的请求'[/**]':# 允许跨域的请求#allowedOrigins: "*" # spring boot2.4以前的配置allowedOriginPatterns: "*" # spring boot2.4以后的配置# 允许请求中携带的头信息allowedHeaders: "*"# 运行跨域的请求方式allowedMethods: "*"# 是否允许携带cookieallowCredentials: true# 跨域检测的有效期,单位smaxAge: 36000config:uri: http://localhost:9010label: master#books-dev.ymlname: booksprofile: devlogging:pattern:console: '%d{MM/dd HH:mm:ss.SSS} %clr(%-5level) ---  [%-15thread] %cyan(%-50logger{50}):%msg%n'