springgateway⽹关常⽤的作⽤
spring:
application:
name: sysgateway
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]': # 匹配所有请求
allowedOrigins: "*" #跨域处理允许所有的域
allowedMethods: # ⽀持的⽅法
-
GET
- POST
- PUT
- DELETE
routes:
- id: goods
uri: lb://goods
predicates:
- Path=/goods/**
filters:
- StripPrefix= 1
-
name: RequestRateLimiter #请求数限流名字不能随便写
args:
key-resolver: "#{@ipKeyResolver}"
redis-rate-limiter.burstCapacity: 1
- id: system
uri: lb://system
predicates:
- Path=/system/**
filters:
- StripPrefix= 1
# 配置Redis 127.0.0.1可以省略配置
redis:
host: 127.0.0.1
port: 6379
password: xxx
server:
port: 9101
eureka:
client:
service-url:
defaultZone: 127.0.0.1:6868/eureka
instance:
prefer-ip-address: true
login:
filter:
allowPaths:
- /system/admin/login
以上是yml总体配置⽂件。
1.路由功能
Route(路由):路由是⽹关的基本单元,由ID、URI、⼀组Predicate、⼀组Filter组成,根据Predicate进⾏匹配转发。其中uri后⾯的lb是当多个服务的时候,启动负载均衡的功能
Predicate(谓语、断⾔):路由转发的判断条件,⽬前SpringCloud Gateway⽀持多种⽅式,常见如:Path、Query、Method、Header等。
Filter(过滤器):过滤器是路由转发请求时所经过的过滤逻辑,可⽤于修改请求、响应内容。stripPrefix转发时截取,⽐如请求到⽹关的地址是/goods/brand,⽹关转发到good服务的/brand地址。
2这是微服务⽹关跨域。在controller控制类上加个@CrossOrigin配合使⽤
globalcors:
cors-configurations:
'[/**]': # 匹配所有请求
allowedOrigins: "*" #跨域处理允许所有的域
allowedMethods: # ⽀持的⽅法
- GET
- POST
- PUT
- DELETE
3.⽹关通过过滤器做⽹关鉴权的功能。
login:
filter:
allowPaths:
- /system/admin/login
package fig;
import org.t.properties.ConfigurationProperties;
import java.util.List;
@ConfigurationProperties(prefix = "login.filter")
public class FilterProperties
{
private List<String> allowPaths;
public List<String> getAllowPaths() {
return allowPaths;
}
public void setAllowPaths(List<String> allowPaths) {
this.allowPaths = allowPaths;
}
}
package com.changgou.filter;
import fig.FilterProperties;
import com.changgou.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.t.properties.EnableConfigurationProperties; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.active.ServerHttpRequest;
import org.springframework.active.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
publisher.Mono;
import java.util.List;
@Component
@EnableConfigurationProperties({FilterProperties.class})
public class loginFilter implements GlobalFilter, Ordered {
@Autowired
private FilterProperties properties;
private  final static String TOKEN_NAME = "token";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {        ServerHttpRequest request = Request();
ServerHttpResponse response = Response();
List<String> allowPaths = AllowPaths();
for (String allowPath : allowPaths){
if (URI().getPath().contains(allowPath)){
return chain.filter(exchange);
}
}
HttpHeaders headers = Headers();
String token = First(TOKEN_NAME);
if (StringUtils.isEmpty(token)){
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
try
{
JwtUtil.parseJWT(token);
} catch (Exception e)
{
e.printStackTrace();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 2;
}
}
4,filters底下还可以配置⽹关限流的操作。是通过以上yml配置加上启动类注⼊⼀下类  # 配置Redis 127.0.0.1可以省略配置
redis:
host: 127.0.0.1
port: 6379
password: xxxxxx
routes:
- id: goods
微服务网关和注册中心区别
uri: lb://goods
predicates:
- Path=/goods/**
filters:
- StripPrefix= 1
- name: RequestRateLimiter #请求数限流名字不能随便写
args:
key-resolver: "#{@ipKeyResolver}"
redis-rate-limiter.burstCapacity: 1
@Bean
public KeyResolver ipKeyResolver() {
return new KeyResolver() {
@Override
public Mono<String> resolve(ServerWebExchange exchange) {
return Mono.Request().getRemoteAddress().getHostName());
}
};
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。