SpringBoot集成redis简要
本文为redis服务的独立部署,内置到应用服务中同理,仅需要2、3、4三步(根据情况添加)
大致步骤:
- 安装redis
- 配置yml和添加pom
- 添加config配置类
- 添加redis数据的get、set类
详细步骤:
1、redis的安装
本人很懒,不想写安装,请移步其他道友:
https://www.runoob.com/redis/redis-install.html
2、新建module作为redis的服务
? application主类不想改可以不动
-
配置yml:
spring: #应用名称 application: name: redis-server redis: #redis 数据库的数量 database: 0 #redis端口地址默认是6379(如果没有改redis软件的配置文件的话) host: 127.0.0.1 port: 6379 password: jedis: pool: #最大连接数,设置为0则无限制 max-active: 8 #最大等待毫秒数, 单位为 ms, 超过时间会出错误信息 max-wait: 1 #空闲连接数,没有数据库的连接时依然可以保持连接不清除的个数 max-idle: 9 min-idle: 0 #单位秒,默认为0(永不断开),在timeout时间之后一直没有连接的话断开连接 timeout: 0 #服务端口 server: port: 8911 #注册中心地址 eureka: client: service-url: defaultZone: http://localhost:8900/eureka/ -
更改pom
org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client 3.1.0 org.springframework.boot spring-boot-starter-data-redis org.springframework.cloud spring-cloud-dependencies 2021.0.0 pom import
3、添加config配置类
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Autowired
CacheProperties cacheProperties;
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer()));
return configuration;
}
@Bean
public RedisTemplate
4、缓存的get、set
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class RedisService {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Resource(name="stringRedisTemplate")
ValueOperations valueOptStr;
@Autowired
RedisTemplate redisTemplate;
@Resource(name = "redisTemplate")
ValueOperations valueOptObj;
/**
* 根据key获取String
* @param key
* @return
*/
public String getStr(String key){
return valueOptStr.get(key);
}
public void setStr(String key, String val){
valueOptStr.set(key, val);
}
public void del(String key){
stringRedisTemplate.delete(key);
}
/**
* Object 设置缓存
* @param key
* @return
*/
public Object getObj(Object key){
return valueOptObj.get(key);
}
public void setObj(Object key, Object val){
valueOptObj.set(key, val);
}
public void del(Object key){
redisTemplate.delete(key);
}
}
调用示例:
1、集成在工程服务中的调用
@Autowired
RedisService redisService;
public String getName(String key){
return redisService.getStr(key);
}
2、redis独立部署的调用
需要有一个controller类接收外部请求,并在调用服务那一方(消费者)添加feignclient
详见: 详细步骤 3