Skip to content

Commit

Permalink
Merge pull request #13 from Frodez/0.3-alpha
Browse files Browse the repository at this point in the history
0.3 alpha
  • Loading branch information
Frodez authored Apr 19, 2019
2 parents 46fcb35 + 83e45d4 commit 40b745c
Show file tree
Hide file tree
Showing 41 changed files with 390 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.constraints.Positive;

/**
* 方法耗时监控注解
Expand All @@ -21,6 +22,7 @@
* @author Frodez
* @date 2019-04-13
*/
@Positive
long threshold() default 3000;

}
3 changes: 0 additions & 3 deletions src/main/java/frodez/config/aop/request/TimeoutAOP.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public class TimeoutAOP {
public Object process(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = MVCUtil.request();
TimeoutLock timeoutLock = AspectUtil.annotation(point, TimeoutLock.class);
if (timeoutLock.value() < 0) {
throw new IllegalArgumentException("the timeout can't be negative!");
}
String key = KeyGenerator.servletKey(AspectUtil.fullMethodName(point), request);
if (checker.check(key)) {
log.info("重复请求:IP地址{}", ServletUtil.getAddr(request));
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/frodez/config/aop/request/annotation/Limit.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.constraints.Positive;

/**
* 请求限流注解,只用于controller中的端点
Expand All @@ -21,13 +22,15 @@
* @author Frodez
* @date 2019-04-13
*/
@Positive
double value() default 100.0;

/**
* 超时时间,默认值3000毫秒
* @author Frodez
* @date 2019-04-13
*/
@Positive
long timeout() default 3000;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.constraints.Positive;

/**
* 控制重复请求注解(带过期时间),只用于controller中的端点
Expand All @@ -21,6 +22,7 @@
* @author Frodez
* @date 2019-04-13
*/
@Positive
long value() default 500;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import frodez.util.aop.AspectUtil;
import frodez.util.beans.result.Result;
import frodez.util.common.EmptyUtil;
import frodez.util.common.ValidationUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
Expand All @@ -28,7 +27,7 @@ public class ValidationAOP {
@Around("@annotation(frodez.config.aop.validation.annotation.Check)")
public Object validate(ProceedingJoinPoint p) throws Throwable {
String msg = ValidationUtil.validateParam(p.getTarget(), AspectUtil.method(p), p.getArgs());
return EmptyUtil.yes(msg) ? p.proceed() : Result.errorRequest(msg);
return msg == null ? p.proceed() : Result.errorRequest(msg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
*/
String regex();

/**
* 正则表达式的模式
* @see java.util.regex.Pattern
* @see javax.validation.constraints.Pattern.Flag
* @author Frodez
* @date 2019-04-18
*/
Flag[] flags() default {};

/**
Expand All @@ -57,6 +64,9 @@ class Validator implements ConstraintValidator<LegalStr, String> {
*/
private String regex;

/**
* 正则表达式的模式
*/
private int flag;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* 本包用于配置方法参数自动验证。<br>
* 使用hibernate-validation框架进行验证。<br>
* <strong>本包中的自定义注解使用后,相当于默认添加了@NotNull注解。如果需要关闭此功能,请在自定义注解的属性中设定nullable为true。</strong>
* @author Frodez
* @date 2019-03-11
*/
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/frodez/config/async/AsyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package frodez.config.async;

import frodez.util.spring.ContextUtil;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
* 异步配置
* @author Frodez
* @date 2019-04-15
*/
@Slf4j
@EnableAsync
@Configuration
public class AsyncConfig {

@Bean
public Executor getAsyncExecutor() {
AsyncProperties properties = ContextUtil.get(AsyncProperties.class);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
int availableProcessors = Runtime.getRuntime().availableProcessors();
int corePoolSize = Math.round(availableProcessors * properties.getCoreThreadTimes());
int maxPoolSize = Math.round(availableProcessors * properties.getMaxThreadTimes());
int queueCapacity = Math.round(maxPoolSize * properties.getQueueFactors());
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(properties.getKeepAliveSeconds());
executor.setThreadNamePrefix(properties.getThreadNamePrefix());
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
log.info("async executor is running now!");
log.info("async config:{}corePoolSize, {}maxPoolSize, {}queueCapacity, {}keepAliveSeconds, {}threadNamePrefix",
corePoolSize, maxPoolSize, queueCapacity, properties.getKeepAliveSeconds(), properties
.getThreadNamePrefix());
return executor;
}

}
44 changes: 44 additions & 0 deletions src/main/java/frodez/config/async/AsyncProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package frodez.config.async;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
* 异步任务配置
* @author Frodez
* @date 2019-04-15
*/
@Data
@Component
@PropertySource(value = { "classpath:settings/${spring.profiles.active}/async.properties" })
@ConfigurationProperties(prefix = "async")
public class AsyncProperties {

/**
* 核心线程基数,实际数量等于系统环境可用核心数乘以该基数并四舍五入。
*/
private float coreThreadTimes = 0.5F;

/**
* 最大线程基数,实际数量等于系统环境可用核心数乘以该基数并四舍五入。
*/
private float maxThreadTimes = 1.0F;

/**
* 队列规模因子,队列最大长度等于计算出的最大线程数乘以规模因子并四舍五入。
*/
private float queueFactors = 16.0F;

/**
* 线程最长活跃时间,单位为秒
*/
private int keepAliveSeconds = 60;

/**
* 线程名前缀
*/
private String threadNamePrefix = "async";

}
33 changes: 33 additions & 0 deletions src/main/java/frodez/config/cache/CacheProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,82 @@
@ConfigurationProperties(prefix = "cache")
public class CacheProperties {

/**
* 标准配置
*/
private StandardProperties standard = new StandardProperties();

/**
* AutoGuavaChecker配置
*/
private AutoGuavaCheckerProperties autoGuavaChecker = new AutoGuavaCheckerProperties();

/**
* ManualGuavaChecker配置
*/
private ManualGuavaCheckerProperties manualGuavaChecker = new ManualGuavaCheckerProperties();

/**
* LimitUserGuavaChecker配置
*/
private LimitUserGuavaCheckerProperties limitUserGuavaChecker = new LimitUserGuavaCheckerProperties();

/**
* URLMatcher配置
*/
private URLMatcherProperties urlMatcher = new URLMatcherProperties();

@Data
public static class StandardProperties {

/**
* 超时时间,单位毫秒
*/
private Integer timeout = 60000;

}

@Data
public static class AutoGuavaCheckerProperties {

/**
* 超时时间,单位毫秒
*/
private Integer timeout = 60000;

}

@Data
public static class ManualGuavaCheckerProperties {

/**
* 超时时间,单位毫秒
*/
private Integer timeout = 60000;

}

@Data
public static class LimitUserGuavaCheckerProperties {

/**
* 超时时间,单位毫秒
*/
private Integer timeout = 60000;

}

@Data
public static class URLMatcherProperties {

/**
* 超时时间,单位毫秒
*/
private Integer timeout = 3600000;

/**
* 缓存最大容量
*/
private Integer maxSize = 65536;

}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/frodez/config/font/FontProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
@ConfigurationProperties(prefix = "font")
public class FontProperties {

/**
* 字体存放路径
*/
private String path = "";

/**
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/frodez/config/mvc/JsonHttpMessageConverer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import frodez.util.constant.setting.DefCharset;
import frodez.util.json.JSONUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -134,12 +135,14 @@ public Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
try {
if (object instanceof Result) {
outputMessage.getBody().write(object.toString().getBytes());
outputMessage.getBody().flush();
OutputStream outputStream = outputMessage.getBody();
if (object.getClass() == Result.class) {
//对通用Result采用特殊的优化过的方式
outputStream.write(object.toString().getBytes());
outputStream.flush();
} else {
outputMessage.getBody().write(JSONUtil.string(object).getBytes());
outputMessage.getBody().flush();
outputStream.write(JSONUtil.string(object).getBytes());
outputStream.flush();
}
} catch (InvalidDefinitionException ex) {
throw new HttpMessageConversionException(StrUtil.concat("Type definition error: ", ex.getType().toString()),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/frodez/controller/user/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public Result out() {
* @author Frodez
* @date 2019-02-27
*/
@GetMapping("/userInfo")
public Result getUserInfo(@RequestParam("userName") String userName) {
@GetMapping("/test")
public Result test(@RequestParam("userName") String userName) {
return authorityService.getUserInfo(userName);
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/frodez/controller/user/RoleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.concurrent.ExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -46,6 +47,8 @@ public Result getRole(@RequestParam("id") @ApiParam(value = "角色ID", required
/**
* 分页查询角色信息
* @author Frodez
* @throws InterruptedException
* @throws ExecutionException
* @date 2019-03-06
*/
@GetMapping("/page")
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/frodez/dao/mapper/user/RolePermissionMapper.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="frodez.dao.mapper.user.RolePermissionMapper">
<select id="getPermissions" resultType="frodez.dao.result.user.PermissionInfo">

<select id="getPermissions" resultType="frodez.dao.result.user.PermissionInfo">
SELECT tb_permission.id AS id, tb_permission.type AS type,
tb_permission.`name` AS `name`, tb_permission.url AS url,
tb_permission.description AS description
Expand All @@ -11,15 +11,19 @@
tb_role_permission.permission_id = tb_permission.id AND
tb_role_permission.role_id = #{roleId}
</select>

<select id="batchGetPermissions" resultType="frodez.util.beans.pair.Pair">
SELECT tb_role_permission.role_id AS key, tb_role_permission.permission_id AS value
SELECT
tb_role_permission.role_id AS `KEY`,
tb_role_permission.permission_id AS `VALUE`
FROM
tb_role_permission.permission_id = tb_permission.id AND
tb_role_permission
INNER JOIN tb_permission ON tb_role_permission.permission_id = tb_permission.id
WHERE
tb_role_permission.role_id in
<foreach collection="roleIds" item="item" index="index">
<foreach collection="roleIds" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public class BaseTaskService {
@PostConstruct
private void init() {
try {
scheduler.setWaitForTasksToCompleteOnShutdown(true);
taskServiceInfos = ContextUtil.gets(ITask.class).entrySet().stream().map((entry) -> {
AvailableTaskInfo info = new AvailableTaskInfo();
info.setName(entry.getKey());
Expand Down
Loading

0 comments on commit 40b745c

Please sign in to comment.