GateWay的简介和简单使用

简介

# 0.原文翻译- https://spring.io/projects/spring-cloud-gateway- 
    这个项目提供了一个在springmvc之上构建API网关的库。
    springcloudgateway旨在提供一种简单而有效的方法来路由到api,并为api提供横切关注点,比如:安全性、监控/度量和弹性。

​# 1.特性- 基于springboot2.x 和 spring webFlux 和 Reactor 构建 响应式异步非阻塞IO模型- 动态路由- 请求过滤

1.开发网关动态路由

网关配置有两种方式一种是快捷方式(Java代码编写网关),一种是完全展开方式(配置文件方式)[推荐]

1.引入依赖
<!--引入gateway网关依赖-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2.编写网关配置
server:
  port: 8088

spring:
  application:
    name: GATEWAY
  cloud:
    consul:
      host: localhost
      port: 8500
    gateway:
      routes:
        - id: category_route	# 指定路由唯一标识
          uri: http://localhost:8085 # 指定路由服务的地址
          predicates:
            - Path=/category	# 指定路由规则

        - id: product_route
          uri: http://localhost:8084
          predicates:
            - Path=/product

3.启动gateway网关项目
直接爆出如下错误:
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration 
required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.
在启动日志中发现,gateway为了效率使用webflux进行异步非阻塞模型的实现,因此和原来的web包冲突,去掉原来的web即可

4.测试路径即可
就会发现网关会将不同的访问路径转发到不同的微服务API中
posted @ 2021-10-01 20:56  code-G  阅读(307)  评论(0编辑  收藏  举报