作为一名合格的开发者学会整理,灵活使用成熟、稳定的开源项目,不要重复造轮子,要站在巨人的肩膀上去工作。
一、日志框架
slf4j-spring-boot-starter
项目上线后,通常都会添加日志打印,方便后期维护排查问题,一般情况我们都会在每个功能模块,添加日志输出代码,确实比较繁琐。
日志框架集成后实现效果
@RestController
public class TestParamLogController {
@ParamLog(value = "ParamLog-test1")
@GetMapping("/paramLogTest1")
public Map<String, Object> logTest1(HttpServletRequest request, Map<String, Object> param) {
Map<String, Object> result = new HashMap<>(3);
result.put("code", 200);
result.put("msg", "success");
result.put("data", param);
return result;
}
@ParamLog(value = "ParamLog-test2", paramFilter = {"request"})
@GetMapping("/paramLogTest2")
public Map<String, Object> logTest2(HttpServletRequest request, Map<String, Object> param) {
Map<String, Object> result = new HashMap<>(3);
result.put("code", 200);
result.put("msg", "success");
result.put("data", param);
return result;
}
@ParamLog(value = "ParamLog-test3", paramFilter = {"request"}, callback = LogTestCallback.class)
@GetMapping("/paramLogTest3")
public Map<String, Object> logTest3(HttpServletRequest request, List<Object> param) {
Map<String, Object> result = new HashMap<>(3);
result.put("code", 200);
result.put("msg", "success");
result.put("data", param);
return result;
}
}
控制台输出效果
2020-01-16 21:51:11.932 DEBUG 27340 --- [ main] wiki.xsx.core.log.LogProcessor : 调用方法:【wiki.xsx.log.controller.TestParamLogController.logTest1(TestParamLogController.java:23)】,业务名称:【ParamLog-test1】,接收参数:【{request=null, param={key1=hello-world, key2=11, key3=11.11, key4=[hello, world], key5=[111, 112, 113]}}】
2020-01-16 21:51:11.939 DEBUG 27340 --- [ main] wiki.xsx.core.log.LogProcessor : 调用方法:【wiki.xsx.log.controller.TestParamLogController.logTest2(TestParamLogController.java:33)】,业务名称:【ParamLog-test2】,接收参数:【{param={key1=hello-world, key2=11, key3=11.11, key4=[hello, world], key5=[111, 112, 113]}}】
2020-01-16 21:51:11.940 DEBUG 27340 --- [ main] wiki.xsx.core.log.LogProcessor : 调用方法:【wiki.xsx.log.controller.TestParamLogController.logTest3(TestParamLogController.java:43)】,业务名称:【ParamLog-test3】,接收参数:【{param=[test-list, {key1=hello-world, key2=11, key3=11.11, key4=[hello, world], key5=[111, 112, 113]}]}】
2020-01-16 21:51:11.941 INFO 27340 --- [ main] wiki.xsx.log.controller.LogTestCallback : wiki.xsx.log.controller.TestParamLogController.logTest3方法的回调函数执行成功
发现通过简单的注解,可以搞定日志打印效果,避免了编写繁琐的日志代码
二、AsyncTool 京东多线程并行框架
该框架目前正在 京东App后台 接受苛刻、高并发、海量用户等复杂场景业务的检验测试,随时会根据实际情况发布更新和bugFix。
maven依赖
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.gitee.jd-platform-opensource</groupId>
<artifactId>asyncTool</artifactId>
<version> V1.3-6a77b0976d-1</version>
</dependency>
三、Retrofit 接口请求框架
适用于retrofit的spring-boot-starter,支持快速集成和功能增强。
代码示例
@RetrofitClient(baseUrl = "${test.baseUrl}")
public interface HttpApi {
@GET("person")
Result<Person> getPerson(@Query("id") Long id);
}
@Service
public class TestService {
@Autowired
private HttpApi httpApi;
public void test() {
// 使用`httpApi`发起HTTP请求
}
}