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

配置Gateway端口9527

简单的配置使用

pom


<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>cloud2020artifactId>
        <groupId>com.menggroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-gateway-gateway9527artifactId>


    <dependencies>
        
        <dependency>
            <groupId>com.menggroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        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值随意取)

你可能感兴趣的:(gateway,网关,cloud,eureka,spring,boot)