详解SpringBoot通用配置文件(不定时更新)
以下是SpringBoot项目中的常用配置类、jar包坐标等通用配置
pom文件
org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-starter-thymeleaf cn.hutool hutool-all 5.5.9 com.alibaba fastjson 1.2.75 org.springframework.boot spring-boot-starter-validation org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 com.baomidou mybatis-plus-boot-starter 3.4.2 mysql mysql-connector-java 8.0.23 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
常用配置类
Feign远程调用拦截器配置
@Configuration
publicclassFeignConfig{
@Bean
publicRequestInterceptorrequestInterceptor(){
returnnewRequestInterceptor(){
/**
*调用远程方法之前先调用此方法
*/
@Override
publicvoidapply(RequestTemplaterequestTemplate){
//Spring提供的工具,获取当前请求的属性,
ServletRequestAttributesrequestAttributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
//获取当前请求对象
HttpServletRequestrequest=requestAttributes.getRequest();
//同步请求头信息
requestTemplate.header("Cookie",request.getHeader("Cookie"));
}
};
}
}
常用方法
分布式中进行线程数据共享-----ThreadLocal
例如:在分布式服务中,进行登录后怎样将当前登录的用户在当前微服务中,可以通过ThreadLocal类型进行共享
//示例,前后端分离环境
/**
*在连接器中进行拦截验证
*/
publicclassMyInterceptorimplementsHandlerInterceptor{
publicstaticThreadLocalloginUser=newThreadLocal<>();
@Override
publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{
//获取系统颁发的token
Stringtoken=request.getHeader("token");
//TODO在认证服务中心通过token去认证当前是否有账户登录及其登录的状态
Useruser=feign.validUser(token);
//将用户信息保存到本地线程中
loginUser.set(user);
}
}
/**
*测试demo
*/
publicclassTestDemo{
publicvoidtest(){
//从本地线程中获取保存的变量
Useruser=MyInterceptor.loginUser.get();
//TODO使用user进行业务
}
}
Feign同步调用时丢失请求头信息—添加拦截器
@Configuration
publicclassFeignConfig{
@Bean
publicRequestInterceptorrequestInterceptor(){
returnnewRequestInterceptor(){
/**
*调用远程方法之前先调用此方法
*/
@Override
publicvoidapply(RequestTemplaterequestTemplate){
//Spring提供的工具,获取当前请求的属性,
ServletRequestAttributesrequestAttributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
//获取当前请求对象
HttpServletRequestrequest=requestAttributes.getRequest();
//同步请求头信息
requestTemplate.header("Cookie",request.getHeader("Cookie"));
}
};
}
}
Feign异步调用时丢失上下文
publicvoidtestDemo()throwsExecutionException,InterruptedException{
//主线程请求的上下文信息
RequestAttributesrequestAttributes=RequestContextHolder.getRequestAttributes();
CompletableFuturetask1=CompletableFuture.runAsync(()->{
//设置上下文信息
RequestContextHolder.setRequestAttributes(requestAttributes);
//TODO执行远程调用
},executor);
CompletableFuturetask2=CompletableFuture.runAsync(()->{
//设置上下文信息
RequestContextHolder.setRequestAttributes(requestAttributes);
//TODO执行远程调用
},executor);
CompletableFuture.allOf(task1,task2).get();
}
//TODOfeign的拦截器代码不用改
到此这篇关于详解SpringBoot通用配置文件(不定时更新)的文章就介绍到这了,更多相关SpringBoot配置文件内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。