Gateway网关配置--SpringCloud学习笔记

配置Gateway端口9527

简单的配置使用

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.meng</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-gateway-gateway9527</artifactId>


    <dependencies>
        <!--自定义api-->
        <dependency>
            <groupId>com.meng</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!--devtool-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>
</project>

注意:在网关的端口中,pom不用写web依赖

yml

server:
  port: 9527

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_routh
          uri: http://localhost:8001
          predicates:
            - Path=/payment/getPaymentById/**


eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
  instance:
    instance-id: gateway9527 #修改主机名
    prefer-ip-address: true #访问路径可以显示IP地址
    hostname: cloud-gateway-service

主启动类

package com.meng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author Administrator
 */
@SpringBootApplication
@EnableEurekaClient
public class GatewayMain9527 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(GatewayMain9527.class,args);
    }
}

在上述配置中,访问9527端口的http://localhost:9527/payment/getPaymentById/1即可跳转到http://localhost:8001/payment/getPaymentById/1,上述配置是在Eureka基础上进行的。

配置类的使用

另一种方法,以外网http://news.baidu.com/guonei为例,添加配置类

package com.meng.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**以http://news.baidu.com/guonei为例
 * @author Administrator
 */
@Configuration
public class GatewayConfig {
    
    

    /**
     * 当访问/guonei时会跳转到http://news.baidu.com/guonei
     * @param routeLocatorBuilder
     * @return
     */
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
    
    
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_route_atguigu",r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
        return routes.build();
    }
}

当访问http://localhost:9527/guonei时,会跳转访问到http://news.baidu.com/guonei

但是这个配置类比较复杂,一般建议像前面的写法一样在配置文件yml中直接配置。

配置动态路由

动态路由的配置,是通过微服务名来实现的。

下面示例中,微服务cloud-payment-service对应8001和8002两个端口。

修改yml配置

server:
  port: 9527

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_routh
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/getPaymentById/**
      discovery:
        locator:
          enabled: true #开启动态创建路由功能,利用微服务名

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
  instance:
    instance-id: gateway9527 #修改主机名
    prefer-ip-address: true #访问路径可以显示IP地址
    hostname: cloud-gateway-service

这样,在访问http://localhost:9527/payment/getPaymentById/1时会在服务端8001和8002之间来回跳到,实现动态路由访问。

添加过滤器Filter

编写过滤器

package com.meng.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * @author Administrator
 */
@Component
@Slf4j
public class MyLogGatewayFilter implements GlobalFilter, Ordered {
    
    
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    
    
        log.info("****MyLogGatewayFilter:"+new Date());
        String usename = exchange.getRequest().getQueryParams().getFirst("usename");
        if (usename==null){
    
    
            log.info("****非法用户!");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
    
    
        return 0;
    }
}

添加上述过滤器之后如果直接访问http://localhost:9527/payment/getPaymentById/2没有结果,需要访问http://localhost:9527/payment/getPaymentById/2?usename=111才行(usename值随意取)

猜你喜欢

转载自blog.csdn.net/liuliusix/article/details/108979898