Spaces:
Sleeping
Sleeping
Commit ·
39e18d3
1
Parent(s): 4ef0fd3
add commmon services
Browse files- src/main/java/com/artandscience/webbff/webbff/common/exception/SessionException.java +25 -0
- src/main/java/com/artandscience/webbff/webbff/common/interceptor/ResponseErrorHandler.java +97 -0
- src/main/java/com/artandscience/webbff/webbff/common/interceptor/TransactionFilter.java +59 -0
- src/main/java/com/artandscience/webbff/webbff/common/service/SessionService.java +11 -0
- src/main/java/com/artandscience/webbff/webbff/common/service/impl/SessionServiceImpl.java +60 -0
- src/main/java/com/artandscience/webbff/webbff/common/utils/RedisUtil.java +32 -0
- src/main/java/com/artandscience/webbff/webbff/configuration/RedisConfiguration.java +46 -0
- src/main/java/com/artandscience/webbff/webbff/configuration/RedisLocalConfiguration.java +51 -0
src/main/java/com/artandscience/webbff/webbff/common/exception/SessionException.java
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.common.exception;
|
| 2 |
+
|
| 3 |
+
import com.artandscience.webbff.webbff.common.exception.enums.BaseErrorType;
|
| 4 |
+
import lombok.Getter;
|
| 5 |
+
|
| 6 |
+
import java.io.Serial;
|
| 7 |
+
import java.io.Serializable;
|
| 8 |
+
|
| 9 |
+
@Getter
|
| 10 |
+
public class SessionException extends RuntimeException implements Serializable {
|
| 11 |
+
|
| 12 |
+
@Serial
|
| 13 |
+
private static final long serialVersionUID = -7123456789012345673L;
|
| 14 |
+
|
| 15 |
+
private final String code;
|
| 16 |
+
private final String message;
|
| 17 |
+
private final String[] parameters;
|
| 18 |
+
|
| 19 |
+
public SessionException(BaseErrorType baseErrorType, String... parameters) {
|
| 20 |
+
this.code = baseErrorType.getCode();
|
| 21 |
+
this.message = baseErrorType.getMessage();
|
| 22 |
+
this.parameters = parameters;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
}
|
src/main/java/com/artandscience/webbff/webbff/common/interceptor/ResponseErrorHandler.java
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.common.interceptor;
|
| 2 |
+
|
| 3 |
+
import com.artandscience.webbff.webbff.common.base.controller.BaseController;
|
| 4 |
+
import com.artandscience.webbff.webbff.common.base.model.MessageModel;
|
| 5 |
+
import com.artandscience.webbff.webbff.common.base.model.RequestHeader;
|
| 6 |
+
import com.artandscience.webbff.webbff.common.base.response.MessageBox;
|
| 7 |
+
import com.artandscience.webbff.webbff.common.base.response.MessageResponse;
|
| 8 |
+
import com.artandscience.webbff.webbff.common.exception.BusinessException;
|
| 9 |
+
import com.artandscience.webbff.webbff.common.exception.SessionException;
|
| 10 |
+
import com.artandscience.webbff.webbff.common.exception.enums.UnhandledErrorType;
|
| 11 |
+
import com.artandscience.webbff.webbff.common.scope.RequestHeaderScope;
|
| 12 |
+
import com.artandscience.webbff.webbff.common.service.ExternalAttributeService;
|
| 13 |
+
import lombok.RequiredArgsConstructor;
|
| 14 |
+
import lombok.extern.slf4j.Slf4j;
|
| 15 |
+
import org.apache.commons.lang3.StringUtils;
|
| 16 |
+
import org.springframework.http.HttpStatus;
|
| 17 |
+
import org.springframework.validation.FieldError;
|
| 18 |
+
import org.springframework.web.bind.MethodArgumentNotValidException;
|
| 19 |
+
import org.springframework.web.bind.annotation.ExceptionHandler;
|
| 20 |
+
import org.springframework.web.bind.annotation.ResponseStatus;
|
| 21 |
+
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
| 22 |
+
import org.springframework.web.method.HandlerMethod;
|
| 23 |
+
|
| 24 |
+
import java.util.ArrayList;
|
| 25 |
+
import java.util.List;
|
| 26 |
+
import java.util.Optional;
|
| 27 |
+
|
| 28 |
+
@RestControllerAdvice
|
| 29 |
+
@RequiredArgsConstructor
|
| 30 |
+
@Slf4j
|
| 31 |
+
public class ResponseErrorHandler extends BaseController {
|
| 32 |
+
|
| 33 |
+
private final ExternalAttributeService externalAttributeService;
|
| 34 |
+
private final RequestHeaderScope requestHeaderScope;
|
| 35 |
+
|
| 36 |
+
@ExceptionHandler(value = Exception.class)
|
| 37 |
+
@ResponseStatus(HttpStatus.OK)
|
| 38 |
+
public MessageResponse<Void> handleExceptionError(Exception e, HandlerMethod handlerMethod) {
|
| 39 |
+
RequestHeader requestHeader = requestHeaderScope.getRequestHeader();
|
| 40 |
+
log.error("Exception occurred: " + e.getMessage(), e);
|
| 41 |
+
var messageModel = getDefaultMessageModel(handlerMethod);
|
| 42 |
+
MessageBox messageBox = externalAttributeService.getMessageBox(messageModel);
|
| 43 |
+
List<MessageBox> messageBoxList = new ArrayList<>();
|
| 44 |
+
messageBoxList.add(messageBox);
|
| 45 |
+
return responseFail(messageBoxList);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
@ExceptionHandler(value = SessionException.class)
|
| 49 |
+
@ResponseStatus(HttpStatus.OK)
|
| 50 |
+
public MessageResponse<Void> handleSessionExceptionError(SessionException e) {
|
| 51 |
+
log.error("SessionException occurred: " + e.getMessage() + " Error code: " + e.getCode(), e);
|
| 52 |
+
var messageModel = MessageModel.builder().code(e.getCode()).build();
|
| 53 |
+
final var messageBox = externalAttributeService.getMessageBox(messageModel);
|
| 54 |
+
List<MessageBox> messageBoxList = new ArrayList<>();
|
| 55 |
+
messageBoxList.add(messageBox);
|
| 56 |
+
return responseFail(messageBoxList);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
@ExceptionHandler(value = BusinessException.class)
|
| 60 |
+
@ResponseStatus(HttpStatus.OK)
|
| 61 |
+
public MessageResponse<Void> handleBusinessExceptionError(BusinessException e) {
|
| 62 |
+
log.error("BusinessException occurred: " + e.getMessage() + " Error code: " + e.getCode(), e);
|
| 63 |
+
var messageModel = MessageModel.builder().code(e.getCode()).build();
|
| 64 |
+
final var messageBox = externalAttributeService.getMessageBox(messageModel);
|
| 65 |
+
List<MessageBox> messageBoxList = new ArrayList<>();
|
| 66 |
+
messageBoxList.add(messageBox);
|
| 67 |
+
return responseFail(messageBoxList);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
| 71 |
+
@ResponseStatus(HttpStatus.OK)
|
| 72 |
+
public MessageResponse<Void> handleValidationExceptionHandler(MethodArgumentNotValidException e) {
|
| 73 |
+
Optional<FieldError> fieldError = Optional.ofNullable(e.getFieldError());
|
| 74 |
+
String fieldErrorMessage = fieldError.map(error -> Optional.ofNullable(error.getDefaultMessage()).get())
|
| 75 |
+
.orElseGet(UnhandledErrorType.UNHANDLED_ERROR_TYPE::getCode);
|
| 76 |
+
log.error("MethodArgumentNotValidException occurred: " + fieldErrorMessage);
|
| 77 |
+
var messageModel = MessageModel.builder().code(fieldErrorMessage).build();
|
| 78 |
+
final var messageBox = externalAttributeService.getMessageBox(messageModel);
|
| 79 |
+
List<MessageBox> messageBoxList = new ArrayList<>();
|
| 80 |
+
messageBoxList.add(messageBox);
|
| 81 |
+
return responseFail(messageBoxList);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
private MessageModel getDefaultMessageModel(HandlerMethod handlerMethod){
|
| 85 |
+
var methodName = getErrorFunction(handlerMethod);
|
| 86 |
+
return StringUtils.isNotEmpty(methodName) ?
|
| 87 |
+
MessageModel.builder().code("DEFAULT_MESSAGE_BOX" + "." + methodName).build() :
|
| 88 |
+
MessageModel.builder().code(UnhandledErrorType.UNHANDLED_ERROR_TYPE.getCode()).build();
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
private String getErrorFunction(HandlerMethod handlerMethod) {
|
| 92 |
+
String controllerName = handlerMethod.getMethod().getDeclaringClass().getName();
|
| 93 |
+
controllerName = controllerName.substring(controllerName.lastIndexOf('.') + 1);
|
| 94 |
+
return controllerName.replace("Controller","") + "_" + handlerMethod.getMethod().getName();
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
}
|
src/main/java/com/artandscience/webbff/webbff/common/interceptor/TransactionFilter.java
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.common.interceptor;
|
| 2 |
+
|
| 3 |
+
import com.artandscience.webbff.webbff.common.base.model.RequestHeader;
|
| 4 |
+
import com.artandscience.webbff.webbff.common.scope.RequestHeaderScope;
|
| 5 |
+
import jakarta.servlet.FilterChain;
|
| 6 |
+
import jakarta.servlet.ServletException;
|
| 7 |
+
import jakarta.servlet.annotation.WebFilter;
|
| 8 |
+
import jakarta.servlet.http.HttpServletRequest;
|
| 9 |
+
import jakarta.servlet.http.HttpServletResponse;
|
| 10 |
+
import lombok.RequiredArgsConstructor;
|
| 11 |
+
import lombok.extern.slf4j.Slf4j;
|
| 12 |
+
import org.springframework.core.annotation.Order;
|
| 13 |
+
import org.springframework.stereotype.Component;
|
| 14 |
+
import org.springframework.web.filter.OncePerRequestFilter;
|
| 15 |
+
|
| 16 |
+
import java.io.IOException;
|
| 17 |
+
|
| 18 |
+
import static com.artandscience.webbff.webbff.common.constants.HeaderConstant.HEADER_LANGUAGE;
|
| 19 |
+
import static com.artandscience.webbff.webbff.common.constants.HeaderConstant.HEADER_DEVICE_APP_NAME;
|
| 20 |
+
import static com.artandscience.webbff.webbff.common.constants.HeaderConstant.HEADER_DEVICE_APP_VERSION;
|
| 21 |
+
import static com.artandscience.webbff.webbff.common.constants.HeaderConstant.HEADER_SESSION_ID;
|
| 22 |
+
import static com.artandscience.webbff.webbff.common.constants.HeaderConstant.HEADER_CORRELATION_ID;
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@Slf4j
|
| 27 |
+
@Order(1)
|
| 28 |
+
@Component
|
| 29 |
+
@RequiredArgsConstructor
|
| 30 |
+
@WebFilter(filterName = "transactionFilter")
|
| 31 |
+
public class TransactionFilter extends OncePerRequestFilter {
|
| 32 |
+
|
| 33 |
+
private final RequestHeaderScope requestHeaderScope;
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@Override
|
| 37 |
+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
| 38 |
+
throws ServletException, IOException {
|
| 39 |
+
|
| 40 |
+
try {
|
| 41 |
+
RequestHeader requestHeader = fillRequestHeader(request);
|
| 42 |
+
requestHeaderScope.setRequestHeader(requestHeader);
|
| 43 |
+
} catch (Exception e) {
|
| 44 |
+
log.error("Transaction Filter exception " + e.getMessage(), e);
|
| 45 |
+
}
|
| 46 |
+
filterChain.doFilter(request,response);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
private RequestHeader fillRequestHeader(HttpServletRequest request) {
|
| 50 |
+
return RequestHeader.builder()
|
| 51 |
+
.language(request.getHeader(HEADER_LANGUAGE))
|
| 52 |
+
.deviceAppName(request.getHeader(HEADER_DEVICE_APP_NAME))
|
| 53 |
+
.deviceAppVersion(request.getHeader(HEADER_DEVICE_APP_VERSION))
|
| 54 |
+
.sessionId(request.getHeader(HEADER_SESSION_ID))
|
| 55 |
+
.correlationId(request.getHeader(HEADER_CORRELATION_ID))
|
| 56 |
+
.build();
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
}
|
src/main/java/com/artandscience/webbff/webbff/common/service/SessionService.java
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.common.service;
|
| 2 |
+
|
| 3 |
+
public interface SessionService {
|
| 4 |
+
|
| 5 |
+
<T> void setSession(String sessionKey, String exTime, T object);
|
| 6 |
+
|
| 7 |
+
<T> T getSession(String sessionKey, Class<T> returnType, boolean... isNullable);
|
| 8 |
+
|
| 9 |
+
void removeSession(String sessionKey);
|
| 10 |
+
|
| 11 |
+
}
|
src/main/java/com/artandscience/webbff/webbff/common/service/impl/SessionServiceImpl.java
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.common.service.impl;
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import com.artandscience.webbff.webbff.common.exception.SessionException;
|
| 5 |
+
import com.artandscience.webbff.webbff.common.exception.enums.SessionErrorType;
|
| 6 |
+
import com.artandscience.webbff.webbff.common.scope.RequestHeaderScope;
|
| 7 |
+
import com.artandscience.webbff.webbff.common.service.SessionService;
|
| 8 |
+
import com.artandscience.webbff.webbff.common.utils.RedisUtil;
|
| 9 |
+
import io.micrometer.common.util.StringUtils;
|
| 10 |
+
import lombok.RequiredArgsConstructor;
|
| 11 |
+
import lombok.extern.slf4j.Slf4j;
|
| 12 |
+
import org.springframework.beans.factory.annotation.Value;
|
| 13 |
+
import org.springframework.stereotype.Service;
|
| 14 |
+
|
| 15 |
+
import java.util.concurrent.TimeUnit;
|
| 16 |
+
|
| 17 |
+
@Service
|
| 18 |
+
@Slf4j
|
| 19 |
+
@RequiredArgsConstructor
|
| 20 |
+
public class SessionServiceImpl implements SessionService {
|
| 21 |
+
|
| 22 |
+
private final RedisUtil redisUtil;
|
| 23 |
+
|
| 24 |
+
private final RequestHeaderScope requestHeaderScope;
|
| 25 |
+
|
| 26 |
+
@Value("${spring.data.redis.session-expire-time}")
|
| 27 |
+
private String sessionExpireTime;
|
| 28 |
+
|
| 29 |
+
@Override
|
| 30 |
+
public <T> void setSession(String sessionKey, String exTime, T object) {
|
| 31 |
+
String key = StringUtils.isEmpty(sessionKey) ? requestHeaderScope.getRequestHeader().getSessionId() : sessionKey;
|
| 32 |
+
String expireTime = !StringUtils.isEmpty(exTime) ? exTime : sessionExpireTime;
|
| 33 |
+
redisUtil.putValue(key,object,Integer.parseInt(expireTime), TimeUnit.MINUTES);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
@Override
|
| 37 |
+
public <T> T getSession(String sessionKey, Class<T> returnType, boolean... isNullable) {
|
| 38 |
+
Object sessionObject = null;
|
| 39 |
+
String key = StringUtils.isEmpty(sessionKey) ? requestHeaderScope.getRequestHeader().getSessionId() : sessionKey;
|
| 40 |
+
try {
|
| 41 |
+
Boolean nullableSession = false;
|
| 42 |
+
if (isNullable.length > 0) {
|
| 43 |
+
nullableSession = isNullable[0];
|
| 44 |
+
}
|
| 45 |
+
sessionObject = redisUtil.getValue(key);
|
| 46 |
+
if ((!nullableSession && sessionObject == null) || returnType == null) {
|
| 47 |
+
log.error("SessionService get object is null for sessionKey: " + sessionKey);
|
| 48 |
+
}
|
| 49 |
+
}catch (Exception ex) {
|
| 50 |
+
log.error("SessionService get object is null for sessionKey: " + sessionKey);
|
| 51 |
+
throw new SessionException(SessionErrorType.SESSION_NOT_FOUND);
|
| 52 |
+
}
|
| 53 |
+
return returnType.cast(sessionObject);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
@Override
|
| 57 |
+
public void removeSession(String sessionKey) {
|
| 58 |
+
|
| 59 |
+
}
|
| 60 |
+
}
|
src/main/java/com/artandscience/webbff/webbff/common/utils/RedisUtil.java
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.common.utils;
|
| 2 |
+
|
| 3 |
+
import org.springframework.beans.factory.annotation.Autowired;
|
| 4 |
+
import org.springframework.data.redis.core.RedisTemplate;
|
| 5 |
+
import org.springframework.data.redis.core.ValueOperations;
|
| 6 |
+
import org.springframework.stereotype.Component;
|
| 7 |
+
|
| 8 |
+
import java.util.concurrent.TimeUnit;
|
| 9 |
+
|
| 10 |
+
@Component
|
| 11 |
+
public class RedisUtil {
|
| 12 |
+
|
| 13 |
+
private final ValueOperations<String,Object> valueOperations;
|
| 14 |
+
|
| 15 |
+
@Autowired
|
| 16 |
+
public RedisUtil(RedisTemplate<String,Object> redisTemplate) {
|
| 17 |
+
this.valueOperations = redisTemplate.opsForValue();
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
public void putValue(String key, Object value, int expirationTime, TimeUnit expirationTimeUnit) {
|
| 21 |
+
valueOperations.set(key,value,expirationTime,expirationTimeUnit);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
public Object getValue(String key) {
|
| 25 |
+
return valueOperations.get(key);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
public void removeValue(String key) {
|
| 29 |
+
valueOperations.getOperations().delete(key);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
}
|
src/main/java/com/artandscience/webbff/webbff/configuration/RedisConfiguration.java
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.configuration;
|
| 2 |
+
|
| 3 |
+
import lombok.RequiredArgsConstructor;
|
| 4 |
+
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
| 5 |
+
import org.springframework.cache.annotation.EnableCaching;
|
| 6 |
+
import org.springframework.context.annotation.Bean;
|
| 7 |
+
import org.springframework.context.annotation.Configuration;
|
| 8 |
+
import org.springframework.context.annotation.Profile;
|
| 9 |
+
import org.springframework.data.redis.connection.RedisNode;
|
| 10 |
+
import org.springframework.data.redis.connection.RedisPassword;
|
| 11 |
+
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
| 12 |
+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
| 13 |
+
import org.springframework.data.redis.core.RedisTemplate;
|
| 14 |
+
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
| 15 |
+
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
| 16 |
+
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
| 17 |
+
|
| 18 |
+
@Configuration
|
| 19 |
+
@RequiredArgsConstructor
|
| 20 |
+
@EnableCaching
|
| 21 |
+
@Profile("!local")
|
| 22 |
+
public class RedisConfiguration {
|
| 23 |
+
|
| 24 |
+
private final RedisProperties redisProperties;
|
| 25 |
+
|
| 26 |
+
@Bean
|
| 27 |
+
protected JedisConnectionFactory redisConnectionFactory() {
|
| 28 |
+
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
|
| 29 |
+
redisSentinelConfiguration.setMaster(redisProperties.getSentinel().getMaster());
|
| 30 |
+
redisSentinelConfiguration.addSentinel(new RedisNode(redisProperties.getHost(), redisProperties.getPort()));
|
| 31 |
+
redisSentinelConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
|
| 32 |
+
return new JedisConnectionFactory(redisSentinelConfiguration);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
@Bean
|
| 36 |
+
public RedisTemplate<String,Object> redisTemplate() {
|
| 37 |
+
final RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
|
| 38 |
+
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
| 39 |
+
redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
|
| 40 |
+
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
|
| 41 |
+
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
| 42 |
+
redisTemplate.setConnectionFactory(redisConnectionFactory());
|
| 43 |
+
return redisTemplate;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
}
|
src/main/java/com/artandscience/webbff/webbff/configuration/RedisLocalConfiguration.java
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.artandscience.webbff.webbff.configuration;
|
| 2 |
+
|
| 3 |
+
import lombok.RequiredArgsConstructor;
|
| 4 |
+
import org.springframework.beans.factory.annotation.Value;
|
| 5 |
+
import org.springframework.cache.annotation.EnableCaching;
|
| 6 |
+
import org.springframework.context.annotation.Bean;
|
| 7 |
+
import org.springframework.context.annotation.Configuration;
|
| 8 |
+
import org.springframework.context.annotation.Profile;
|
| 9 |
+
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
| 10 |
+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
| 11 |
+
import org.springframework.data.redis.core.RedisTemplate;
|
| 12 |
+
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
| 13 |
+
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
| 14 |
+
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
| 15 |
+
|
| 16 |
+
@Configuration
|
| 17 |
+
@RequiredArgsConstructor
|
| 18 |
+
@EnableCaching
|
| 19 |
+
@Profile("local")
|
| 20 |
+
public class RedisLocalConfiguration {
|
| 21 |
+
|
| 22 |
+
@Value("${spring.data.redis.host}")
|
| 23 |
+
private String redisHostName;
|
| 24 |
+
|
| 25 |
+
@Value("${spring.data.redis.port}")
|
| 26 |
+
private int redisPost;
|
| 27 |
+
|
| 28 |
+
@Value("${spring.data.redis.password}")
|
| 29 |
+
private String redisPassword;
|
| 30 |
+
|
| 31 |
+
@Bean
|
| 32 |
+
protected JedisConnectionFactory redisConnectionFactory() {
|
| 33 |
+
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
|
| 34 |
+
redisStandaloneConfiguration.setHostName(redisHostName);
|
| 35 |
+
redisStandaloneConfiguration.setPort(redisPost);
|
| 36 |
+
redisStandaloneConfiguration.setPassword(redisPassword);
|
| 37 |
+
return new JedisConnectionFactory(redisStandaloneConfiguration);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
@Bean
|
| 41 |
+
public RedisTemplate<String,Object> redisTemplate() {
|
| 42 |
+
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
|
| 43 |
+
redisTemplate.setConnectionFactory(redisConnectionFactory());
|
| 44 |
+
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
| 45 |
+
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
| 46 |
+
redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
|
| 47 |
+
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
|
| 48 |
+
return redisTemplate;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
}
|