结构图
网关:就是所有微服务的入口点
为什么使用网关
路由转发
认证校验
跨域统一
黑白名单
常用的网关组件
概述gateway
使用gateway
路由转发
创建网关微服务
创建网关模块
添加gateway依赖<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies>
前提:不能引入spring-boot-starter-web依赖,否则回报错,因为web内置了tomcat服务器,而gateway内置了netty服务器
配置文件
server: port: 89 spring: application: name: gateway #配置路由转发 cloud: gateway: routes: - id: product #路由id,没有实际意义。如果不定义UUID随机生成 uri: http://localhost:8002 # 表示路由真实转发的微服务的地址 predicates: #断言: 如果断言满足要求则转发到对应uri地址>===http://localhost:80/product/getById/1==>http://localhost:8001/product/getByIid/1 - Path=/product/** - id: order uri: http://localhost:9002 predicates: - Path=/order/**
访问
增强版
添加nacos依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
修改配置文件
server: port: 89 spring: application: name: gateway #配置路由转发 cloud: gateway: routes: - id: product #路由id,没有实际意义。如果不定义UUID随机生成 #uri: http://localhost:8002 # 表示路由真实转发的微服务的地址 uri: lb://product predicates: #断言: 如果断言满足要求则转发到对应uri地址>===http://localhost:80/product/getById/1==>http://localhost:8001/product/getByIid/1 - Path=/product/** - id: order #uri: http://localhost:9002 uri: lb://order predicates: - Path=/order/** #nacos的配置 nacos: discovery: server-addr: localhost:8848 register-enabled: false #是否注册到nacos上
访问和上面一样
简洁版
gateway开启定位功能
修改配置文件server: port: 89 spring: application: name: gateway #nacos的配置 nacos: discovery: server-addr: localhost:8848 cloud: gateway: discovery: locator: enabled: true #开启gateway的定位功能
在访问资源是要添加微服务的名称
认证授权
添加依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency>
配置文件
server: port: 89 spring: application: name: gateway #nacos的配置 nacos: discovery: server-addr: localhost:8848 cloud: gateway: discovery: locator: enabled: true #开启gateway的定位功能 #白名单 url: white: - /login - /register - /sendMsg
vo
@Component//交给容器和管理 @ConfigurationProperties(prefix = "url") public class UrlVo { private List<String> white; public List<String> getWhite() { return white; } public void setWhite(List<String> white) { this.white = white; } }
创建网关过滤器接口
//网关过滤器只能在网关中使用 @Component public class LoginFilter implements GlobalFilter, Ordered { @Autowired private UrlVo urlVo; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); // 获取请求路径 String path= request.getPath().toString(); path = path.substring(1); path = path.substring(path.indexOf("/")); // 判断该路径是否属于放行路径--白名单 if (urlVo.getWhite().contains(path)){ // 放行 return chain.filter(exchange); } // 判断用户是否登录 String token = request.getHeaders().getFirst("token"); // 校验token是否为空以及合法 if (StringUtils.hasText(token)&&"admin".equals(token)){ // 放行 return chain.filter(exchange); } // 封装返回数据 Map<String, Object> map = new HashMap<>(); map.put("msg","未登录"); map.put("code",501); // json转换 byte[] bytes = JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8); // 调用bufferFactory方法,生成DataBuffer对象 DataBuffer buffer = response.bufferFactory().wrap(bytes); // 调用mono中的just方法,返回要写给前端的json数据 return response.writeWith(Mono.just(buffer)); } // 值越小优先级越高 @Override public int getOrder() { return 0; } }
跨域
第一种:配置类
第二种:配置文件