GateWay网关 - 环境搭建v2

 

目录

基础使用

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/
           

浏览器输入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响应码

@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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章