Springcloud Alibaba Nacos整合GateWay基础使用

Springcloud Alibaba Nacos整合GateWay基础使用

  • 文章目录
    • GateWay简述
    • 版本依赖
    • 项目搭建
    • 备注

文章目录

GateWay简述

SpringCloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,

版本依赖

Springboot:v2.4.12
SpringCloud GateWay: v3.0.4
com.alibaba.cloud: v2021.1

具体依赖

        <!-- actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${com.alibaba.cloud}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${com.alibaba.cloud}</version>
        </dependency>

上面${com.alibaba.cloud}代表2021.1

  • 注意事项
    由于springcloud2020弃用了Ribbon,因此Alibaba在2021版本nacos中删除了Ribbon的jar包,因此无法通过lb路由到指定微服务,出现了503情况。所以需要添加
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>3.0.4</version>
        </dependency>

版本与GateWay一致

版本问题参考

Springcloud Alibaba Nacos整合GateWay基础使用_第1张图片
网址:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

项目搭建

application.yaml

server:
  port: 8080


spring:
  application:
    name: gateway
#  profiles:
#    active: dev
  cloud:
    nacos:
      discovery:
        enabled: true
        server-addr: 127.0.0.1:8848
        group: DEFAULT_GROUP
        namespace: gateway-service
        username: nacos
        password: nacos
        ip: 127.0.0.1
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        refresh-enabled: true
    gateway:
      routes:
        - id: my-timeBetween
          uri: lb://gateway
          predicates:
            - Path=/nacos/**
            - Time=上午7:00,下午11:00
          filters:
            - StripPrefix=1
      discovery:
        locator:
          #开启gateway从nacos上获取服务列表
          enabled: true
#开启acutor端点
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      #开发端点详情
      show-details: always

新建配置类

TimeConfig.java

import lombok.Data;

import java.time.LocalTime;

@Data
public class TimeConfig {
    private LocalTime startTime;
    private LocalTime  endTime;
}

TimeRoutePredicateFactory.java

import com.alibaba.nacos.shaded.org.checkerframework.checker.nullness.qual.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
@Component
@Slf4j
public class TimeRoutePredicateFactory extends AbstractRoutePredicateFactory<TimeConfig> {

    public TimeRoutePredicateFactory() {
        super(TimeConfig.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(TimeConfig config) {
        LocalTime  startTime=config.getStartTime();
        LocalTime  endTime=config.getEndTime();
        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(@Nullable ServerWebExchange serverWebExchange) {
              LocalTime now=LocalTime.now();
              //判断当前时间是否在在配置的时间范围类
                return now.isAfter(startTime)&& now.isBefore(endTime);
            }
        };
    }


    public List<String> shortcutFieldOrder() {
        return Arrays.asList("startTime","endTime");
    }
}

测试api
TestController.java

@RestController
@Slf4j
public class TestController {
    @GetMapping("/test/{name}")
    public  String  test(@PathVariable String  name){
        log.info("请求。。。");
        return "hello "+name;
    }
}

启动项目

查看nacos

Springcloud Alibaba Nacos整合GateWay基础使用_第2张图片

查看actuator端点,status是否为‘UP’

在这里插入图片描述
访问路由
在这里插入图片描述

  • 注意事项

  • 如果spring.cloud.nacos.namespace填写了,nacos需要新建命名空间,不然nacos不会显示服务。
    Springcloud Alibaba Nacos整合GateWay基础使用_第3张图片

  • application.name 的名称必须相同,nacos注册服务是根据name来注册的,名称相同会注册进同一个服务

备注

项目按照单机搭建

你可能感兴趣的:(Java,SpringBoot,spring,cloud,gateway,spring,boot)