当前位置: 首页 > 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
http://www.lryc.cn/news/532088.html

相关文章:

  • 如何解决 Vue 应用中的内存泄漏
  • 什么是物理地址,什么是虚拟地址?
  • find 和 filter 都是 JavaScript 数组的常用方法
  • MVC、MVP和MVVM模式
  • 基于RTOS的STM32游戏机
  • 【CPP】CPP经典面试题
  • WPF基础03——InitializeComponent()函数解释
  • 如何在自己mac电脑上私有化部署deep seek
  • iOS 老项目适配 #Preview 预览功能
  • 7 与mint库对象互转宏(macros.rs)
  • pytorch实现变分自编码器
  • Node.js与嵌入式开发:打破界限的创新结合
  • Noise Conditional Score Network
  • 低代码系统-产品架构案例介绍、蓝凌(十三)
  • 51单片机 02 独立按键
  • 2021.3.1的android studio版本就很好用
  • CSV数据分析智能工具(基于OpenAI API和streamlit)
  • 移除元素-双指针(下标)
  • 蓝桥杯备赛题目练习(一)
  • FortiOS 存在身份验证绕过导致命令执行漏洞(CVE-2024-55591)
  • 【多线程】线程池核心数到底如何配置?
  • Windows图形界面(GUI)-QT-C/C++ - Qt Combo Box
  • 开源AI智能名片2 + 1链动模式S2B2C商城小程序:内容价值创造与传播新引擎
  • python读取excel工具:openpyxl | AI应用开发
  • 堆的基本概念
  • Android车机DIY开发之软件篇(九) NXP AutomotiveOS编译
  • 嵌入式工程师必学(143):模拟信号链基础
  • 《LLM大语言模型深度探索与实践:构建智能应用的新范式,融合代理与数据库的高级整合》
  • e2studio开发RA2E1(5)----GPIO输入检测
  • Spring @Lazy:延迟初始化,为应用减负