You've already forked FrameTour-BE
This commit is contained in:
@@ -3,8 +3,12 @@ package com.ycwl.basic;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
@MapperScan(basePackages = "com.ycwl.basic.mapper")
|
||||
@MapperScan(basePackages = "com.ycwl.basic.*.mapper")
|
||||
public class Application {
|
||||
|
63
src/main/java/com/ycwl/basic/config/FeignConfig.java
Normal file
63
src/main/java/com/ycwl/basic/config/FeignConfig.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package com.ycwl.basic.config;
|
||||
|
||||
import feign.Logger;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.codec.ErrorDecoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class FeignConfig {
|
||||
|
||||
@Bean
|
||||
public Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.BASIC;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RequestInterceptor requestInterceptor() {
|
||||
return requestTemplate -> {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if (attributes != null) {
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
|
||||
// 传递认证头
|
||||
String authorization = request.getHeader("Authorization");
|
||||
if (authorization != null) {
|
||||
requestTemplate.header("Authorization", authorization);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorDecoder errorDecoder() {
|
||||
return new FeignErrorDecoder();
|
||||
}
|
||||
|
||||
public static class FeignErrorDecoder implements ErrorDecoder {
|
||||
private final ErrorDecoder defaultErrorDecoder = new Default();
|
||||
|
||||
@Override
|
||||
public Exception decode(String methodKey, feign.Response response) {
|
||||
log.error("Feign调用失败: method={}, status={}, reason={}",
|
||||
methodKey, response.status(), response.reason());
|
||||
|
||||
if (response.status() >= 400 && response.status() < 500) {
|
||||
// 4xx错误,客户端错误
|
||||
return new RuntimeException("客户端请求错误: " + response.reason());
|
||||
} else if (response.status() >= 500) {
|
||||
// 5xx错误,服务器错误
|
||||
return new RuntimeException("服务器内部错误: " + response.reason());
|
||||
}
|
||||
|
||||
return defaultErrorDecoder.decode(methodKey, response);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user