建站工作室源码,野花韩国视频观看免费高清的,品牌网站建设特色大蝌蚪,wordpress不用模版前言
在分布式系统中#xff0c;共用组件的设计可以极大地提升代码复用性和维护性。Spring Cloud中将Redis共用到一个公共模块#xff08;common模块#xff09;是一个常见的设计实践#xff0c;这样可以让多个微服务共享相同的Redis配置和操作逻辑。本文将详细介绍如何在…前言
在分布式系统中共用组件的设计可以极大地提升代码复用性和维护性。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 RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {RedisTemplateString, 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 RedisTemplateString, 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.hostlocalhost
spring.redis.port6379
5. 添加依赖
在 common-module的 pom.xml中添加Spring Data Redis依赖
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency
/dependencies
Service模块的实现
1. 添加依赖
在 service-module的 pom.xml中添加对 common-module的依赖
dependenciesdependencygroupIdcom.example/groupIdartifactIdcommon-module/artifactIdversion1.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.hostlocalhost
spring.redis.port6379