在使用springcloud-gateway网关路由到微服时出现异常如何对异常进行统一封装处理?
问题:项目中使用springcloud-gateway,请求到网关,再路由到微服务时出现微服务未找到异常
网关pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
请求:http://192.168.135.99:9999/busi/sms/send
返回
{
"timestamp": "2020-04-02 12:01:13",
"path": "/busi/sms/send",
"status": 503,
"error": "Service Unavailable",
"message": "Unable to find instance for jhjcn-business"
}
解决方案
1、创建AbstractExceptionHandler
package com.jhjcn.gateway.exception;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.web.server.ResponseStatusException;
import java.util.HashMap;
import java.util.Map;
/**
* @Deacription TODO
* @Author jianhua.hong
* @Date 2020/4/2 11:33
**/
public abstract class AbstractExceptionHandler {
private static final String DEFAULT_ERROR_CODE = "999999";
protected String formatMessage(Throwable ex) {
String errorMessage = null;
if (ex instanceof NotFoundException) {
String reason = ((NotFoundException) ex).getReason();
errorMessage = reason;
} else if (ex instanceof ResponseStatusException) {
ResponseStatusException responseStatusException = (ResponseStatusException) ex;
errorMessage = responseStatusException.getMessage();
} else {
errorMessage = ex.getMessage();
}
return errorMessage;
}
protected Map<String, Object> buildErrorMap(String errorMessage) {
Map<String, Object> resMap = new HashMap<>();
resMap.put("isSuccess", false);
resMap.put("code", DEFAULT_ERROR_CODE);
resMap.put("message", errorMessage);
resMap.put("data", null);
return resMap;
}
}
2、创建GatewayExceptionHandler
package com.jhjcn.gateway.exception;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @Deacription TODO
* @Author jianhua.hong
* @Date 2020/4/2 10:36
**/
@Slf4j
public class GatewayExceptionHandler extends AbstractExceptionHandler implements ErrorWebExceptionHandler {
private static final String TRACE_ID = "traceId";
private static final String JHJCN_BUSI_NOT_FOUND = "Unable to find instance for jhjcn-business";
private static final String JHJCN_BUSI_NOT_FOUND_ZH = "未xxx微服务,请检查服务是否可用";
private List<HttpMessageReader<?>> messageReaders = Collections.emptyList();
private List<HttpMessageWriter<?>> messageWriters = Collections.emptyList();
private List<ViewResolver> viewResolvers = Collections.emptyList();
private ThreadLocal<Map<String, Object>> exceptionHandlerResult = new ThreadLocal<>();
public void setMessageReaders(List<HttpMessageReader<?>> messageReaders) {
Assert.notNull(messageReaders, "'messageReaders' must not be null");
this.messageReaders = messageReaders;
}
public void setViewResolvers(List<ViewResolver> viewResolvers) {
this.viewResolvers = viewResolvers;
}
public void setMessageWriters(List<HttpMessageWriter<?>> messageWriters) {
Assert.notNull(messageWriters, "'messageWriters' must not be null");
this.messageWriters = messageWriters;
}
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
String errorMessage = super.formatMessage(ex);
if (errorMessage.equals(JHJCN_BUSI_NOT_FOUND)) {
errorMessage = JHJCN_BUSI_NOT_FOUND_ZH;
}
Map<String, Object> errorMap = super.buildErrorMap(errorMessage);
ServerHttpRequest request = exchange.getRequest();
String traceId = request.getHeaders().getFirst(TRACE_ID);
log.error("GatewayExceptionHandler request info [traceId={}] result error=[{}]", traceId, JSONObject.toJSONString(errorMap));
if (exchange.getResponse().isCommitted()) {
return Mono.error(ex);
}
exceptionHandlerResult.set(errorMap);
ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders);
return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
.switchIfEmpty(Mono.error(ex))
.flatMap((handler) -> handler.handle(newRequest))
.flatMap((response) -> write(exchange, response));
}
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
Map<String, Object> result = exceptionHandlerResult.get();
return ServerResponse.status(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(BodyInserters.fromObject(result));
}
private Mono<? extends Void> write(ServerWebExchange exchange,
ServerResponse response) {
exchange.getResponse().getHeaders()
.setContentType(response.headers().getContentType());
return response.writeTo(exchange, new ResponseContext());
}
private class ResponseContext implements ServerResponse.Context {
@Override
public List<HttpMessageWriter<?>> messageWriters() {
return GatewayExceptionHandler.this.messageWriters;
}
@Override
public List<ViewResolver> viewResolvers() {
return GatewayExceptionHandler.this.viewResolvers;
}
}
}
3、创建GatewayExceptionConfig
package com.jhjcn.gateway.exception;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;
import java.util.Collections;
import java.util.List;
/**
* @Deacription TODO
* @Author jianhua.hong
* @Date 2020/4/2 10:45
**/
@Configuration
public class GatewayExceptionConfig {
@Primary
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
GatewayExceptionHandler gatewayExceptionHandler = new GatewayExceptionHandler();
gatewayExceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList));
gatewayExceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
gatewayExceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
return gatewayExceptionHandler;
}
}
验证
请求:http://192.168.135.99:9999/busi/sms/send
返回
{
"code": "999999",
"data": null,
"message": "未找xxx微服务,请检查服务是否可用",
"isSuccess": false
}
网友评论