feat: 升级SpringBoot和SpringCloud版本
This commit is contained in:
@@ -37,15 +37,14 @@
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-websocket</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
+6
-6
@@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
||||
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
||||
@@ -52,10 +52,10 @@ public class DefaultResourceServerConf extends ResourceServerConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.AuthorizedUrl authorizedUrl = setHttp(http)
|
||||
.authorizeRequests()
|
||||
.antMatchers(securityProperties.getIgnore().getUrls()).permitAll()
|
||||
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
||||
AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizedUrl authorizedUrl = setHttp(http)
|
||||
.authorizeHttpRequests()
|
||||
.requestMatchers(securityProperties.getIgnore().getUrls()).permitAll()
|
||||
.requestMatchers(HttpMethod.OPTIONS).permitAll()
|
||||
.anyRequest();
|
||||
setAuthenticate(authorizedUrl);
|
||||
|
||||
@@ -72,7 +72,7 @@ public class DefaultResourceServerConf extends ResourceServerConfigurerAdapter {
|
||||
* url权限控制,默认是认证就通过,可以重写实现个性化
|
||||
* @param authorizedUrl
|
||||
*/
|
||||
public HttpSecurity setAuthenticate(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.AuthorizedUrl authorizedUrl) {
|
||||
public HttpSecurity setAuthenticate(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizedUrl authorizedUrl) {
|
||||
return authorizedUrl.authenticated().and();
|
||||
}
|
||||
|
||||
|
||||
+3
-4
@@ -10,9 +10,8 @@ import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurity
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
@@ -47,7 +46,7 @@ public class DefaultSecurityHandlerConfig {
|
||||
return new OAuth2AccessDeniedHandler() {
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) throws IOException, ServletException {
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) throws IOException {
|
||||
ResponseUtil.responseFailed(objectMapper, response, authException.getMessage());
|
||||
}
|
||||
};
|
||||
|
||||
+1
-2
@@ -1,12 +1,11 @@
|
||||
package com.central.oauth2.common.config;
|
||||
|
||||
import com.central.oauth2.common.util.AuthUtils;
|
||||
import jakarta.websocket.server.ServerEndpointConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.websocket.server.ServerEndpointConfig;
|
||||
|
||||
/**
|
||||
* webSocket鉴权配置
|
||||
*
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
@@ -17,6 +18,7 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.security")
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class SecurityProperties {
|
||||
private AuthProperties auth = new AuthProperties();
|
||||
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Token配置
|
||||
@@ -19,6 +20,7 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.oauth2.token.store")
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class TokenStoreProperties {
|
||||
/**
|
||||
* token存储类型(redis/db/authJwt/resJwt)
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAut
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import java.util.List;
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = UserServiceFallbackFactory.class, decode404 = true)
|
||||
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = UserServiceFallbackFactory.class, dismiss404 = true)
|
||||
public interface UserService {
|
||||
/**
|
||||
* feign rpc访问远程/users/{username}接口
|
||||
|
||||
+2
@@ -6,6 +6,7 @@ import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -17,6 +18,7 @@ import java.util.List;
|
||||
* @date 2019/1/18
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {
|
||||
@Override
|
||||
public UserService create(Throwable throwable) {
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -16,6 +17,7 @@ import java.util.Set;
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "zlt.datascope")
|
||||
@Data
|
||||
@Component
|
||||
public class DataScopeProperties {
|
||||
private static final Set<String> INGORE_SQL_ID = ImmutableSet
|
||||
.of("com.central.user.mapper.findRolesByUserId"
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -17,6 +18,7 @@ import java.util.List;
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.tenant")
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class TenantProperties {
|
||||
/**
|
||||
* 是否开启多租户
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* head中的应用参数注入clientId中
|
||||
|
||||
+1
-10
@@ -1,27 +1,18 @@
|
||||
package com.central.common.resolver;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.annotation.LoginUser;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.common.utils.LoginUserUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Token转化SysUser
|
||||
*
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.central.common.utils;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
+1
-1
@@ -5,13 +5,13 @@ import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
+11
-13
@@ -3,9 +3,7 @@ package com.central.common.utils;
|
||||
import com.central.common.passwordEncoder.SM3PasswordEncoder;
|
||||
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
|
||||
import org.springframework.security.crypto.password.*;
|
||||
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -26,16 +24,16 @@ public class PwdEncoderUtil {
|
||||
public static PasswordEncoder getDelegatingPasswordEncoder(String encodingId) {
|
||||
Map<String, PasswordEncoder> encoders = new HashMap<>();
|
||||
encoders.put("bcrypt", new BCryptPasswordEncoder());
|
||||
encoders.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder());
|
||||
encoders.put("MD4", new org.springframework.security.crypto.password.Md4PasswordEncoder());
|
||||
encoders.put("MD5", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("MD5"));
|
||||
encoders.put("noop", org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance());
|
||||
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
|
||||
encoders.put("scrypt", new SCryptPasswordEncoder());
|
||||
encoders.put("SHA-1", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1"));
|
||||
encoders.put("SHA-256", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256"));
|
||||
encoders.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder());
|
||||
encoders.put("argon2", new Argon2PasswordEncoder());
|
||||
encoders.put("ldap", new LdapShaPasswordEncoder());
|
||||
encoders.put("MD4", new Md4PasswordEncoder());
|
||||
encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
|
||||
encoders.put("noop", NoOpPasswordEncoder.getInstance());
|
||||
encoders.put("pbkdf2",Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8());
|
||||
encoders.put("scrypt", SCryptPasswordEncoder.defaultsForSpringSecurity_v5_8());
|
||||
encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));
|
||||
encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));
|
||||
encoders.put("sha256", new StandardPasswordEncoder());
|
||||
encoders.put("argon2", Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8());
|
||||
encoders.put("SM3", new SM3PasswordEncoder());
|
||||
|
||||
Assert.isTrue(encoders.containsKey(encodingId), encodingId + " is not found in idToPasswordEncoder");
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import com.central.common.model.Result;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
|
||||
@@ -14,11 +14,5 @@
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+5
-5
@@ -3,15 +3,15 @@ package com.central.common.filter;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.common.utils.LoginUserUtils;
|
||||
import jakarta.servlet.Filter;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
|
||||
+5
-5
@@ -4,14 +4,14 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.context.TenantContextHolder;
|
||||
import jakarta.servlet.Filter;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* mybatis-plus 配置
|
||||
@@ -18,6 +19,7 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.mybatis-plus.auto-fill")
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class MybatisPlusAutoFillProperties {
|
||||
/**
|
||||
* 是否开启自动填充字段
|
||||
|
||||
+44
-49
@@ -1,19 +1,20 @@
|
||||
package com.central.es.config;
|
||||
|
||||
import com.central.es.properties.RestClientPoolProperties;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientProperties;
|
||||
import org.springframework.boot.autoconfigure.elasticsearch.RestClientBuilderCustomizer;
|
||||
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* es自动配置
|
||||
@@ -25,51 +26,45 @@ import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@EnableConfigurationProperties(RestClientPoolProperties.class)
|
||||
public class RestAutoConfigure {
|
||||
@Bean
|
||||
public RestClientBuilderCustomizer restClientBuilderCustomizer(RestClientPoolProperties poolProperties
|
||||
, ElasticsearchRestClientProperties restProperties) {
|
||||
return (builder) -> {
|
||||
setRequestConfig(builder, poolProperties);
|
||||
public class RestAutoConfigure extends AbstractElasticsearchConfiguration {
|
||||
private final static String SCHEME = "http";
|
||||
private final static String URI_SPLIT = ":";
|
||||
|
||||
setHttpClientConfig(builder, poolProperties, restProperties);
|
||||
};
|
||||
}
|
||||
@Resource
|
||||
private ElasticsearchProperties restProperties;
|
||||
|
||||
/**
|
||||
* 异步httpclient连接延时配置
|
||||
*/
|
||||
private void setRequestConfig(RestClientBuilder builder, RestClientPoolProperties poolProperties){
|
||||
builder.setRequestConfigCallback(requestConfigBuilder -> {
|
||||
requestConfigBuilder.setConnectTimeout(poolProperties.getConnectTimeOut())
|
||||
.setSocketTimeout(poolProperties.getSocketTimeOut())
|
||||
.setConnectionRequestTimeout(poolProperties.getConnectionRequestTimeOut());
|
||||
return requestConfigBuilder;
|
||||
});
|
||||
}
|
||||
@Resource
|
||||
private RestClientPoolProperties poolProperties;
|
||||
|
||||
/**
|
||||
* 异步httpclient连接数配置
|
||||
*/
|
||||
private void setHttpClientConfig(RestClientBuilder builder, RestClientPoolProperties poolProperties, ElasticsearchRestClientProperties restProperties){
|
||||
builder.setHttpClientConfigCallback(httpClientBuilder -> {
|
||||
httpClientBuilder.setMaxConnTotal(poolProperties.getMaxConnectNum())
|
||||
.setMaxConnPerRoute(poolProperties.getMaxConnectPerRoute());
|
||||
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
map.from(restProperties::getUsername).to(username -> {
|
||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(AuthScope.ANY,
|
||||
new UsernamePasswordCredentials(username, restProperties.getPassword()));
|
||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
||||
});
|
||||
return httpClientBuilder;
|
||||
});
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ElasticsearchRestTemplate elasticsearchRestTemplate(RestHighLevelClient restHighLevelClient) {
|
||||
return new ElasticsearchRestTemplate(restHighLevelClient);
|
||||
@Override
|
||||
public RestHighLevelClient elasticsearchClient() {
|
||||
List<String> urlArr = restProperties.getUris();
|
||||
HttpHost[] httpPostArr = new HttpHost[urlArr.size()];
|
||||
for (int i = 0; i < urlArr.size(); i++) {
|
||||
HttpHost httpHost = new HttpHost(urlArr.get(i).split(URI_SPLIT)[0].trim(),
|
||||
Integer.parseInt(urlArr.get(i).split(URI_SPLIT)[1].trim()), SCHEME);
|
||||
httpPostArr[i] = httpHost;
|
||||
}
|
||||
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(AuthScope.ANY,
|
||||
new UsernamePasswordCredentials(restProperties.getUsername(), restProperties.getPassword()));
|
||||
RestClientBuilder builder = RestClient.builder(httpPostArr)
|
||||
// 异步httpclient连接数配置
|
||||
.setHttpClientConfigCallback(httpClientBuilder -> {
|
||||
// 账号密码登录
|
||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
||||
// httpclient连接数配置
|
||||
httpClientBuilder.setMaxConnTotal(poolProperties.getMaxConnectNum())
|
||||
.setMaxConnPerRoute(poolProperties.getMaxConnectPerRoute());
|
||||
return httpClientBuilder;
|
||||
})
|
||||
// 异步httpclient连接延时配置
|
||||
.setRequestConfigCallback(requestConfigBuilder -> {
|
||||
requestConfigBuilder.setConnectTimeout(poolProperties.getConnectTimeOut())
|
||||
.setSocketTimeout(poolProperties.getSocketTimeOut())
|
||||
.setConnectionRequestTimeout(poolProperties.getConnectionRequestTimeOut());
|
||||
return requestConfigBuilder;
|
||||
});
|
||||
return new RestHighLevelClient(builder);
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* es的httpClient连接池配置
|
||||
@@ -18,6 +19,7 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.elasticsearch.rest-pool")
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class RestClientPoolProperties {
|
||||
/**
|
||||
* 链接建立超时时间
|
||||
|
||||
@@ -30,8 +30,8 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
|
||||
+21
-16
@@ -1,20 +1,21 @@
|
||||
package com.central.common.lb;
|
||||
|
||||
import com.central.common.lb.config.RestTemplateProperties;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.classic.HttpClient;
|
||||
import org.apache.hc.client5.http.config.ConnectionConfig;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.hc.core5.http.config.Registry;
|
||||
import org.apache.hc.core5.http.config.RegistryBuilder;
|
||||
import org.apache.hc.core5.util.Timeout;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
@@ -26,6 +27,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(RestTemplateProperties.class)
|
||||
public class RestTemplateAutoConfigure {
|
||||
@Autowired
|
||||
@@ -61,19 +63,22 @@ public class RestTemplateAutoConfigure {
|
||||
// 同路由并发数20
|
||||
connectionManager.setDefaultMaxPerRoute(restTemplateProperties.getMaxPerRoute());
|
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
ConnectionConfig connectConfig = ConnectionConfig.custom()
|
||||
// 读超时
|
||||
.setSocketTimeout(restTemplateProperties.getReadTimeout())
|
||||
.setSocketTimeout(Timeout.ofMicroseconds(restTemplateProperties.getReadTimeout()))
|
||||
// 链接超时
|
||||
.setConnectTimeout(restTemplateProperties.getConnectTimeout())
|
||||
.setConnectTimeout(Timeout.ofMicroseconds(restTemplateProperties.getConnectTimeout()))
|
||||
.build();
|
||||
connectionManager.setDefaultConnectionConfig(connectConfig);
|
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
// 链接不够用的等待时间
|
||||
.setConnectionRequestTimeout(restTemplateProperties.getReadTimeout())
|
||||
.setConnectionRequestTimeout(Timeout.ofMicroseconds(restTemplateProperties.getReadTimeout()))
|
||||
.build();
|
||||
|
||||
return HttpClientBuilder.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setConnectionManager(connectionManager)
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ package com.central.common.lb.config;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* RestTemplate 配置
|
||||
@@ -16,6 +17,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.rest-template")
|
||||
@Component
|
||||
public class RestTemplateProperties {
|
||||
/**
|
||||
* 最大链接数
|
||||
|
||||
@@ -18,15 +18,6 @@
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>transmittable-thread-local</artifactId>
|
||||
@@ -52,11 +43,6 @@
|
||||
<artifactId>spring-web</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
@@ -72,5 +58,9 @@
|
||||
<artifactId>feign-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
+1
-1
@@ -4,6 +4,7 @@ import com.central.log.annotation.AuditLog;
|
||||
import com.central.log.model.Audit;
|
||||
import com.central.log.properties.AuditLogProperties;
|
||||
import com.central.log.service.IAuditService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
@@ -20,7 +21,6 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 审计日志配置
|
||||
@@ -18,6 +19,7 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.audit-log")
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class AuditLogProperties {
|
||||
/**
|
||||
* 是否开启审计日志
|
||||
|
||||
+4
@@ -3,7 +3,9 @@ package com.central.log.properties;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 日志数据源配置
|
||||
@@ -18,5 +20,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.audit-log.datasource")
|
||||
@ConditionalOnClass(HikariConfig.class)
|
||||
@Component
|
||||
public class LogDbProperties extends HikariConfig {
|
||||
}
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 日志链路追踪配置
|
||||
@@ -15,6 +16,7 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.trace")
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class TraceProperties {
|
||||
/**
|
||||
* 是否开启日志链路追踪
|
||||
|
||||
+5
-5
@@ -2,16 +2,16 @@ package com.central.log.trace;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.log.properties.TraceProperties;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
|
||||
+91
-108
@@ -2,12 +2,10 @@ package org.slf4j;
|
||||
|
||||
import ch.qos.logback.classic.util.LogbackMDCAdapter;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import org.slf4j.helpers.ThreadLocalMapOfStacks;
|
||||
import org.slf4j.spi.MDCAdapter;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 重构{@link LogbackMDCAdapter}类,搭配TransmittableThreadLocal实现父子线程之间的数据传递
|
||||
@@ -16,17 +14,12 @@ import java.util.Set;
|
||||
* @date 2019/8/17
|
||||
*/
|
||||
public class TtlMDCAdapter implements MDCAdapter {
|
||||
private final ThreadLocal<Map<String, String>> copyOnInheritThreadLocal = new TransmittableThreadLocal<>();
|
||||
|
||||
private static final int WRITE_OPERATION = 1;
|
||||
private static final int MAP_COPY_OPERATION = 2;
|
||||
final ThreadLocal<Map<String, String>> readWriteThreadLocalMap = new TransmittableThreadLocal<>();
|
||||
final ThreadLocal<Map<String, String>> readOnlyThreadLocalMap = new TransmittableThreadLocal<>();
|
||||
private final ThreadLocalMapOfStacks threadLocalMapOfDeques = new ThreadLocalMapOfStacks();
|
||||
|
||||
private static TtlMDCAdapter mtcMDCAdapter;
|
||||
|
||||
/**
|
||||
* keeps track of the last operation performed
|
||||
*/
|
||||
private final ThreadLocal<Integer> lastOperation = new ThreadLocal<>();
|
||||
|
||||
static {
|
||||
mtcMDCAdapter = new TtlMDCAdapter();
|
||||
@@ -37,60 +30,39 @@ public class TtlMDCAdapter implements MDCAdapter {
|
||||
return mtcMDCAdapter;
|
||||
}
|
||||
|
||||
private Integer getAndSetLastOperation(int op) {
|
||||
Integer lastOp = lastOperation.get();
|
||||
lastOperation.set(op);
|
||||
return lastOp;
|
||||
}
|
||||
|
||||
private static boolean wasLastOpReadOrNull(Integer lastOp) {
|
||||
return lastOp == null || lastOp == MAP_COPY_OPERATION;
|
||||
}
|
||||
|
||||
private Map<String, String> duplicateAndInsertNewMap(Map<String, String> oldMap) {
|
||||
Map<String, String> newMap = Collections.synchronizedMap(new HashMap<>());
|
||||
if (oldMap != null) {
|
||||
// we don't want the parent thread modifying oldMap while we are
|
||||
// iterating over it
|
||||
synchronized (oldMap) {
|
||||
newMap.putAll(oldMap);
|
||||
}
|
||||
}
|
||||
|
||||
copyOnInheritThreadLocal.set(newMap);
|
||||
return newMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a context value (the <code>val</code> parameter) as identified with the
|
||||
* <code>key</code> parameter into the current thread's context map. Note that
|
||||
* contrary to log4j, the <code>val</code> parameter can be null.
|
||||
* <p/>
|
||||
* <p/>
|
||||
* If the current thread does not have a context map it is created as a side
|
||||
* effect of this call.
|
||||
*
|
||||
* @throws IllegalArgumentException in case the "key" parameter is null
|
||||
*/
|
||||
@Override
|
||||
public void put(String key, String val) {
|
||||
public void put(String key, String val) throws IllegalArgumentException {
|
||||
if (key == null) {
|
||||
throw new IllegalArgumentException("key cannot be null");
|
||||
}
|
||||
Map<String, String> current = readWriteThreadLocalMap.get();
|
||||
if (current == null) {
|
||||
current = new HashMap<>();
|
||||
readWriteThreadLocalMap.set(current);
|
||||
}
|
||||
|
||||
Map<String, String> oldMap = copyOnInheritThreadLocal.get();
|
||||
Integer lastOp = getAndSetLastOperation(WRITE_OPERATION);
|
||||
current.put(key, val);
|
||||
nullifyReadOnlyThreadLocalMap();
|
||||
}
|
||||
|
||||
if (wasLastOpReadOrNull(lastOp) || oldMap == null) {
|
||||
Map<String, String> newMap = duplicateAndInsertNewMap(oldMap);
|
||||
newMap.put(key, val);
|
||||
/**
|
||||
* Get the context identified by the <code>key</code> parameter.
|
||||
* <p/>
|
||||
* <p/>
|
||||
* This method has no side effects.
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
Map<String, String> hashMap = readWriteThreadLocalMap.get();
|
||||
|
||||
if ((hashMap != null) && (key != null)) {
|
||||
return hashMap.get(key);
|
||||
} else {
|
||||
oldMap.put(key, val);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the the context identified by the <code>key</code> parameter.
|
||||
* <p>Remove the context identified by the <code>key</code> parameter.
|
||||
* <p/>
|
||||
*/
|
||||
@Override
|
||||
@@ -98,53 +70,58 @@ public class TtlMDCAdapter implements MDCAdapter {
|
||||
if (key == null) {
|
||||
return;
|
||||
}
|
||||
Map<String, String> oldMap = copyOnInheritThreadLocal.get();
|
||||
if (oldMap == null) {
|
||||
return;
|
||||
|
||||
Map<String, String> current = readWriteThreadLocalMap.get();
|
||||
if (current != null) {
|
||||
current.remove(key);
|
||||
nullifyReadOnlyThreadLocalMap();
|
||||
}
|
||||
|
||||
Integer lastOp = getAndSetLastOperation(WRITE_OPERATION);
|
||||
|
||||
if (wasLastOpReadOrNull(lastOp)) {
|
||||
Map<String, String> newMap = duplicateAndInsertNewMap(oldMap);
|
||||
newMap.remove(key);
|
||||
} else {
|
||||
oldMap.remove(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void nullifyReadOnlyThreadLocalMap() {
|
||||
readOnlyThreadLocalMap.set(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all entries in the MDC.
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
lastOperation.set(WRITE_OPERATION);
|
||||
copyOnInheritThreadLocal.remove();
|
||||
readWriteThreadLocalMap.set(null);
|
||||
nullifyReadOnlyThreadLocalMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the context identified by the <code>key</code> parameter.
|
||||
* <p/>
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
final Map<String, String> map = copyOnInheritThreadLocal.get();
|
||||
if ((map != null) && (key != null)) {
|
||||
return map.get(key);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current thread's MDC as a map. This method is intended to be used
|
||||
* internally.
|
||||
* <p>Get the current thread's MDC as a map. This method is intended to be used
|
||||
* internally.</p>
|
||||
*
|
||||
* The returned map is unmodifiable (since version 1.3.2/1.4.2).
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, String> getPropertyMap() {
|
||||
lastOperation.set(MAP_COPY_OPERATION);
|
||||
return copyOnInheritThreadLocal.get();
|
||||
Map<String, String> readOnlyMap = readOnlyThreadLocalMap.get();
|
||||
if (readOnlyMap == null) {
|
||||
Map<String, String> current = readWriteThreadLocalMap.get();
|
||||
if (current != null) {
|
||||
final Map<String, String> tempMap = new HashMap<>(current);
|
||||
readOnlyMap = Collections.unmodifiableMap(tempMap);
|
||||
readOnlyThreadLocalMap.set(readOnlyMap);
|
||||
}
|
||||
}
|
||||
return readOnlyMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a copy of the current thread's context map. Returned value may be
|
||||
* null.
|
||||
*/
|
||||
public Map getCopyOfContextMap() {
|
||||
Map<String, String> readOnlyMap = getPropertyMap();
|
||||
if (readOnlyMap == null) {
|
||||
return null;
|
||||
} else {
|
||||
return new HashMap<>(readOnlyMap);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,37 +129,43 @@ public class TtlMDCAdapter implements MDCAdapter {
|
||||
* null.
|
||||
*/
|
||||
public Set<String> getKeys() {
|
||||
Map<String, String> map = getPropertyMap();
|
||||
Map<String, String> readOnlyMap = getPropertyMap();
|
||||
|
||||
if (map != null) {
|
||||
return map.keySet();
|
||||
if (readOnlyMap != null) {
|
||||
return readOnlyMap.keySet();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a copy of the current thread's context map. Returned value may be
|
||||
* null.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> getCopyOfContextMap() {
|
||||
Map<String, String> hashMap = copyOnInheritThreadLocal.get();
|
||||
if (hashMap == null) {
|
||||
return null;
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setContextMap(Map contextMap) {
|
||||
if (contextMap != null) {
|
||||
readWriteThreadLocalMap.set(new HashMap<String, String>(contextMap));
|
||||
} else {
|
||||
return new HashMap<>(hashMap);
|
||||
readWriteThreadLocalMap.set(null);
|
||||
}
|
||||
nullifyReadOnlyThreadLocalMap();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void pushByKey(String key, String value) {
|
||||
threadLocalMapOfDeques.pushByKey(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContextMap(Map<String, String> contextMap) {
|
||||
lastOperation.set(WRITE_OPERATION);
|
||||
public String popByKey(String key) {
|
||||
return threadLocalMapOfDeques.popByKey(key);
|
||||
}
|
||||
|
||||
Map<String, String> newMap = Collections.synchronizedMap(new HashMap<>());
|
||||
newMap.putAll(contextMap);
|
||||
@Override
|
||||
public Deque<String> getCopyOfDequeByKey(String key) {
|
||||
return threadLocalMapOfDeques.getCopyOfDequeByKey(key);
|
||||
}
|
||||
|
||||
// the newMap replaces the old one for serialisation's sake
|
||||
copyOnInheritThreadLocal.set(newMap);
|
||||
@Override
|
||||
public void clearDequeByKey(String key) {
|
||||
threadLocalMapOfDeques.clearDequeByKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
<!-- 控制台日志 -->
|
||||
<appender name="StdoutAppender" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<withJansi>true</withJansi>
|
||||
<encoder>
|
||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||
<charset>UTF-8</charset>
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ package com.central.oss.properties;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
@@ -14,6 +15,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = FileServerProperties.PREFIX)
|
||||
@Component
|
||||
public class FileServerProperties {
|
||||
public static final String PREFIX = "zlt.file-server";
|
||||
public static final String TYPE_FDFS = "fastdfs";
|
||||
|
||||
+6
-2
@@ -1,13 +1,15 @@
|
||||
package com.central.common.redis;
|
||||
|
||||
import com.central.common.redis.properties.CacheManagerProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
@@ -28,10 +30,12 @@ import java.util.Map;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableConfigurationProperties({RedisProperties.class, CacheManagerProperties.class})
|
||||
@EnableCaching
|
||||
public class RedisAutoConfigure {
|
||||
@Autowired
|
||||
@Resource
|
||||
private CacheManagerProperties cacheManagerProperties;
|
||||
|
||||
@Bean
|
||||
|
||||
+2
@@ -9,6 +9,7 @@ import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -25,6 +26,7 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
@ConditionalOnClass(RedissonClient.class)
|
||||
@ConditionalOnProperty(prefix = "zlt.lock", name = "lockerType", havingValue = "REDIS", matchIfMissing = true)
|
||||
@Component
|
||||
public class RedissonDistributedLock implements DistributedLock {
|
||||
@Autowired
|
||||
private RedissonClient redisson;
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ package com.central.common.redis.properties;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -13,6 +14,7 @@ import java.util.List;
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.cache-manager")
|
||||
@Component
|
||||
public class CacheManagerProperties {
|
||||
private List<CacheConfig> configs;
|
||||
|
||||
|
||||
+2
@@ -10,6 +10,7 @@ import org.springframework.data.redis.connection.RedisServerCommands;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.*;
|
||||
@@ -25,6 +26,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RedisRepository {
|
||||
/**
|
||||
* Spring Redis Template
|
||||
|
||||
+1
-3
@@ -1,4 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.common.redis.RedisAutoConfigure,\
|
||||
com.central.common.redis.lock.RedissonDistributedLock,\
|
||||
com.central.common.redis.template.RedisRepository
|
||||
com.central.common.redis.RedisAutoConfigure
|
||||
+1
-1
@@ -12,7 +12,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Sentinel配置类
|
||||
|
||||
+4
-4
@@ -58,7 +58,7 @@ public class SwaggerAutoConfiguration implements BeanFactoryAware {
|
||||
return new SwaggerProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
/*@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(name = "zlt.swagger.enabled", matchIfMissing = true)
|
||||
public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
|
||||
@@ -131,7 +131,7 @@ public class SwaggerAutoConfiguration implements BeanFactoryAware {
|
||||
docketList.add(docket);
|
||||
}
|
||||
return docketList;
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 创建 Docket对象
|
||||
@@ -139,7 +139,7 @@ public class SwaggerAutoConfiguration implements BeanFactoryAware {
|
||||
* @param swaggerProperties swagger配置
|
||||
* @return Docket
|
||||
*/
|
||||
private Docket createDocket(final SwaggerProperties swaggerProperties) {
|
||||
/*private Docket createDocket(final SwaggerProperties swaggerProperties) {
|
||||
ApiInfo apiInfo = new ApiInfoBuilder()
|
||||
.title(swaggerProperties.getTitle())
|
||||
.description(swaggerProperties.getDescription())
|
||||
@@ -184,7 +184,7 @@ public class SwaggerAutoConfiguration implements BeanFactoryAware {
|
||||
.build()
|
||||
.securitySchemes(securitySchemes())
|
||||
.securityContexts(securityContexts());
|
||||
}
|
||||
}*/
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -18,6 +19,7 @@ import java.util.Map;
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("zlt.swagger")
|
||||
@Component
|
||||
public class SwaggerProperties {
|
||||
/**是否开启swagger**/
|
||||
private Boolean enabled;
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ package com.central.common.zookeeper.properties;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* zookeeper配置
|
||||
@@ -17,6 +18,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.zookeeper")
|
||||
@Component
|
||||
public class ZookeeperProperty {
|
||||
/**
|
||||
* zk连接集群,多个用逗号隔开
|
||||
|
||||
Reference in New Issue
Block a user