From 668a022a073eb3fe21ee558d29c62d0439334a7e Mon Sep 17 00:00:00 2001 From: Jesse-Ma <24167796@qq.com> Date: Thu, 25 May 2023 09:35:17 +0800 Subject: [PATCH] spring boot 3.06 --- pom.xml | 17 +- .../config/ApiLimiterConfiguration.java | 8 +- .../config/BizKeyLoadBalancerClient.java | 47 +++-- .../config/BkrLoadBalancerConfiguration.java | 12 +- .../flagnote/gateway/config/GwCorsFilter.java | 43 ++-- .../gateway/config/WebFluxWebConfig.java | 8 +- .../filter/ValidateNoteCipherFilter.java | 10 +- .../gateway/filter/ValidateNoteKeyFilter.java | 4 +- ...alidateNoteCipherGatewayFilterFactory.java | 14 +- .../ValidateNoteKeyGatewayFilterFactory.java | 18 +- .../flagnote/gateway/utils/BizKeyUtils.java | 11 +- .../com/flagnote/gateway/utils/JsonUtils.java | 183 +++++++++--------- .../com/flagnote/gateway/utils/NoteMeta.java | 25 +-- .../flagnote/gateway/utils/RandomUtils.java | 14 +- src/main/resources/bootstrap.yml | 5 +- src/main/resources/logback-spring.xml | 35 ++++ 16 files changed, 232 insertions(+), 222 deletions(-) create mode 100644 src/main/resources/logback-spring.xml diff --git a/pom.xml b/pom.xml index 7431d4a..45f573d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 3.0.5 + 3.0.6 com.flagnote @@ -20,7 +20,8 @@ 17 17 2022.0.2 - flagnote + registry.openif.com:5000 + flagnote @@ -56,7 +57,7 @@ cn.hutool hutool-all - 5.7.22 + 5.8.18 org.springframework.boot @@ -117,11 +118,15 @@ com.spotify docker-maven-plugin + 1.2.2 - false - http://rancher:42375 + docker-openif + http://${docker.repository} + true + http://144.34.221.20:42375 - ${docker.image.prefix}/${project.artifactId}:${project.version} + ${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version} + src/main/docker diff --git a/src/main/java/com/flagnote/gateway/config/ApiLimiterConfiguration.java b/src/main/java/com/flagnote/gateway/config/ApiLimiterConfiguration.java index 3a97d3b..811e593 100644 --- a/src/main/java/com/flagnote/gateway/config/ApiLimiterConfiguration.java +++ b/src/main/java/com/flagnote/gateway/config/ApiLimiterConfiguration.java @@ -10,18 +10,18 @@ import reactor.core.publisher.Mono; @Configuration public class ApiLimiterConfiguration { - @Bean(name="remoteSessionKeyResolver") + @Bean(name = "remoteSessionKeyResolver") public KeyResolver remoteSessionKeyResolver() { return exchange -> Mono.just(exchange.getSession().block().getId()); } - - @Bean(name="noteKeyResolver") + + @Bean(name = "noteKeyResolver") public KeyResolver noteKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getPath().toString()); } @Primary - @Bean(name="remoteAddrKeyResolver") + @Bean(name = "remoteAddrKeyResolver") public KeyResolver remoteAddrKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()); } diff --git a/src/main/java/com/flagnote/gateway/config/BizKeyLoadBalancerClient.java b/src/main/java/com/flagnote/gateway/config/BizKeyLoadBalancerClient.java index 6464f1f..827e7dd 100644 --- a/src/main/java/com/flagnote/gateway/config/BizKeyLoadBalancerClient.java +++ b/src/main/java/com/flagnote/gateway/config/BizKeyLoadBalancerClient.java @@ -5,8 +5,6 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.ObjectProvider; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.DefaultResponse; @@ -16,7 +14,6 @@ import org.springframework.cloud.client.loadbalancer.RequestData; import org.springframework.cloud.client.loadbalancer.RequestDataContext; import org.springframework.cloud.client.loadbalancer.Response; import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier; -import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer; import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer; import org.springframework.cloud.loadbalancer.core.SelectedInstanceCallback; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; @@ -28,17 +25,17 @@ import reactor.core.publisher.Mono; @Slf4j public class BizKeyLoadBalancerClient implements ReactorServiceInstanceLoadBalancer { - + private final String serviceId; - - private ObjectProvider serviceInstanceListSupplierProvider; - + + private ObjectProvider serviceInstanceListSupplierProvider; + public BizKeyLoadBalancerClient(ObjectProvider serviceInstanceListSupplierProvider, String serviceId) { this.serviceId = serviceId; this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider; } - + @SuppressWarnings("rawtypes") @Override public Mono> choose(Request request) { @@ -47,7 +44,7 @@ public class BizKeyLoadBalancerClient implements ReactorServiceInstanceLoadBalan return supplier.get(request).next() .map(serviceInstances -> processInstanceResponse(request, supplier, serviceInstances)); } - + @SuppressWarnings("rawtypes") private Response processInstanceResponse(Request request, ServiceInstanceListSupplier supplier, List serviceInstances) { @@ -57,7 +54,7 @@ public class BizKeyLoadBalancerClient implements ReactorServiceInstanceLoadBalan } return serviceInstanceResponse; } - + @SuppressWarnings("rawtypes") private Response getInstanceResponse(Request request, List instances) { if (instances.isEmpty()) { @@ -66,25 +63,25 @@ public class BizKeyLoadBalancerClient implements ReactorServiceInstanceLoadBalan } return new EmptyResponse(); } - - RequestDataContext rdContext = (RequestDataContext)request.getContext(); + + RequestDataContext rdContext = (RequestDataContext) request.getContext(); RequestData rdata = rdContext.getClientRequest(); String path = rdata.getUrl().getPath(); - + String pattern = "^/note/([abcdefhikmnopqstuvwxyz23456789]{16})(\\.txt|/noteMeta|/delete)?$"; - Pattern pt = Pattern.compile(pattern); - Matcher m = pt.matcher(path); - - //默认随机 - int index = ThreadLocalRandom.current().nextInt(instances.size()); - - //如果有key,则为key分配指定服务。 - if(m.find()) { - String mixKey = BizKeyUtils.mixKey(m.group(1)); - index = Math.abs(mixKey.hashCode())%instances.size(); - } + Pattern pt = Pattern.compile(pattern); + Matcher m = pt.matcher(path); + + // 默认随机 + int index = ThreadLocalRandom.current().nextInt(instances.size()); + + // 如果有key,则为key分配指定服务。 + if (m.find()) { + String mixKey = BizKeyUtils.mixKey(m.group(1)); + index = Math.abs(mixKey.hashCode()) % instances.size(); + } ServiceInstance instance = instances.get(index); - log.info("path:{},service:{}",path,instance.getUri().toString()); + log.info("path:{},service:{}", path, instance.getUri().toString()); return new DefaultResponse(instance); } diff --git a/src/main/java/com/flagnote/gateway/config/BkrLoadBalancerConfiguration.java b/src/main/java/com/flagnote/gateway/config/BkrLoadBalancerConfiguration.java index 03046ae..6683f30 100644 --- a/src/main/java/com/flagnote/gateway/config/BkrLoadBalancerConfiguration.java +++ b/src/main/java/com/flagnote/gateway/config/BkrLoadBalancerConfiguration.java @@ -9,9 +9,11 @@ import org.springframework.core.env.Environment; //@Configuration 必须注释掉 public class BkrLoadBalancerConfiguration { - @Bean - public ReactorLoadBalancer reactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) { - String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); - return new BizKeyLoadBalancerClient(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name); - } + @Bean + public ReactorLoadBalancer reactorServiceInstanceLoadBalancer(Environment environment, + LoadBalancerClientFactory loadBalancerClientFactory) { + String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); + return new BizKeyLoadBalancerClient( + loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name); + } } diff --git a/src/main/java/com/flagnote/gateway/config/GwCorsFilter.java b/src/main/java/com/flagnote/gateway/config/GwCorsFilter.java index c744e44..a63aa0f 100644 --- a/src/main/java/com/flagnote/gateway/config/GwCorsFilter.java +++ b/src/main/java/com/flagnote/gateway/config/GwCorsFilter.java @@ -1,7 +1,5 @@ package com.flagnote.gateway.config; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; @@ -11,28 +9,27 @@ import org.springframework.web.util.pattern.PathPatternParser; public class GwCorsFilter { // @Bean - public CorsWebFilter corsFilter() { - CorsConfiguration config = new CorsConfiguration(); - config.addAllowedMethod("*"); - config.addAllowedOrigin("*"); - config.addAllowedHeader("*"); + public CorsWebFilter corsFilter() { + CorsConfiguration config = new CorsConfiguration(); + config.addAllowedMethod("*"); + config.addAllowedOrigin("*"); + config.addAllowedHeader("*"); - config.setAllowCredentials(true); // 允许cookies跨域 - config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin - config.addAllowedHeader("*");// #允许访问的头信息,*表示全部 - config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了 - config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许 - config.addAllowedMethod("HEAD"); - config.addAllowedMethod("GET"); - config.addAllowedMethod("PUT"); - config.addAllowedMethod("POST"); - config.addAllowedMethod("DELETE"); - config.addAllowedMethod("PATCH"); + config.setAllowCredentials(true); // 允许cookies跨域 + config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin + config.addAllowedHeader("*");// #允许访问的头信息,*表示全部 + config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了 + config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许 + config.addAllowedMethod("HEAD"); + config.addAllowedMethod("GET"); + config.addAllowedMethod("PUT"); + config.addAllowedMethod("POST"); + config.addAllowedMethod("DELETE"); + config.addAllowedMethod("PATCH"); - UrlBasedCorsConfigurationSource source = - new UrlBasedCorsConfigurationSource(new PathPatternParser()); - source.registerCorsConfiguration("/**", config); + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); + source.registerCorsConfiguration("/**", config); - return new CorsWebFilter(source); - } + return new CorsWebFilter(source); + } } \ No newline at end of file diff --git a/src/main/java/com/flagnote/gateway/config/WebFluxWebConfig.java b/src/main/java/com/flagnote/gateway/config/WebFluxWebConfig.java index 7e76951..a044e77 100644 --- a/src/main/java/com/flagnote/gateway/config/WebFluxWebConfig.java +++ b/src/main/java/com/flagnote/gateway/config/WebFluxWebConfig.java @@ -8,8 +8,8 @@ import org.springframework.web.reactive.config.WebFluxConfigurer; @Configuration @EnableWebFlux public class WebFluxWebConfig implements WebFluxConfigurer { - @Override - public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) { - configurer.defaultCodecs().maxInMemorySize(10 * 1024 * 1024); - } + @Override + public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) { + configurer.defaultCodecs().maxInMemorySize(10 * 1024 * 1024); + } } diff --git a/src/main/java/com/flagnote/gateway/filter/ValidateNoteCipherFilter.java b/src/main/java/com/flagnote/gateway/filter/ValidateNoteCipherFilter.java index 9c126cd..9b0d2db 100644 --- a/src/main/java/com/flagnote/gateway/filter/ValidateNoteCipherFilter.java +++ b/src/main/java/com/flagnote/gateway/filter/ValidateNoteCipherFilter.java @@ -12,7 +12,6 @@ import org.springframework.web.server.ServerWebExchange; import com.flagnote.gateway.utils.BizKeyUtils; -import cn.hutool.json.JSON; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import reactor.core.publisher.Mono; @@ -30,18 +29,17 @@ public class ValidateNoteCipherFilter implements GatewayFilter, Ordered { String cipher = body.getStr("cipher"); String initTime = body.getStr("initTime"); String key = body.getStr("key"); - + if (!noteKey.equals(key)) { exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); return exchange.getResponse().setComplete(); } - - if (!BizKeyUtils.validateCipher(noteKey,initTime, cipher)) { + + if (!BizKeyUtils.validateCipher(noteKey, initTime, cipher)) { exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); return exchange.getResponse().setComplete(); } - - + return chain.filter(exchange); } diff --git a/src/main/java/com/flagnote/gateway/filter/ValidateNoteKeyFilter.java b/src/main/java/com/flagnote/gateway/filter/ValidateNoteKeyFilter.java index 65abcfc..39c6b20 100644 --- a/src/main/java/com/flagnote/gateway/filter/ValidateNoteKeyFilter.java +++ b/src/main/java/com/flagnote/gateway/filter/ValidateNoteKeyFilter.java @@ -8,7 +8,6 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.core.Ordered; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; @@ -33,7 +32,8 @@ public class ValidateNoteKeyFilter implements GatewayFilter, Ordered { if (!BizKeyUtils.validateKey(noteKey)) { NoteMeta meta = new NoteMeta(); - DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(JsonUtils.toJson(meta).getBytes(StandardCharsets.UTF_8)); + DataBuffer dataBuffer = exchange.getResponse().bufferFactory() + .wrap(JsonUtils.toJson(meta).getBytes(StandardCharsets.UTF_8)); return exchange.getResponse().writeWith(Flux.just(dataBuffer)); } diff --git a/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteCipherGatewayFilterFactory.java b/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteCipherGatewayFilterFactory.java index 08503a8..7780b98 100644 --- a/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteCipherGatewayFilterFactory.java +++ b/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteCipherGatewayFilterFactory.java @@ -1,22 +1,13 @@ package com.flagnote.gateway.filter.factory; -import java.net.URI; -import java.util.Arrays; -import java.util.List; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.gateway.filter.GatewayFilter; -import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; -import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory.NameConfig; -import org.springframework.cloud.gateway.support.GatewayToStringStyler; import org.springframework.stereotype.Component; -import org.springframework.web.server.ServerWebExchange; import com.flagnote.gateway.filter.ValidateNoteCipherFilter; import lombok.extern.slf4j.Slf4j; -import reactor.core.publisher.Mono; @Slf4j @Component @@ -25,15 +16,14 @@ public class ValidateNoteCipherGatewayFilterFactory @Autowired private ValidateNoteCipherFilter validateNoteCipherFilter; - - + public ValidateNoteCipherGatewayFilterFactory() { super(NameConfig.class); } @Override public GatewayFilter apply(NameConfig config) { - + return validateNoteCipherFilter; } } \ No newline at end of file diff --git a/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteKeyGatewayFilterFactory.java b/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteKeyGatewayFilterFactory.java index cd9ad08..b6648d6 100644 --- a/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteKeyGatewayFilterFactory.java +++ b/src/main/java/com/flagnote/gateway/filter/factory/ValidateNoteKeyGatewayFilterFactory.java @@ -1,22 +1,13 @@ package com.flagnote.gateway.filter.factory; -import java.net.URI; -import java.util.Arrays; -import java.util.List; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.gateway.filter.GatewayFilter; -import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; -import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory.NameConfig; -import org.springframework.cloud.gateway.support.GatewayToStringStyler; import org.springframework.stereotype.Component; -import org.springframework.web.server.ServerWebExchange; import com.flagnote.gateway.filter.ValidateNoteKeyFilter; import lombok.extern.slf4j.Slf4j; -import reactor.core.publisher.Mono; @Slf4j @Component @@ -25,8 +16,7 @@ public class ValidateNoteKeyGatewayFilterFactory @Autowired private ValidateNoteKeyFilter validateNoteKeyFilter; - - + public ValidateNoteKeyGatewayFilterFactory() { super(NameConfig.class); } @@ -35,12 +25,12 @@ public class ValidateNoteKeyGatewayFilterFactory // public List shortcutFieldOrder() { // return Arrays.asList("name"); // } - + @Override public GatewayFilter apply(NameConfig config) { - + return validateNoteKeyFilter; - + // return new GatewayFilter() { // @Override // public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { diff --git a/src/main/java/com/flagnote/gateway/utils/BizKeyUtils.java b/src/main/java/com/flagnote/gateway/utils/BizKeyUtils.java index 119a22b..508c7e9 100644 --- a/src/main/java/com/flagnote/gateway/utils/BizKeyUtils.java +++ b/src/main/java/com/flagnote/gateway/utils/BizKeyUtils.java @@ -84,14 +84,13 @@ public class BizKeyUtils { return Integer.parseInt(String.valueOf(new Date().getTime()).substring(0, 4)); } - public static String getCipher(String key,String initTime) { - return md5(key +"#"+ MIX_STRING + "#" + initTime); + public static String getCipher(String key, String initTime) { + return md5(key + "#" + MIX_STRING + "#" + initTime); } - - public static Boolean validateCipher(String key,String initTime,String cipher) { - return md5(key +"#"+ MIX_STRING + "#" + initTime).equals(cipher); + + public static Boolean validateCipher(String key, String initTime, String cipher) { + return md5(key + "#" + MIX_STRING + "#" + initTime).equals(cipher); } - public static String getSecretKey(String key, String password) { return md5(key + md5(MIX_STRING + password)); diff --git a/src/main/java/com/flagnote/gateway/utils/JsonUtils.java b/src/main/java/com/flagnote/gateway/utils/JsonUtils.java index d2530b3..5bc1dca 100644 --- a/src/main/java/com/flagnote/gateway/utils/JsonUtils.java +++ b/src/main/java/com/flagnote/gateway/utils/JsonUtils.java @@ -14,99 +14,96 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import lombok.extern.slf4j.Slf4j; - + @Slf4j public class JsonUtils { - - private static ObjectMapper om = new ObjectMapper(); - - static { - - // 对象的所有字段全部列入,还是其他的选项,可以忽略null等 - om.setSerializationInclusion(Include.ALWAYS); - // 设置Date类型的序列化及反序列化格式 - om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); - - // 忽略空Bean转json的错误 - om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); - // 忽略未知属性,防止json字符串中存在,java对象中不存在对应属性的情况出现错误 - om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - // 注册一个时间序列化及反序列化的处理模块,用于解决jdk8中localDateTime等的序列化问题 - om.registerModule(new JavaTimeModule()); - } - - /** - * 对象 => json字符串 - * - * @param obj 源对象 - */ - public static String toJson(T obj) { - - String json = null; - if (obj != null) { - try { - json = om.writeValueAsString(obj); - } catch (JsonProcessingException e) { - log.warn(e.getMessage(), e); - throw new IllegalArgumentException(e.getMessage()); - } - } - return json; - } - - /** - * json字符串 => 对象 - * - * @param json 源json串 - * @param clazz 对象类 - * @param 泛型 - */ - public static T parse(String json, Class clazz) { - - return parse(json, clazz, null); - } - - /** - * json字符串 => 对象 - * - * @param json 源json串 - * @param type 对象类型 - * @param 泛型 - */ - public static T parse(String json, TypeReference type) { - - return parse(json, null, type); - } - - - /** - * json => 对象处理方法 - *
- * 参数clazz和type必须一个为null,另一个不为null - *
- * 此方法不对外暴露,访问权限为private - * - * @param json 源json串 - * @param clazz 对象类 - * @param type 对象类型 - * @param 泛型 - */ - private static T parse(String json, Class clazz, TypeReference type) { - - T obj = null; - if (StringUtils.hasLength(json)) { - try { - if (clazz != null) { - obj = om.readValue(json, clazz); - } else { - obj = om.readValue(json, type); - } - } catch (IOException e) { - log.warn(e.getMessage(), e); - throw new IllegalArgumentException(e.getMessage()); - } - } - return obj; - } + + private static ObjectMapper om = new ObjectMapper(); + + static { + + // 对象的所有字段全部列入,还是其他的选项,可以忽略null等 + om.setSerializationInclusion(Include.ALWAYS); + // 设置Date类型的序列化及反序列化格式 + om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); + + // 忽略空Bean转json的错误 + om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + // 忽略未知属性,防止json字符串中存在,java对象中不存在对应属性的情况出现错误 + om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + // 注册一个时间序列化及反序列化的处理模块,用于解决jdk8中localDateTime等的序列化问题 + om.registerModule(new JavaTimeModule()); + } + + /** + * 对象 => json字符串 + * + * @param obj 源对象 + */ + public static String toJson(T obj) { + + String json = null; + if (obj != null) { + try { + json = om.writeValueAsString(obj); + } catch (JsonProcessingException e) { + log.warn(e.getMessage(), e); + throw new IllegalArgumentException(e.getMessage()); + } + } + return json; + } + + /** + * json字符串 => 对象 + * + * @param json 源json串 + * @param clazz 对象类 + * @param 泛型 + */ + public static T parse(String json, Class clazz) { + + return parse(json, clazz, null); + } + + /** + * json字符串 => 对象 + * + * @param json 源json串 + * @param type 对象类型 + * @param 泛型 + */ + public static T parse(String json, TypeReference type) { + + return parse(json, null, type); + } + + /** + * json => 对象处理方法
+ * 参数clazz和type必须一个为null,另一个不为null
+ * 此方法不对外暴露,访问权限为private + * + * @param json 源json串 + * @param clazz 对象类 + * @param type 对象类型 + * @param 泛型 + */ + private static T parse(String json, Class clazz, TypeReference type) { + + T obj = null; + if (StringUtils.hasLength(json)) { + try { + if (clazz != null) { + obj = om.readValue(json, clazz); + } else { + obj = om.readValue(json, type); + } + } catch (IOException e) { + log.warn(e.getMessage(), e); + throw new IllegalArgumentException(e.getMessage()); + } + } + return obj; + } } \ No newline at end of file diff --git a/src/main/java/com/flagnote/gateway/utils/NoteMeta.java b/src/main/java/com/flagnote/gateway/utils/NoteMeta.java index 0d08a47..7a9aed4 100644 --- a/src/main/java/com/flagnote/gateway/utils/NoteMeta.java +++ b/src/main/java/com/flagnote/gateway/utils/NoteMeta.java @@ -5,25 +5,26 @@ import java.io.Serializable; import lombok.Data; @Data -public class NoteMeta implements Serializable {/** - * - */ +public class NoteMeta implements Serializable { + /** + * + */ private static final long serialVersionUID = -8234044213813670440L; -/** - * - */ - + /** + * + */ + private String key; - + private Integer state; - + private String cipher; - + private Integer lock; - + private String md5; - + private Long serverTime; private Long ttl; diff --git a/src/main/java/com/flagnote/gateway/utils/RandomUtils.java b/src/main/java/com/flagnote/gateway/utils/RandomUtils.java index 86752fe..fec8e3d 100644 --- a/src/main/java/com/flagnote/gateway/utils/RandomUtils.java +++ b/src/main/java/com/flagnote/gateway/utils/RandomUtils.java @@ -6,15 +6,15 @@ import java.security.SecureRandom; public class RandomUtils { public static SecureRandom sr = null; - + static { - try { - sr = SecureRandom.getInstanceStrong(); - } catch (NoSuchAlgorithmException e) { - sr = new SecureRandom(); - } + try { + sr = SecureRandom.getInstanceStrong(); + } catch (NoSuchAlgorithmException e) { + sr = new SecureRandom(); + } } - + public static Integer nextInt(Integer num) { return sr.nextInt(num); } diff --git a/src/main/resources/bootstrap.yml b/src/main/resources/bootstrap.yml index 32572bb..7479ad4 100644 --- a/src/main/resources/bootstrap.yml +++ b/src/main/resources/bootstrap.yml @@ -5,7 +5,6 @@ spring: config: name: flagnote-gateway profile: prd - uri: http://flagnote-config-01:8080/flagnote-config,http://flagnote-config-02:8080/flagnote-config + uri: http://flagnote-config-01:8080/flagnote-config config: - import: optional:configserver:http://flagnote-config-01:8080/,optional:configserver:http://flagnote-config-02:8080/ - + import: optional:configserver:http://flagnote-config-01:8080/ diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..338f084 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,35 @@ + + + + + + + + + ${pattern} + ${charsetEncoding} + + + + true + + ${pattern} + ${charsetEncoding} + + + + ${LOG_HOME}${file.separator}%d{yyMMdd}_info_%i.log + + 30 + 20MB + + + INFO + + + + + + + + \ No newline at end of file