⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 juejin.cn/post/7360947498943578139 「enyan」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注微信公众号:【芋道源码】有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言都将得到认真回复。甚至不知道如何读源码也可以请教噢。
  4. 新的源码解析文章实时收到通知。每周更新一篇左右。
  5. 认真的源码交流微信群。

1. 项目初始化

如果你问研发同学,在开发过程中最讨厌、最痛苦的事情是什么?大部分同学会告诉环境,环境,还是环境。

我带你走一趟你就知道环境搭建是多么头疼的事情了。

在开发一个新项目之前,先下载IDE,光是IDE这个事情,可能就折腾半天。为啥要折腾这么久呢,下载倒是非常快,可现在的IDE基本上都收费,所以网上就出现了各种破解软件,有每30天需要激活一次的,有各种lisence的,总之这些方法在你尝试了很多次之后,基本无一奏效,jetBrains是靠这个挣钱的,如果大家都破解了,人家怎么生存?找各种方法破解,最终都是浪费时间。

为啥大家喜欢用盗版呀,不是喜欢,有免费的会用收费的吗?这是一种心理。说起用盗版这个成因可能就比较复杂了,大部分程序员是随着免费环境成长起来的,一说到收费,第一反应是很难适应的,还记得Mp3吗?刚开始的时候大家都免费下载MP3,但后来因为版权问题开始收费了,下载量跌了50%。

可能还有另外一个原因,作为程序员还不能找一个破解的方法?虽然你道高一尺,但我魔高一丈。

除此之外,大家觉得收费并不便宜,所以望而却步了。

虽然有诸多限制,但IDE必须还得用啊,官方提供了社区版,很多同学用着社区版,还有一部分同学继续走着破解之路。接下来咱们先看看如何用IDE创建springboot项目,然后一路next就行了

这就是刚创建好的项目,新鲜出炉,有启动类、配置文件、测试启动类。

2. 版本管理

咱们的项目就这么轻松的创建成功了,是不是可以上手开发了,先别着急。先给你看个东西。

这是springCloud和springboot版本之间的对应关系:

  • https://spring.io/projects/spring-cloud

这是springboot和kafka的版本对应关系:

  • https://spring.io/projects/spring-kafka

很复杂吧,瞬间就想骂娘了?

我先给你讲个最近发生的故事,让你平复一下心情。我最近就在spring-kakfa版本上面栽了跟头

事情是这样的:我们生产环境用的kafka-server是0.11版本的,但我们的客户端用的是3.0.4版本,我的springboot用的是2.7.x版本,从上边表中看到springboot的版本和kafka-client的版本是适配的,但kafka-client的版本和server的版本是不适配的

这是当时的报错信息

?,?:Exception thrown when sending a message with key=’null’ and payload=’byte[205]’ to topic notify org.apache.kafka.common.errors.UnsupportedVersionException: Attempting to use idempotence with a broker which does not support the required message format (v2). The broker must be version 0.11 or later.

你可能会问这是非常容易发现的问题呀,也很容易测试出来呀,对,问题很容易复现

关键就是生产环境的版本和测试环境的server版本不一样,不一样,不一样,真是没想到啊,所以就栽了跟头。

有一款神器不是叫Maven吗,这个不就是解决版本之间的依赖关系吗?

在说maven之前,先简单说一下springboot的自动配置,在springboot出来之前,大家依赖关系都是通过手动添加,springboot的autoconfiuration功能解决了包之间依赖关系,至少让研发的开发效率提升了50%,但有些场景下依赖的冲突还是未能解决。

  • https://maven.apache.org/index.html

Apache Maven is a software project management and comprehension tool

我们最常用的maven命令是build,package,在构建上真的是一把利器,maven确实提升了研发的效率。

3. 废话不多说,直接看脚手架

接下来我们来看看都有哪些核心类,我把代码贴到下方。

全局异常处理

@RestControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

@ExceptionHandler(value = {MethodArgumentNotValidException.class})
public ResponseResult<String> handleValidException(MethodArgumentNotValidException ex, HttpServletResponse httpServletResponse) {
log.error("[GlobalExceptionHandler][handleValidException] 参数校验exception", ex);
return wrapperBindingResult(ex.getBindingResult(), httpServletResponse);
}


private ResponseResult<String> wrapperBindingResult(BindingResult bindingResult, HttpServletResponse httpServletResponse) {
StringBuilder errorMsg = new StringBuilder();
for (ObjectError error : bindingResult.getAllErrors()) {
if (error instanceof FieldError) {
errorMsg.append(((FieldError) error).getField()).append(": ");
}
errorMsg.append(error.getDefaultMessage() == null ? "" : error.getDefaultMessage());

}
httpServletResponse.setStatus(HttpStatus.BAD_REQUEST.value());
return ResponseResult.failed(ResultCode.FAILED.getCode(),null);
}

日志处理

@Aspect
@Slf4j
@Component
public class WebLogAspect {

@Pointcut("@within(org.springframework.stereotype.Controller) || @within(org.springframework.web.bind.annotation.RestController)")
public void cutController() {
}

@Before("cutController()")
public void doBefore(JoinPoint point) {
//获取拦截方法的参数
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String url = request.getRequestURL().toString();
List<Object> list = Lists.newArrayList();
for (Object object : point.getArgs()) {
if (object instanceof MultipartFile || object instanceof HttpServletRequest || object instanceof HttpServletResponse || object instanceof BindingResult) {
continue;
}
list.add(object);
}
log.info("请求 uri:[{}],params:[{}]", url, StringUtils.join(list, ","));
}

/**
* 返回通知:
* 1. 在目标方法正常结束之后执行
* 1. 在返回通知中补充请求日志信息,如返回时间,方法耗时,返回值,并且保存日志信息
*
* @param response
* @throws Throwable
*/
@AfterReturning(returning = "response", pointcut = "cutController()")
public void doAfterReturning(Object response) {
if (response != null) {
log.info("请求返回result:[{}]", JSONUtil.toJsonStr(response));
}
}
}

跨域类

@Configuration
public class GlobalCorsConfig {
/**
* 允许跨域调用的过滤器
*/
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
//允许所有域名进行跨域调用
config.setAllowedOrigins(Lists.newArrayList("*"));
//允许跨越发送cookie
config.setAllowCredentials(true);
//放行全部原始头信息
config.addAllowedHeader("*");
//允许所有请求方法跨域调用
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo()).enable(true)
.select()
//apis: 添加swagger接口提取范围
.apis(RequestHandlerSelectors.basePackage("com.vines.controller"))
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("项目描述")
.description("基础服务项目描述")
.contact(new Contact("作者", "作者URL", "作者Email"))
.version("1.0")
.build();
}
}

响应体

@Data
public class ResponseResult<T> {
private int code;
private String message;
private T data;

public static <T> ResponseResult<T> success(T data){
ResponseResult<T> responseResult=new ResponseResult<>();
responseResult.setCode(ResultCode.SUCCESS.getCode());
responseResult.setMessage(ResultCode.SUCCESS.getMessage());
responseResult.setData(data);
return responseResult;
}
public static <T> ResponseResult<T> success(){
ResponseResult<T> responseResult=new ResponseResult<>();
responseResult.setCode(ResultCode.SUCCESS.getCode());
responseResult.setMessage(ResultCode.SUCCESS.getMessage());
return responseResult;
}

public static <T> ResponseResult failed(int code,String message){
ResponseResult<T> responseResult=new ResponseResult<>();
responseResult.setCode(code);
responseResult.setMessage(message);
return responseResult;
}

public static boolean isSucceed(ResponseResult responseResult){
return responseResult.getCode()==ResultCode.SUCCESS.getCode();
}
}

3.1 常用工具

除了这些基本的工具之外,我再推荐几款我们项目中常用的工具

我们项目常常依赖中间件,比如mysql,kafka,redis等,如果要单元测试,我们通常的做法是在dev环境部署一套项目中依赖的中间件,非常麻烦,而且数据还不容易隔离,所以内存版的中间件就是来解决这个问题的。

内存版Redis:

  • https://github.com/kstyrc/embedded-redis

内存版DB:

  • https://github.com/mariadb

内存版kafka,springboot提供了测试依赖,直接引入starter即可

<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>

hutool:

  • https://hutool.cn/

mybatis plus:

  • https://baomidou.com/

mapStruct:

  • https://mapstruct.org/

redisson:

  • https://github.com/redisson/redisson

总结

在真实的工作中,IDE的配置工作其实不是最麻烦的和最浪费的时间的,有一件事情更加浪费时间,每次搞的我都特别的崩溃,这件事情也和环境相关,同时也和其他人相关。你们猜猜是什么事情呢?

文章目录
  1. 1. 1. 项目初始化
  2. 2. 2. 版本管理
  3. 3. 3. 废话不多说,直接看脚手架
    1. 3.0.0.1. 3.1 常用工具
  • 4. 总结