服務(wù)近2000家企業(yè),依托一系列實(shí)踐中打磨過的技術(shù)和產(chǎn)品,根據(jù)企業(yè)的具體業(yè)務(wù)問題和需求,針對性的提供各行業(yè)大數(shù)據(jù)解決方案。
Springboot極速之道,spb集成redis服務(wù)器提高訪問速度
來源:未知 時間:2018-56-29 瀏覽次數(shù):140次
SpringBoot是一款輕量級的JAVA WEB開發(fā)框架,配置簡單,使用方便,解決了項目開發(fā)中不通開發(fā)工程師之前代碼的耦合性,但是如果遇到頻繁訪問的數(shù)據(jù)處理情況對數(shù)據(jù)的查詢就會比較慢,解決方案是對于平凡范圍的數(shù)據(jù)存儲在redis集群中,使用SPRINGboot操作redis來提高訪問速度和降低對數(shù)據(jù)庫得負(fù)載壓力,本文詳細(xì)講解SpringBoot整合redis的配置
一、引入jar包
1、引入 spring-boot-starter-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
2、添加配置文件
# REDIS (RedisProperties) # Redis數(shù)據(jù)庫索引(默認(rèn)為0)
spring.redis.database=0 # Redis服務(wù)器地址
spring.redis.host=192.168.0.58 # Redis服務(wù)器連接端口
spring.redis.port=6379 # Redis服務(wù)器連接密碼(默認(rèn)為空)
spring.redis.password= # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.pool.max-active=8 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
spring.redis.database=0 # Redis服務(wù)器地址
spring.redis.host=192.168.0.58 # Redis服務(wù)器連接端口
spring.redis.port=6379 # Redis服務(wù)器連接密碼(默認(rèn)為空)
spring.redis.password= # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.pool.max-active=8 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
spring.redis.pool.max-wait=-1 # 連接池中的最大空閑連接
spring.redis.pool.max-idle=8 # 連接池中的最小空閑連接
spring.redis.pool.min-idle=0 # 連接超時時間(毫秒)
spring.redis.timeout=0
spring.redis.pool.max-idle=8 # 連接池中的最小空閑連接
spring.redis.pool.min-idle=0 # 連接超時時間(毫秒)
spring.redis.timeout=0
3、添加cache的配置類
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//設(shè)置緩存過期時間
//rcm.setDefaultExpiration(60);//秒
return rcm;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4、直接使用redis開發(fā)項目
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class TestRedis {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test() throws Exception {
stringRedisTemplate.opsForValue().set("aaa", "111");
Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
}
@Test
public void testObj() throws Exception {
User user=new User("aa@126.com", "aa", "aa123456", "aa","123");
ValueOperations<String, User> operations=redisTemplate.opsForValue();
operations.set("com.neox", user);
operations.set("com.neo.f", user,1,TimeUnit.SECONDS);
Thread.sleep(1000);
//redisTemplate.delete("com.neo.f");
boolean exists=redisTemplate.hasKey("com.neo.f");
if(exists){
System.out.println("exists is true");
}else{
System.out.println("exists is false");
}
// Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());
}
}
以上都是手動使用的方式,如何在查找數(shù)據(jù)庫的時候自動使用緩存呢,看下面;
4、自動根據(jù)方法生成緩存
@RequestMapping("/getUser")
@Cacheable(value="user-key")
public User getUser() {
User user=userRepository.findByUserName("aa");
System.out.println("若下面沒出現(xiàn)“無緩存的時候調(diào)用”字樣且能打印出數(shù)據(jù)表示測試成功");
return user;
}
其中value的值就是緩存到redis中的key
二、共享Session-spring-session-data-redis
分布式系統(tǒng)中,sessiong共享有很多的解決方案,其中托管到緩存中應(yīng)該是最常用的方案之一,
1、引入依賴
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
2、Session配置:
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
public class SessionConfig {
}
maxInactiveIntervalInSeconds: 設(shè)置Session失效時間,使用Redis Session之后,原Boot的server.session.timeout屬性不再生效
maxInactiveIntervalInSeconds: 設(shè)置Session失效時間,使用Redis Session之后,原Boot的server.session.timeout屬性不再生效
三、結(jié)束語
springboot集成redis不僅能夠提高數(shù)據(jù)讀取的效率,降低服務(wù)器負(fù)載,而且能夠解決部分架構(gòu)層面的問題
springboot集成redis不僅能夠提高數(shù)據(jù)讀取的效率,降低服務(wù)器負(fù)載,而且能夠解決部分架構(gòu)層面的問題