当前位置: 首页 > article >正文

springCloud2025+springBoot3.5.0+Nacos集成redis从nacos拉配置起服务

文章目录

  • 前言
  • 一、网关gateway选型
    • 1. 响应式编程模型
    • 2. 网关的特定需求
    • 3. 技术栈一致性
    • 4. 性能对比
    • 5. 实际应用场景优势
  • 二、redis的集成
    • 1.引入库
    • 2.配置类
      • A、自定义配置类RedisAfterNacosAutoConfiguration
      • B、自定义配置类RedisConfig
  • 总结


前言

       最近在搭建最新的springCloud + springboot3 + nacos微服务框架,有个新项目要启动了,做完二期公司都可以上市的,所以基础得搭建牢固。有建议找开源的,之前呆过的一家公司居然还花钱买框架,真是。。。。。。
       以上2种方式我都不喜欢,要搞就搞最新的,就跟找朋友样,要找就找年轻的,所以我就算是今天周六也在家搞搞呗。因为都是最新的,参考真的很少啊,踩几个坑了,但是没有太多时间复盘,今天就先简单说下gateway网关用redis的事情。
       这里是redis的配置也放nacos,那么问题就来了,自动配置的RedisProperties怎么在服务还在启动过程中就去nacos哪配置呢?


一、网关gateway选型

       gateway选择基于 WebFlux 而非传统的 Servlet 栈(如 Spring MVC)呢?就是因为这个原因,gateway的redis我不能直接引入通用的common,因为我把数据库连接、异常统一处理、redis等都放在了common包,但是异常统一处理是基于starter-web写的,所以最后我gateway单独引入redis。
       为什么我不把gateway也换成基于starter-web呢?原因如下:

1. 响应式编程模型

核心优势:

  • 非阻塞 I/O:WebFlux 基于 Reactor 实现非阻塞异步处理,特别适合网关这种 I/O 密集型场景

  • 高并发能力:用少量线程处理大量并发连接(相比线程池模型的 Servlet 容器)

  • 资源高效:避免线程上下文切换开销,减少内存消耗

2. 网关的特定需求

功能性考量:

  • 请求转发:网关需要高效处理大量 HTTP 请求转发

  • 过滤器链:WebFlux 的函数式编程模型更适合实现灵活的过滤器管道

  • 背压支持:内置响应式流背压机制,防止下游服务过载

3. 技术栈一致性

架构匹配:

  • Spring 5+ 生态:Gateway 作为 Spring Cloud 新一代组件,自然采用 Spring 5 的响应式体系

  • Netty 集成:默认使用 Netty 作为服务器,与 WebFlux 深度集成

  • 函数式路由:支持 lambda 表达式定义路由规则,更简洁的 API 设计

4. 性能对比

指标WebFlux (Netty)Servlet (Tomcat)
线程模型事件循环线程池
并发连接处理能力更高受限于线程池大小
内存占用更低更高
长连接支持更优一般

5. 实际应用场景优势

典型用例:

  • 微服务架构中的边缘服务

  • 需要处理大量并发连接(如 IoT 场景)

  • 需要低延迟的请求转发

  • 需要灵活的自定义过滤逻辑

二、redis的集成

1.引入库

引库没啥可说的,无非就是现在最新的redis的starter的配置项前缀发生了改变,不再是“spring.redis”了,而是“spring.data.redis”。

<!-- Spring Boot 3 Redis 依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>${redisson.version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>

2.配置类

这里因为是要先加载完nacos,再从nacos拉取redis,最后启动服务。正因为这个服务启动顺序,又涉及到最新的springboot、cloud,所以就出现了2种写法,写在这里也是笔记也是分享。
redis在nacos的配置

spring:data:redis:host: 127.0.0.1port: 6379password: database: 0timeout: 5000ms

A、自定义配置类RedisAfterNacosAutoConfiguration


import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;/*** @author zwmac*/
@AutoConfiguration(after = NacosConfigAutoConfiguration.class)
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", havingValue = "true", matchIfMissing = true)
public class RedisAfterNacosAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic RedisConnectionFactory redisConnectionFactory(ConfigurableEnvironment env) {String host = env.getProperty("spring.data.redis.host");Integer port = env.getProperty("spring.data.redis.port", Integer.class, 6379);String password = env.getProperty("spring.data.redis.password");if (host == null) {throw new IllegalStateException("Redis配置未从Nacos加载,请检查 redis-config.yaml 是否加载成功");}RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);if (StringUtils.hasText(password)) {config.setPassword(RedisPassword.of(password));}return new LettuceConnectionFactory(config);}@Bean@ConditionalOnMissingBeanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();template.setDefaultSerializer(serializer);template.setKeySerializer(new StringRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setValueSerializer(serializer);template.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}
}

注意这种方式需要在启动类上加@Import(RedisAfterNacosAutoConfiguration.class),否则无效。

B、自定义配置类RedisConfig

import com.rs.gov.govgateway.service.redis.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;/*** @author zwmac*/
@Configuration
@DependsOn("nacosConfigManager")
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {private final RedisProperties redisProperties;public RedisConfig(RedisProperties redisProperties) {this.redisProperties = redisProperties;}@Beanpublic RedisService redisService(RedisTemplate<Object, Object> redisTemplate) {return new RedisService(redisTemplate);}@Beanpublic LettuceConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();config.setHostName(redisProperties.getHost());config.setPort(redisProperties.getPort());config.setPassword(RedisPassword.of(redisProperties.getPassword()));return new LettuceConnectionFactory(config);}@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);// 推荐的序列化方式:不需要设置 ObjectMapperGenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();template.setDefaultSerializer(jackson2JsonRedisSerializer);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}// 可选:注册 RedissonClient@Bean(destroyMethod = "shutdown")public RedissonClient redissonClient() {Config config = new Config();SingleServerConfig singleServerConfig = config.useSingleServer();singleServerConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort());if (StringUtils.isNotBlank(redisProperties.getPassword())) {singleServerConfig.setPassword(redisProperties.getPassword());}return Redisson.create(config);}}

       我也怕麻烦,个人认为RedisConfig更简单,无非就是要注意@DependsOn(“nacosConfigManager”)这个注解。


总结

       最近的事有点多,压力很大啊,关键是BOSS对软件没有认知,部门软件角色的人员也配备不全。主责的项目啥都没有,只有一个前端截图、一个Excel文档,根据Excel文档估的工时打骨折,难啊!

http://www.lryc.cn/news/2404006.html

相关文章:

  • AI生成的基于html+marked.js实现的Markdown转html工具,离线使用,可实时预览 [
  • 机器学习:load_predict_project
  • OkHttp 3.0源码解析:从设计理念到核心实现
  • 【storage】
  • 微信小程序带参分享、链接功能
  • JVM 垃圾回收器 详解
  • FreeRTOS任务之深入篇
  • Linux 系统、代码与服务器进阶知识深度解析
  • 人工智能--AI换脸
  • NLP学习路线图(二十七):Transformer编码器/解码器
  • 【机器学习】支持向量机实验报告——基于SVM进行分类预测
  • 策略模式实战:Spring中动态选择商品处理策略的实现
  • 主流信创数据库对向量功能的支持对比
  • Matlab | matlab中的画图工具详解
  • HA: Wordy靶场
  • 6.7本日总结
  • 中国移动6周年!
  • Svelte 核心语法详解:Vue/React 开发者如何快速上手?
  • Fullstack 面试复习笔记:HTML / CSS 基础梳理
  • 408第一季 - 数据结构 - 树与二叉树II
  • 打卡第47天
  • 从上下文学习和微调看语言模型的泛化:一项对照研究
  • 智慧城市建设方案
  • phosphobot开源程序是控制您的 SO-100 和 SO-101 机器人并训练 VLA AI 机器人开源模型
  • pygame开发的坦克大战
  • C++2025.6.7 C++五级考题
  • 【原神 × 二叉树】角色天赋树、任务分支和圣遗物强化路径的算法秘密!
  • 功能安全实战系列09-英飞凌TC3xx LBIST开发详解
  • 一个完整的日志收集方案:Elasticsearch + Logstash + Kibana+Filebeat (二)
  • RT-Thread内核组成——内核移植