这里使用IDEA的Spring Initializr进行项目的创建,到选择依赖这一步勾选gateway依赖,如下图:
网关组件一般都配合服务发现组件使用,我这里使用Nacos作为服务发现组件,具体的依赖如下:
dependencies dependency groupId org.springframework.cloud /groupId artifactId spring-cloud-starter-gateway /artifactId /dependency !-- Nacos Client -- dependency groupId com.alibaba.cloud /groupId artifactId spring-cloud-starter-alibaba-nacos-discovery /artifactId /dependency !-- actuator -- dependency groupId org.springframework.boot /groupId artifactId spring-boot-starter-actuator /artifactId /dependency dependency groupId org.springframework.boot /groupId artifactId spring-boot-starter-test /artifactId scope test /scope /dependency /dependencies dependencyManagement dependencies !--整合Spring Cloud-- dependency groupId org.springframework.cloud /groupId artifactId spring-cloud-dependencies /artifactId version Greenwich.SR2 /version type pom /type scope import /scope /dependency !--整合Spring Cloud Alibaba-- dependency groupId com.alibaba.cloud /groupId artifactId spring-cloud-alibaba-dependencies /artifactId version 2.1.0.RELEASE /version type pom /type scope import /scope /dependency /dependencies /dependencyManagement
如果对Nacos不熟悉的话可以参考另一篇关于Nacos的文章,或者采用Eureka也是一样的:
Spring Cloud Alibaba之服务发现组件 - Nacos然后编写配置文件内容如下:
server: port: 8040spring: application: name: gateway cloud: nacos: discovery: # 指定nacos server的地址 server-addr: 127.0.0.1:8848 gateway: discovery: locator: # 让gateway通过服务发现组件找到其他的微服务,从而自动转发请求 enabled: true# actuator相关配置management: endpoints: web: exposure: # 暴露所有监控端点 include: \'*\' endpoint: health: # 总是显示健康检测详情 show-details: always
完成以上步骤后,我们来启动这个网关服务,进行一个简单的测试,看看是否能将请求正常地转发到指定的微服务上。此时有一个名为user-center的微服务,该微服务有一个按id获取用户信息的接口,接口路径为/users/{id}。若通过网关服务来访问这个接口,要如何做呢?很简单,gateway配合服务发现组件使用时,会有一个默认的转发规则,如下:
${GATEWAY_URL}/{微服务名称}/{接口路径}所以按该规则得出来的具体url为:localhost:8040/user-center/users/{id},访问结果如下:
从测试结果可以看到,gateway可以根据url上的微服务名称将访问请求转发到该微服务上。
以上这种是Gateway最简单的使用方式,但通常在实际开发中,可能不希望使用默认的转发规则,因为这种方式不太灵活,例如一些服务接口是存在版本划分的,需要根据不同版本的访问路径转发到不同版本的微服务上。此时就需要自定义转发路由,实际上在第一小节的时候就已经给出过配置示例了。修改配置如下:
spring: cloud: gateway: routes: - id: user-center # 唯一标识,通常使用服务id uri: lb://user-center # 目标URL,lb代表从注册中心获取服务 predicates: # Predicate集合 - Path=/zj/cloud/v1/user-center/** # 匹配转发路径 filters: # Filter集合 - StripPrefix=4 # 从第几级开始转发,数字从0开始
自定义路由的注意事项:
predicates配置项必须有,且必须配置一个及以上的Predicate,但不一定非要配置Path,可以配置其他的Predicate,例如After、Before等,此时Path的默认值为/**重启项目,此时访问的url为:localhost:8040/zj/cloud/v1/user-center/users/{id},访问结果如下:
Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上一小节的示例中我们就已经使用过路由到微服务的这种配置形式了。在这两种形式中,均支持访问路径的通配及精确匹配,在之前的示例中我们只使用了通配。所以本小节将给出具体的配置示例,以此直观的了解这两种形式及不同匹配方式在配置上的区别。
1、路由到指定的URL通配,使用通配符/**进行匹配,示例:
spring: cloud: gateway: routes: - id: test_route # 路由的唯一标识 uri: http://www.xxx.com predicates: # 使用通配符匹配 - Path=/**该配置使访问 GATEWAY_URL/** 时会转发到 http://www.xxx.com/**
精确匹配,配置具体的接口路径即可,示例:
spring: cloud: gateway: routes: - id: test_route # 路由的唯一标识 uri: http://www.xxx.com/user/order/detail predicates: # 指定具体的路径进行匹配 - Path=/user/order/detail该配置使访问 GATEWAY_URL/user/order/detail 时会转发到 http://www.xxx.com/user/order/detail2、路由到指定的微服务
通配,示例:
spring: cloud: gateway: routes: - id: user-center # 路由的唯一标识,这种形式下通常是微服务名称 uri: lb://user-center # lb代表从注册中心获取服务 predicates: # 使用通配符匹配 - Path=/**该配置使访问 GATEWAY_URL/** 时会转发到 user-center微服务的/**
精确匹配,示例:
spring: cloud: gateway: routes: - id: user-center # 路由的唯一标识,这种形式下通常是微服务名称 uri: lb://user-center/users/info # lb代表从注册中心获取服务 predicates: # 指定具体的路径进行匹配 - Path=/users/info该配置使访问 GATEWAY_URL/users/info 时会转发到 user-center微服务的/users/info
本文链接: http://gateway.immuno-online.com/view-713280.html