SpringBoot配置文件 详解SpringBoot通用配置文件(不定时更新)
华小灼 人气:1想了解详解SpringBoot通用配置文件(不定时更新)的相关内容吗,华小灼在本文为您仔细讲解SpringBoot配置文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot配置文件,SpringBoot通用配置文件,下面大家一起来学习吧。
以下是SpringBoot项目中的常用配置类、jar包坐标等通用配置
pom文件
<!-- --> <!-- 自定义配置文件提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- Redis启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- SpringSession解决session共享问题--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <!-- RabbitMQ启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- 热部署启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- thymeleaf模板引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- hutool工具包 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.9</version> </dependency> <!-- 阿里巴巴Fastjson工具包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> <!-- 注解校验启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!-- mybatis启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- mybatis-plus启动器 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <!-- MySQL驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <!-- Nacos 服务注册发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- nacos 配置中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
常用配置类
Feign远程调用拦截器配置
@Configuration public class FeignConfig { @Bean public RequestInterceptor requestInterceptor() { return new RequestInterceptor() { /** * 调用远程方法之前先调用此方法 */ @Override public void apply(RequestTemplate requestTemplate) { //Spring提供的工具,获取当前请求的属性, ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); //获取当前请求对象 HttpServletRequest request = requestAttributes.getRequest(); //同步请求头信息 requestTemplate.header("Cookie", request.getHeader("Cookie")); } }; } }
常用方法
分布式中进行线程数据共享-----ThreadLocal
例如:在分布式服务中,进行登录后怎样将当前登录的用户在当前微服务中,可以通过ThreadLocal类型进行共享
// 示例,前后端分离环境 /** * 在连接器中进行拦截验证 */ public class MyInterceptor implements HandlerInterceptor{ public static ThreadLocal<User> loginUser = new ThreadLocal<>(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 获取系统颁发的token String token = request.getHeader("token"); // TODO 在认证服务中心通过token去认证当前是否有账户登录及其登录的状态 User user = feign.validUser(token); //将用户信息保存到本地线程中 loginUser.set(user); } } /** * 测试demo */ public class TestDemo{ public void test(){ // 从本地线程中获取保存的变量 User user = MyInterceptor.loginUser.get(); // TODO 使用user进行业务 } }
Feign同步调用时丢失请求头信息—添加拦截器
@Configuration public class FeignConfig { @Bean public RequestInterceptor requestInterceptor() { return new RequestInterceptor() { /** * 调用远程方法之前先调用此方法 */ @Override public void apply(RequestTemplate requestTemplate) { //Spring提供的工具,获取当前请求的属性, ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); //获取当前请求对象 HttpServletRequest request = requestAttributes.getRequest(); //同步请求头信息 requestTemplate.header("Cookie", request.getHeader("Cookie")); } }; } }
Feign异步调用时丢失上下文
public void testDemo() throws ExecutionException, InterruptedException { // 主线程请求的上下文信息 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> { //设置上下文信息 RequestContextHolder.setRequestAttributes(requestAttributes); //TODO 执行远程调用 }, executor); CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> { //设置上下文信息 RequestContextHolder.setRequestAttributes(requestAttributes); //TODO 执行远程调用 }, executor); CompletableFuture.allOf(task1, task2).get(); } // TODO feign的拦截器代码不用改
加载全部内容