SpringCloud官方地址 Spring Cloud
源码地址:https://gitee.com/datadogapache/dashboard/projects
一.添加Nacos依赖:
org.springframework.cloud spring-cloud-starter-alibaba-nacos-config0.2.2.RELEASE com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery2.2.1.RELEASE
二.添加GateWay依赖:
org.springframework.cloud spring-cloud-starter-gatewayorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-webflux
注意:坑!坑!坑!
springcloudgateway的底层实现是基于webflux,而webflux和传统的webmvc冲突,所以当项目中存在spring-boot-starter-web依赖时,需排除webmvc,且在spring-cloud-starter-gateway依赖中排除spring-boot-starter-web和spring-boot-starter-webflux,单独引入spring-boot-starter-webflux依赖替代webmvc,pom.xml中完整的依赖如下:
4.0.0 org.springframework.boot spring-boot-starter-parent2.3.3.RELEASE com.example springcloudgateway0.0.1-SNAPSHOT springcloudgateway Demo project for Spring Boot 1.8 Hoxton.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework spring-webmvcorg.springframework.boot spring-boot-starter-tomcatorg.springframework.boot spring-boot-starter-webfluxorg.springframework.cloud spring-cloud-starter-alibaba-nacos-config0.2.2.RELEASE com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery2.2.1.RELEASE org.springframework.cloud spring-cloud-starter-gatewayorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-webfluxorg.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.springframework.cloud spring-cloud-dependencies${spring.cloud.version} pom import org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok
最后也是最重要的一点就是!!!
1.springboot的版本和springcloud的版本对应一定要一致,否者项目将报各种错误。spring官网中有其对应的版本,上面代码中spingboot的版本为2.3.3.RELEASE,springcloud的版本为Hoxton.RELEASE.
2.因为webflux基于netty,所以还需在spring-boot-starter-web中排除tomcat依赖:
org.springframework.boot spring-boot-starter-weborg.springframework spring-webmvcorg.springframework.boot spring-boot-starter-tomcat
三.配置bootstrap.yml
spring:
application:
name: gateway
profiles:
active: dev
cloud:
nacos:
config:
server-addr: 192.168.91.1:8848
file-extension: yml
discovery:
server-addr: 192.168.91.1:8848
gateway:
httpclient:
connect-timeout: 3000
response-timeout: 10s
routes:
- id: test
uri: lb://gateway
predicates:
- Path=/345/123
filters:
- StripPrefix=1
各字段含义如下:
id:我们自定义的路由 ID,保持唯一
uri:目标服务地址,lb为自定义nacos中的服务
predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
filters:过滤器,有多种过滤条件,其中StripPrefix为跳过路径数。例如:访问localhost:8090/test/api/home,当StripPrefix=2时,/test/api被跳过,直接访问localhost:8090/home
上面这段配置的意思是,配置了一个 id 为test的URI代理规则,路由的规则为:
当访问地址http://localhost:80890/345/123时,
会路由到上游地址http://localhost:80890/123。
四.建立controller测试路径
package com.example.springcloudgateway.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class gatecontroller {
@RequestMapping(value = "/123",method = RequestMethod.GET)
public String tsetGate() {
return "welcome to my house!";
}
}
浏览器中输入localhost:8090/345/123
当输入localhost:8090/123时,由于网关没有断言(predicates)设置路由规则为真,即可直接访问
完毕!后期还会更更多关于网关的知识点,特推荐一篇关于SpringCloud gateway的博文:SpringCloud gateway (史上最全) - 疯狂创客圈 - 博客园 (cnblogs.com)
希望多多交流指正!



