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

在Spring Cloud中将Redis共用到Common模块

前言

在分布式系统中,共用组件的设计可以极大地提升代码复用性和维护性。Spring Cloud中将Redis共用到一个公共模块(common模块)是一个常见的设计实践,这样可以让多个微服务共享相同的Redis配置和操作逻辑。本文将详细介绍如何在Spring Cloud中实现这一目标。

项目结构

首先,定义项目的结构:

spring-cloud-redis-common
│
├── common-module
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── example
│   │   │   │           └── common
│   │   │   │               ├── RedisConfig.java
│   │   │   │               ├── RedisService.java
│   │   │   │               └── model
│   │   │   │                   └── CacheItem.java
│   │   │   └── resources
│   │   │       └── application.properties
│   └── pom.xml
│
└── service-module├── src│   ├── main│   │   ├── java│   │   │   └── com│   │   │       └── example│   │   │           └── service│   │   │               └── ServiceApplication.java│   │   └── resources│   │       └── application.properties└── pom.xml
​

Common模块的实现

1. 定义Redis配置

在 common-module中创建 RedisConfig.java,配置Redis连接:

package com.example.common;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);return template;}@Beanpublic StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {return new StringRedisTemplate(factory);}
}
​

2. 定义Redis操作服务

在 common-module中创建 RedisService.java,提供Redis操作方法:

package com.example.common;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class RedisService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void set(String key, Object value, long timeout, TimeUnit unit) {redisTemplate.opsForValue().set(key, value, timeout, unit);}public Object get(String key) {return redisTemplate.opsForValue().get(key);}public void delete(String key) {redisTemplate.delete(key);}
}
​

3. 定义数据模型

在 common-module中创建 CacheItem.java,定义数据模型:

package com.example.common.model;import java.io.Serializable;public class CacheItem implements Serializable {private String id;private String value;// getters and setters
}
​

4. 配置文件

在 common-module的 resources目录下添加 application.properties

spring.redis.host=localhost
spring.redis.port=6379
​

5. 添加依赖

在 common-module的 pom.xml中添加Spring Data Redis依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
</dependencies>
​

Service模块的实现

1. 添加依赖

在 service-module的 pom.xml中添加对 common-module的依赖:

<dependencies><dependency><groupId>com.example</groupId><artifactId>common-module</artifactId><version>1.0.0</version></dependency>
</dependencies>
​

2. 使用Common模块中的Redis服务

在 service-module中创建 ServiceApplication.java,使用 RedisService

package com.example.service;import com.example.common.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ServiceApplication implements CommandLineRunner {@Autowiredprivate RedisService redisService;public static void main(String[] args) {SpringApplication.run(ServiceApplication.class, args);}@Overridepublic void run(String... args) throws Exception {redisService.set("testKey", "testValue", 1, TimeUnit.HOURS);System.out.println("Stored value: " + redisService.get("testKey"));}
}
​

3. 配置文件

在 service-module的 resources目录下添加 application.properties,以覆盖common模块中的配置:

spring.redis.host=localhost
spring.redis.port=6379

相关文章:

  • 健康管理系统的核心价值:降低成本,提升效率
  • leetcode701.二叉搜索树中的插入操作:迭代法利用有序性寻找空节点插入点
  • Java HashMap原理:高效键值存储的秘密
  • Spring AI(9)——MCP客户端
  • 4060显卡什么水平 4060显卡参数介绍
  • 【QT】理解QT机制之“元对象系统”
  • JavaSE:面向对象进阶之抽象类
  • [python] lock 解决线程安全问题
  • 信号与系统速成-1.绪论
  • Java面试八股(Java基础,Spring,SpringBoot篇)
  • json中对象转字符串和字符串转对象的方法
  • 【Linux系统移植】Cortex-A8 Linux系统移植(超详细)
  • Next.js 布局(Layout)与模板(Template)深度解析:从原理到实战
  • Vue模板语法
  • 大模型应用开发之评估
  • LeetCode 75. 颜色分类 - 双指针法高效解决(Java实现)
  • 【评测】推理和微调 “GTE文本向量-中文-通用领域-base”模型
  • [嵌入式实验]实验二:LED控制
  • 公司数据不泄露,DeepSeek R1本地化部署+web端访问+个人知识库搭建与使用
  • 19、Python字符串高阶实战:转义字符深度解析、高效拼接与输入处理技巧
  • 网站建设与运营财务预算/公众号微博seo
  • 做网站后台数据库建设/一般的电脑培训班要多少钱
  • 做网站申请多少类商标/临沂做网站推广的公司
  • 易支付做网站接口怎么赚钱/seo知识培训
  • 中国机械加工网平台/seo优化技术排名
  • ps做图下载网站/跟我学seo