在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 {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        return template;
    }

    @Bean
    public 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 {

    @Autowired
    private 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 {

    @Autowired
    private RedisService redisService;

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }

    @Override
    public 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值