Commit 2d3e43ac by yw

项目初始化

parent 0a2093cb
......@@ -36,8 +36,8 @@ feign:
client:
config:
default:
connectTimeout: 10000
readTimeout: 10000
connectTimeout: 5000
readTimeout: 5000
compression:
request:
enabled: true
......@@ -49,10 +49,12 @@ hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
strategy: SEMAPHORE
thread:
timeoutInMilliseconds: 60000
timeoutInMilliseconds: 5000 #断路器超时时间,默认1000ms
shareSecurityContext: true
#请求处理的超时时间
......
......@@ -10,8 +10,8 @@ spring:
allowCredentials: true
discovery:
locator:
lowerCaseServiceId: true
enabled: true
lower-case-service-id: true
default-filters:
- StripPrefix=1
routes:
......@@ -24,7 +24,7 @@ spring:
- name: Hystrix
args:
name: authFallback
fallbackUri: forward:/fallback/datax-auth
fallbackUri: forward:/fallback
# 系统配置中心
- id: datax-service-system
uri: lb://datax-service-system
......@@ -34,7 +34,7 @@ spring:
- name: Hystrix
args:
name: systemFallback
fallbackUri: forward:/fallback/datax-service-system
fallbackUri: forward:/fallback
# 邮件中心
- id: datax-service-email
uri: lb://datax-service-email
......@@ -44,7 +44,7 @@ spring:
- name: Hystrix
args:
name: emailFallback
fallbackUri: forward:/fallback/datax-service-email
fallbackUri: forward:/fallback
# 邮件中心
- id: datax-service-file
uri: lb://datax-service-file
......@@ -54,7 +54,7 @@ spring:
- name: Hystrix
args:
name: fileFallback
fallbackUri: forward:/fallback/datax-service-file
fallbackUri: forward:/fallback
# 即时通讯消息中心
- id: datax-websocket-server
uri: ws://localhost:9876
......
package cn.datax.gateway.config;
import cn.datax.gateway.handler.HystrixFallbackHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
/**
* 路由配置信息 特殊请求直接在此处理,不进行路由转发
*/
@Slf4j
@Component
@AllArgsConstructor
public class RouterFunctionConfiguration {
private final HystrixFallbackHandler hystrixFallbackHandler;
@Bean
public RouterFunction routerFunction() {
return RouterFunctions.route(
RequestPredicates.path("/fallback")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler);
}
}
package cn.datax.gateway.controller;
import cn.datax.common.core.R;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class FallbackController {
@RequestMapping("fallback/{name}")
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Mono<R> systemFallback(@PathVariable String name) {
String response = String.format("访问%s超时或者服务不可用", name);
return Mono.just(R.error(response));
}
}
......@@ -32,7 +32,7 @@ public class DataGatewayRequestFilter implements GlobalFilter {
printLog(exchange);
byte[] token = Base64Utils.encode((DataConstant.GATEWAY_TOKEN_VALUE).getBytes());
String headerValues = new String(token);
String[] headerValues = {new String(token)};
ServerHttpRequest build = request.mutate().header(DataConstant.GATEWAY_TOKEN_HEADER, headerValues).build();
ServerWebExchange newExchange = exchange.mutate().request(build).build();
return chain.filter(newExchange);
......
package cn.datax.gateway.handler;
import cn.datax.common.core.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import java.util.Optional;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
/**
* Hystrix 降级处理 网关请求错误重定向到fallback 再到这里
*/
@Slf4j
@Component
public class HystrixFallbackHandler implements HandlerFunction<ServerResponse> {
@Override
public Mono<ServerResponse> handle(ServerRequest serverRequest) {
//得到原始的请求的url
Optional<Object> originalUris = serverRequest.attribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
//如果这个urls里面有东西
originalUris.ifPresent(originalUri -> log.error("网关执行请求:{}失败,hystrix服务降级处理", originalUri));
//返回空的response
return ServerResponse
.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(BodyInserters.fromObject(R.error("服务超时或者服务不可用,请稍后重试")));
}
}
......@@ -10,9 +10,7 @@ public class EmailServiceFeignFallbackFactory implements FallbackFactory<EmailSe
@Override
public EmailServiceFeign create(Throwable throwable) {
EmailServiceFeignFallbackImpl emailServiceFeignFallbackImpl = new EmailServiceFeignFallbackImpl();
emailServiceFeignFallbackImpl.setCause(throwable);
return emailServiceFeignFallbackImpl;
return new EmailServiceFeignFallbackImpl(throwable);
}
}
......@@ -3,20 +3,18 @@ package cn.datax.service.email.api.feign.fallback;
import cn.datax.common.core.R;
import cn.datax.service.email.api.entity.EmailEntity;
import cn.datax.service.email.api.feign.EmailServiceFeign;
import lombok.Setter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@AllArgsConstructor
public class EmailServiceFeignFallbackImpl implements EmailServiceFeign {
@Setter
private Throwable cause;
private final Throwable cause;
@Override
public R sendMail(EmailEntity mail) {
log.error("feign 发送邮件失败", cause);
log.error("feign 调用邮件出错,信息:{}", cause.getLocalizedMessage());
return null;
}
}
......@@ -10,8 +10,6 @@ public class UserServiceFeignFallbackFactory implements FallbackFactory<UserServ
@Override
public UserServiceFeign create(Throwable throwable) {
UserServiceFeignFallbackImpl userServiceFeignFallbackImpl = new UserServiceFeignFallbackImpl();
userServiceFeignFallbackImpl.setCause(throwable);
return userServiceFeignFallbackImpl;
return new UserServiceFeignFallbackImpl(throwable);
}
}
......@@ -2,20 +2,18 @@ package cn.datax.service.system.api.feign.fallback;
import cn.datax.common.core.R;
import cn.datax.service.system.api.feign.UserServiceFeign;
import lombok.Setter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@AllArgsConstructor
public class UserServiceFeignFallbackImpl implements UserServiceFeign {
@Setter
private Throwable cause;
private final Throwable cause;
@Override
public R getUserByUsername(String username) {
log.error("feign 获取用户信息失败", cause);
log.error("feign 调用用户{}出错,信息:{}", username, cause.getLocalizedMessage());
return null;
}
}
......@@ -9,8 +9,8 @@ import cn.datax.service.system.api.entity.UserPostEntity;
import cn.datax.service.system.api.entity.UserRoleEntity;
import cn.datax.service.system.dao.UserDao;
import cn.datax.service.system.dao.UserDeptDao;
import cn.datax.service.system.dao.UserPostDao;
import cn.datax.service.system.dao.UserRoleDao;
import cn.datax.service.system.dao.UserPostDao;
import cn.datax.service.system.mapstruct.UserMapper;
import cn.datax.service.system.service.UserService;
import cn.datax.common.base.BaseServiceImpl;
......@@ -132,5 +132,5 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
String passwordEncode = new BCryptPasswordEncoder().encode(userPasswordDto.getPassword());
userDao.updateUserPassword(passwordEncode, userPasswordDto.getId());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment