1.网关配置类增加路由过滤器配置
route:
- id:sign
uri:lb:
order:66
metadata:
fallbackBean: defaultFallbackHeadler
predicates:
-Path:/sign
2.配置熔断
可忽略此配置:可自己封装接口
@Component("defaultFallbackHeadler")
public DefaultFallbackHeadler implements IFallbackHandler{
private static final Logger logger = LoggerFactory.getLOgger(DefaultFallbackHeadler.class);
@Override
public Object handle(ServerWebExchange exchange,Throwable throwable){
loggin.info(------此请求出发熔断机制-------);
JSONObject respInfo = new JSONObject();
respInfo.put("errorMsg","触发熔断");
respInfo.put("errorCode","1234");
return respInfo ;
}
}
3.对chnlnoMac 设置入参请求过滤器
@Component public calss ChnlnoMacGatewayFIlterFactory extends AbstractGatewayFilterFactory{
private static final Logger logger = LoggerFactory.getLOgger(ChnlnoMacGatewayFIlterFactory .class);
private final ChnalMacFilter filter = new ChnalMacFilter();
@Override
public GatewayFIlter apply(Object ovject){
return filter;
}
private class ChnalMacFilter implements GatewayFilter,Orderd{
@Override
public int getOder(){
//定义过滤器顺序,越小越先执行
return -4;
}
//获取入参
private String getStringBody(Flux<DataBUffer> body){
AtomicReference<String> ato = new AtomicReference<>();
body.subscribe(buffer ->{
CharBuffer charbuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
DataBUfferUtils.release(buffer);
ato.set(chaebuffer.toString());
}
);
return ato.get();
}
//过滤器
@Override
public Meno<Void> filter(ServerWebExchange exchange ,GatewayFilterChain chain){
try{
//从上下文对象中获取请求
ServerHttpRequest request = exchange.getRequest();
HttpHeaders headers = request.getHeaders();
//获取请求体
String reqBody = getStringBody(request.getBody());
logger.info("请求入参:"+reqBody);
JSONObject bodyObj =null;
if(reqBody != null ){
bodyObj = JSONObject.formObject(JSON.parse(reqBody));
}else{
bodyObj = new JSONObject();
}
//获取请求路径的最后一位,例如ip:端口/a/b,获取b
String rawPath request.getURI().getRawPath();
String b = rawPath.sustring(rawPath.lastIndexOf('/')+1);
logger.info("b:"+b)
//http头或者报文体获取参数
if(headers.coontainsKey(c)){
String c = headers.getFirst(c);
}
if(bodyObj.containsKey(d)){
String d = (String)bodyObj.get(d);
}
}catch(Exception e){
return intercep(exchange,e);
}
//请求继续执行
return chain.filter(exchange);
}
protected Mono<Void> intercept(ServerWebExchange exchange,Exception e){
logger.info("触发异常"+e+",请求为:"+exchange.getRequest().getURI());
//封装状态码和错误信息
exchange.getResponse().setStatusCode("500");
return exchange.getRespnse.writeWith(Mono.error(new ZDYYCLException("1234","错误信息")));
//注:ZDYYCLException为封装的异常类,继承运行异常runtimeException
}
}}
4.responseMac返回报文过滤器
@Component
public calss responseMacGatewayFIlterFactory extends AbstractGatewayFilterFactory<object>{
private static final Logger logger = LoggerFactory.getLOgger(responseMacGatewayFIlterFactory.class);
private final responseMacFilter filter = new responseMacFilter();
@Override
public GatewayFIlter apply(Object ovject){
return filter;
}
private class filter implements GatewayFilter,Orderd{
@Override
public int getOder(){
return -2;
}
@Override
public Meno<Void> filter(ServerWebExchange exchange ,GatewayFilterChain chain){
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(exchange.getResponse()){
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body){
ServerHttpRequest request = exchange.getRequest();
HttpHeaders headers = request.getHeaders();
HttpHeaders headers = getHeaders();
if(HttpStatus.OK.equals(getStatusCOde()) && body instanceof Flux){
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
return super.writhWith(fluxBody.buffer.map(dataBuffer ->{
byte[][] contents = new byte[dataBuffers.size()][];
int id =0;
for(DataBuffer databuffer :dataBuffers){
byte[] content = new byte[DataBUffer.readableByteCount()];
dataBuffer.read(content);
DataBufferUtils.release(dataBuffer);
contents[id++] = content;
}
byte[] dodyBytes = ByteUtils.concatAll(contents);
String resStr = new String(dodyBytes,"utf-8");
dodyBytes = resStr.getBytes("utf-8");
logger.info("返回报文:"+resStr );
header.remove(HttpHeaders.CONTENT_LENGTH);
header.setContentLength(dodyBytes.length);
return bufferFactory.wrap(dodyBytes);
}));
}
return super.writeWith(body);
}
};
return chain.filter(exchange.mutate().response(decoratedResponse).build());
}}}
5.额外扩展:
5.1:第四部中,获取response的返回报文body时:
byte[][] contents = new byte[dataBuffers.size()][];
int id =0;
for(DataBuffer databuffer :dataBuffers){
byte[] content = new byte[DataBUffer.readableByteCount()];
dataBuffer.read(content);
DataBufferUtils.release(dataBuffer);
contents[id++] = content;
}
byte[] dodyBytes = ByteUtils.concatAll(contents);
String resStr = new String(dodyBytes,"utf-8");
方法二:
private final Joiner joiner = Joiner.on("");
String jsonStr = joiner.join(dataBuffers.stream.map(dataBUffer ->{
byte[] content = new byte[dataBUffer.readableByteCount()];
databuffer.read(content);
DataBUfferUtils.release(databuffer);
return new String(content,"utf-8");
}).tpArray(String[] ::new));
Map<String,Object> json = JSON.parseObject(jsonStr).getInnerMap;
5.2quest入参过滤器中,在过滤器中,重新组装请求,访问另一个ip,
1。可看情况是否重新添加http头信息
ServerHttpRequest request = exchange.getRequest();
request.mutate().header("key","value");
或者
HttpHeaders headers = new httpHeaders();
headers.putAll(super.getHeaders());
headers.set("key","value");
2.重新组装一个请求访问业务服务;
private final List<HttpMessageReader<?>> messageReaders = HandlerStrategies.withDefaults().messageReaders();
ServerRequest serverRequest = ServerRequest.create(exchange,messageReaders);
final JSONObject finalBodyObj = bodyObj;
Mono<byte[]> modifiedBody = serverRequest.bodyToMono(byte[].class).flatMap(body -> {
byte[] bytes = JSON.toJSONString(finalBodyObj).getBytes(utf-8);
return Mono.just(bytes);.
});
BodyInserter<Mono<byte[]>,ReactiveHttpOutputMessage> bodyInserter = BodyInserter.fromPublisher(modifiedBody,byte[].class);
CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange,headers);
return bodyInserter.insert(outputMessage,new BodyInserterContext()).then(Mono.defer(() -> {
ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getrequest()){
@Override
public URI getURI(){return "再次跳转的访问ip地址路径"}
@Override
public HttpHeaders getHeaders(){
return headers;
}
@Override
Public Flux<DataBuffer> getBody(){
return outputMessage.getBody();
}
};
return chain.filter(exchange.mutate.request(decorator).build());
})).onErrorResume(Exception.class,e ->{
return intercep(exchange,e);
});
protected Mono<Void> intercept(ServerWebExchange exchange,Exception e){
logger.info("触发异常"+e+",请求为:"+exchange.getRequest().getURI());
exchange.getResponse().setStatusCode("500");
return exchange.getRespnse.writeWith(Mono.error(new ZDYYCLException("1234","错误信息")));
}
|