GateWay网关 – 环境搭建v2「建议收藏」

GateWay网关 – 环境搭建v2「建议收藏」目录基础使用Maven依赖application配置Gateway整合Nacos实现服务转发Maven依赖application配置自定义TokenFilter实现参数拦截gateWay高可用集群方式基础使用Maven依赖注意:不能引入spring-cloud-starter-web,会出现错误<parent>…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

 

目录

基础使用

Maven依赖 

application配置

Gateway整合Nacos实现服务转发

Maven依赖

application配置

自定义TokenFilter实现参数拦截

gateWay高可用集群方式


基础使用

Maven依赖 

  注意:不能引入 spring-cloud-starter-web,会出现错误


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
</dependencies>

application配置

server:
  port: 80
  
####服务网关名称
spring:
  application:
    name: mayikt-gateway
  cloud:
    gateway:
        ###路由策略
      routes:
        ###路由id
        - id: mayikt
          ####转发http://www.mayikt.com/
          uri: http://www.mayikt.com/
          ###匹配规则
          predicates:
            - Path=/mayikt/**
             ###浏览器请求127.0.0.1/mayikt 会转发到http://www.mayikt.com/
        - id: cq
          uri: http://127.0.0.1:8060
          ###匹配规则
          predicates:
            - Path=/user/**
            ###浏览器请求127.0.0.1/user/lo 会转发到http://127.0.0.1:8060/user/lo

浏览器输入127.0.0.1/mayikt会转发到http://www.mayikt.com/


Gateway整合Nacos实现服务转发

Maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>0.2.2.RELEASE</version>
    </dependency>
</dependencies>

application配置

server:
  port: 80
####服务网关名称
spring:
  application:
    name: mayikt-gateway
  cloud:
    gateway:
      discovery:
        locator:
          ####开启以服务id去注册中心上获取转发地址
          enabled: true
        ###路由策略
      routes:
        ###路由id
        - id: mayikt
          ####转发http://www.mayikt.com/
          uri: http://www.mayikt.com/
          ###匹配规则
          predicates:
            - Path=/mayikt/**
        ###路由id
        - id: member
          #### 基于lb负载均衡形式转发,mayikt-member为nacos中的会员服务名称
          uri: lb://mayikt-member
          ###是否带请求前缀
          filters:
            - StripPrefix=1
          ###匹配规则
          predicates:
            - Path=/member/**
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    ### nacos注册中心地址

自定义TokenFilter实现参数拦截

案例:拦截所求请求,如果请求参数不包含token则返回500响应码

/**
 * gateway拦截器
 */
@Component
public class TokenFilter implements GlobalFilter {
    
    // 拦截所求请求,如果请求参数不包含token则返回500响应码
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if (token == null || token.isEmpty()) {
            // 封装响应内容
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.BAD_REQUEST);
            String msg = "token not is null "; 
            DataBuffer buffer = response.bufferFactory().wrap(msg.getBytes());
            return response.writeWith(Mono.just(buffer));
        }
        // 使用网关过滤
        return chain.filter(exchange); // 放行
    }

}

gateWay高可用集群方式:

https://mp.csdn.net/postedit/104088145

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194163.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号