• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Gateway网关转发demo

武飞扬头像
jmysql
帮助1

1.引入依赖包

  1.  
    <dependencies>
  2.  
    <dependency>
  3.  
    <groupId>org.springframework.cloud</groupId>
  4.  
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  5.  
    <version>0.2.2.RELEASE</version>
  6.  
    </dependency>
  7.  
    <dependency>
  8.  
    <groupId>org.springframework.cloud</groupId>
  9.  
    <artifactId>spring-cloud-starter-gateway</artifactId>
  10.  
    <version>2.0.1.RELEASE</version>
  11.  
    </dependency>
  12.  
    </dependencies>

不要导入web包,要不然会报下列错误:
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.
原因就是
因为SpringCloud gateway基于webflux实现的,不是基于SpringBoot-web,所以应该删除Springboot-web依赖组件。

2.bootstrap.yml新增配置

  1.  
    server:
  2.  
    ###测试的时候,本地端口最好不要用80,容易出错
  3.  
    port: 9999
  4.  
    spring:
  5.  
    application:
  6.  
    name: test-gateway
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    ####开启以服务id去注册中心上获取转发地址
  11.  
    enabled: true
  12.  
    #注册发现使用nginx地址
  13.  
    server-addr: 10.211.55.16:9999
  14.  
    gateway:
  15.  
    routes:
  16.  
    ###静态转发路由策略
  17.  
    - id: orderss
  18.  
    uri: http://www.百度.com/
  19.  
    ###匹配规则
  20.  
    predicates:
  21.  
    - Path=/百度/**
  22.  
    ###基于注册中心的路由策略
  23.  
    - id: member
  24.  
    #### 基于lb负载均衡形式转发,后面地址为注册中心的服务名称
  25.  
    uri: lb://member-service
  26.  
    filters:
  27.  
    #转发请求时去掉1级前缀,eg:http://localhost:9999/member/test?token=111中间的member字段会被清掉
  28.  
    - StripPrefix=1
  29.  
    ###匹配规则
  30.  
    predicates:
  31.  
    - Path=/member/**
学新通

3.核心代码

启动下列就可以测试转发情况

  1.  
    @SpringBootApplication
  2.  
    public class GateWayApp {
  3.  
    public static void main(String[] args) {
  4.  
    SpringApplication.run(GateWayApp.class);
  5.  
    }
  6.  
    }
  7.  
     
  8.  
    /**
  9.  
    * 读取头部信息,判断令牌是否存在,需要加入到容器中
  10.  
    */
  11.  
    @Component
  12.  
    public class TokenFilter implements GlobalFilter {
  13.  
    @Override
  14.  
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  15.  
    String token = exchange.getRequest().getQueryParams().getFirst("token");
  16.  
    if (token == null || token.isEmpty()) {
  17.  
    ServerHttpResponse response = exchange.getResponse();
  18.  
    response.setStatusCode(HttpStatus.BAD_REQUEST);
  19.  
    String msg = "token not is null ";
  20.  
    DataBuffer buffer = response.bufferFactory().wrap(msg.getBytes());
  21.  
    return response.writeWith(Mono.just(buffer));
  22.  
    }
  23.  
    return chain.filter(exchange);
  24.  
    }
学新通



IDEA一键激活,两分钟搞定,白嫖真香_哔哩哔哩_bilibili

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbkgg
系列文章
更多 icon
同类精品
更多 icon
继续加载