亲宝软件园·资讯

展开

Springcloud GateWay网关配置过程图解

梦泽千秋 人气:0

一般为了不暴露自己的端口信息等,会选择架构一个网关在前面进行阻挡,起到保护的作用。附上一张工作示列图。

1.配置网关9527

gateway作为网关需要和其他的应用一样需要注册进eureka中进行管理,先创建应用gateway9527

pom文件,关键是gateway依赖

<dependencies>
    <dependency>
      <groupId>com.bai</groupId>
      <artifactId>cloud-api-common</artifactId>
      <version>${project.version}</version>
    </dependency>
    <!--gateway-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <!--eureka client-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--热部署-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <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>
  </dependencies>

yml文件配置

server:
 port: 9527

spring:
 application:
  name: cloud-gateway
 cloud:
  gateway:
   discovery:
    locator:
     enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名称进行路由
   routes:
    - id: payment_route # 路由的id,没有规定规则但要求唯一,建议配合服务名
     #匹配后提供服务的路由地址
     #uri: http://localhost:8001
     uri: lb://cloud-payment-service
     predicates:
      - Path=/payment/get/** # 断言,路径相匹配的进行路由

    - id: payment_route2
     #uri: http://localhost:8001
     uri: lb://cloud-payment-service
     predicates:
      - Path=/payment/sp/** #断言,路径相匹配的进行路由
eureka:
 instance:
  hostname: cloud-gateway-service
 client:
  fetch-registry: true
  register-with-eureka: true
  service-url:
   defaultZone: http://eureka7001.com:7001/eureka/

主启动类正常配置

@SpringBootApplication
@EnableEurekaClient
public class GateWay9527 {
  public static void main(String[] args) {
    SpringApplication.run(GateWay9527.class,args);
  }
}

2.配置路由两种方式

1)yml文件格式(推荐)

spring:
 application:
  name: cloud-gateway
 cloud:
  gateway:
   discovery:
    locator:
     enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名称进行路由
   routes:
    - id: payment_route # 路由的id,没有规定规则但要求唯一,建议配合服务名
     #匹配后提供服务的路由地址
     #uri: http://localhost:8001
     uri: lb://cloud-payment-service
     predicates:
      - Path=/payment/get/** # 断言,路径相匹配的进行路由

    - id: payment_route2
     #uri: http://localhost:8001
     uri: lb://cloud-payment-service
     predicates:
      - Path=/payment/sp/** #断言,路径相匹配的进行路由
      - After=2020-11-09T11:09:52.794+08:00[Asia/Shanghai]
      - Cookie=username,bai  #带Cookie,并且username的值为bai
#      - Header=X-Request-Id,\d+ #请求头要有 X-Request-Id属性并且值为整数的正则表达式

2)通过config配置类实现

@Configuration
public class RouteConfig {
  @Bean
  public RouteLocator customerRoute(RouteLocatorBuilder builder){
    RouteLocatorBuilder.Builder routes = builder.routes();
     routes.route("payment_route1",
         r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
    return routes.build();
  }
  @Bean
  public RouteLocator bokeRoute(RouteLocatorBuilder builder){
    RouteLocatorBuilder.Builder routes = builder.routes();
    routes.route("payment_route2",r->r.path("/boke").uri("http://www.cnblogs.com/lin530/")).build();
    return routes.build();
  }
}

3.断言(Predicate)

相当于多种限制,只有通过验证才能够访问到对应的方法

- id: payment_route2
     #uri: http://localhost:8001
     uri: lb://cloud-payment-service
     predicates:
      - Path=/payment/sp/** #断言,路径相匹配的进行路由
      - After=2020-11-09T11:09:52.794+08:00[Asia/Shanghai]
      - Cookie=username,bai  #带Cookie,并且username的值为bai
#      - Header=X-Request-Id,\d+ #请求头要有 X-Request-Id属性并且值为整数的正则表达式

不同的方法可以配置不同的断言,此处的断言需要满足路径对,after时间后,cookie带上正确的用户名才可以访问到,用cmd测试

其他还有很多断言,请对照文档自行研究

4)过滤(filter)

对访问加上限制,通过验证才可以访问

新建过滤配置类

@Component
@Slf4j
public class GatewayGlobalFilter implements GlobalFilter, Ordered {
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    log.info("***time is :"+new Date());
    String uname = exchange.getRequest().getQueryParams().getFirst("uname");
    if(uname==null){

      log.info("没有用户名,出现错误:o(╥﹏╥)o");
      exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
      return exchange.getResponse().setComplete();
    }
    return chain.filter(exchange);
  }

  /*
  设置过滤器优先级,越小优先级越大
   */
  @Override
  public int getOrder() {
    return 0;
  }
}

访问方式带上参数,

本篇所有代码均在GitHub:

https://github.com/MaTsukun/springcloud2020

加载全部内容

相关教程
猜你喜欢
用户评论