- 文章目录
- 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
具体依赖
org.springframework.boot spring-boot-starter-actuatororg.springframework.cloud spring-cloud-starter-gateway3.0.4 org.springframework.cloud spring-cloud-starter-loadbalancer3.0.4 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery${com.alibaba.cloud} com.alibaba.nacos nacos-client2.0.3 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config${com.alibaba.cloud}
上面${com.alibaba.cloud}代表2021.1
- 注意事项
由于springcloud2020弃用了Ribbon,因此Alibaba在2021版本nacos中删除了Ribbon的jar包,因此无法通过lb路由到指定微服务,出现了503情况。所以需要添加
org.springframework.cloud spring-cloud-starter-loadbalancer3.0.4
版本与GateWay一致
版本问题参考
网址: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{ public TimeRoutePredicateFactory() { super(TimeConfig.class); } @Override public Predicate apply(TimeConfig config) { LocalTime startTime=config.getStartTime(); LocalTime endTime=config.getEndTime(); return new Predicate () { @Override public boolean test(@Nullable ServerWebExchange serverWebExchange) { LocalTime now=LocalTime.now(); //判断当前时间是否在在配置的时间范围类 return now.isAfter(startTime)&& now.isBefore(endTime); } }; } public List 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
查看actuator端点,status是否为‘UP’
访问路由
-
注意事项
-
如果spring.cloud.nacos.namespace填写了,nacos需要新建命名空间,不然nacos不会显示服务。
-
application.name 的名称必须相同,nacos注册服务是根据name来注册的,名称相同会注册进同一个服务
项目按照单机搭建
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)