Springboot整合redis及redis叢集
1、maven依賴 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2、配置檔案application.properties在新增配置: #redis 叢集 spring.redis.cluster.nodes=192.168.94.130:7000, 192.168.94.130:7001, 192.168.94.130:7002, 192.168.94.130:7003, 192.168.94.130:7004, 192.168.94.130:7005 #redis單個節點 #spring.redis.database=0 #spring.redis.host=192.168.94.130 #spring.redis.port=6379 #spring.redis.password= #spring.redis.pool.max-idle=8 #spring.redis.pool.min-idle=0 #spring.redis.pool.max-active=8 #spring.redis.pool.max-wait=-1 #spring.redis.timeout=5000 3、redisTemplate測試: @Autowired RedisTemplate redisTemplate; @Test public void redisTest() { String key = "aaa"; String value = "bbb"; ValueOperations opsForValue = redisTemplate.opsForValue(); //資料插入 opsForValue.set(key, value); String valueFromRedis = opsForValue.get(key); System.out.println(valueFromRedis) //資料刪除 redisTemplate.delete(key); valueFromRedis = opsForValue.get(key); System.out.println(valueFromRedis) } 4. Spring boot工程測試: application.properties檔案 server.port=8080 #mysql spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.94.130:3306/mytest spring.datasource.username=xuhaixing spring.datasource.password=xuhaixing #spring.jpa.hibernate.naming-strategy=com.xhx.ms.entity.MySQLUpperCaseStrategy #redis #spring.redis.database=0 #spring.redis.host=192.168.94.130 #spring.redis.port=6379 #spring.redis.password= #spring.redis.pool.max-idle=8 #spring.redis.pool.min-idle=0 #spring.redis.pool.max-active=8 #spring.redis.pool.max-wait=-1 #spring.redis.timeout=5000 #redis 叢集 spring.redis.cluster.nodes=192.168.94.130:7000, 192.168.94.130:7001, 192.168.94.130:7002, 192.168.94.130:7003, 192.168.94.130:7004, 192.168.94.130:7005 #jpa spring.jpa.database=mysql spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update javaConfig package com.xhx.ms.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.sun.org.apache.xml.internal.utils.StringBufferPool; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; 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.StringRedisSerializer; import java.lang.reflect.Method; /** * redis快取配置 * Created by xuhaixing on 17-11-25. */ @Configuration @EnableCaching//啟用快取 public class RedisCacheConfig extends CachingConfigurerSupport { /** * 自定義key,此方法會根據類名+方法名+所有引數的值生成一個 * 唯一的key,即@Cacheable中的key */ @Override public KeyGenerator keyGenerator(){ return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for(Object obj:objects){ sb.append(obj.toString()); } System.out.println(sb); return sb.toString(); } }; } /** * redisTemplate快取操作類 * @param redisConnectionFactory * @return */ @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // StringRedisSerializer redisSerializer = new StringRedisSerializer(); // redisTemplate.setKeySerializer(redisSerializer); // redisTemplate.setHashKeySerializer(redisSerializer); setSerializer(redisTemplate); return redisTemplate; } private void setSerializer(RedisTemplate template) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( 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.setHashValueSerializer(jackson2JsonRedisSerializer); } /** * 快取管理器 * */ @Bean public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate){ RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); redisCacheManager.setDefaultExpiration(3000);//s return redisCacheManager; } } Service注入快取 package com.xhx.ms.service; import com.xhx.ms.entity.Person; import com.xhx.ms.repository.PersonRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * Created by xuhaixing on 17-11-25. */ @Service public class PersonService { @Resource private PersonRepository personRepository; @Autowired private RedisTemplate<String,Object> redisTemplate; public void test(){ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); valueOperations.set("mykey1","random1="+Math.random()); System.out.println(valueOperations.get("mykey1")); } @Cacheable(value="person") public Person findById(String id){ System.out.println("------findById-----id = "+id); return personRepository.findOne(id); } // @CacheEvict(value="person",allEntries=true) //刪除全部 @CacheEvict(value="person",key="'com.xhx.ms.service.PersonServicefindById'+#id") //刪除指定 public void deleteFromCache(String id){ System.out.println("------deleteFromCache-----從快取刪除"); } } @Cacheable會記住請求引數和返回值,只有第一次請求時執行了方法,後面請求都沒有執行方法,清楚快取後又執行方法 主方法 package com.xhx.ms; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootRedisApplication { public static void main(String[] args) { SpringApplication.run(SpringbootRedisApplication.class, args); } }
滿意請支援一下:
相關推薦
Springboot整合redis及redis叢集
1、maven依賴 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis
SpringBoot整合集成redis
nap 空閑 imp art rest aps name for runner Redis安裝:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven.a
第三節,SpringBoot整合shrio,Redis快取session與許可權
1.建立Springboot專案 省略。。。 pom檔案 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt
springboot整合jpa、redis+mybais逆向生成domain錯誤集錦
LZ寫的搜劇看劇網站是做好了。但是效率不太高,由於爬取的資料太多,導致前端響應時間太慢。(主要還是LZ是前端分的頁) 開了mybatis二級快取還是不太好。所以想試試redis。然後就搭了一下。碰到了一些錯誤。。記錄一下 【一】Hibernate DDL策略報錯 出現這
SpringBoot 整合Jedis操作Redis快取
在使用SpringBoot構建SpringCloud微服務時,需要用到Redis做資料快取,提高業務邏輯的處理。所以就不得不讓SpringBoot整合Redis,但如果使用官方的Redis去操作的話,你叫麻煩,所以就使用Jedis去操作Reids,這樣操作簡便,編碼效率
解決 springboot整合shiro,redis快取session 多次從redis獲取session問題
spring boot整合shiro redis快取session的教程很多,我這裡就不多說了,看了好多教程沒有解決快取session 多次從redis獲取session的問題,所以發表此部落格,希望對大家有所幫助。本人也是小白一個,如果有什麼問題還請各位大神多多指教
SpringBoot整合Spring-data-redis實現集中式快取
從框架的角度來看,儲存在Redis中的資料只是位元組。雖然說Redis支援多種資料型別,但是那只是意味著儲存資料的方式,而不是它所代表的內容。由我們將這些資料轉化成字串或者是其他物件。我們通過org.springframework.data.redis.serializer. RedisSerializer
springboot-整合mybatis-mysql-redis-quartz,到redis整合時就報錯
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | )
springboot整合mybatis及封裝curd操作-配置文件
enabled () init tps github mys tde oos maven 1 配置文件 application.properties #server server.port=8090 server.address=127.0.0.1 server.ses
SpringBoot整合MyBatis及Thymeleaf
配置文件 target char 使用 otp head batis dmi 分離 http://www.cnblogs.com/ludashi/archive/2017/05/08/6669133.html 上篇博客我們聊了《JavaEE開發之SpringBoot工程的
SpringBoot 整合mybatis 及使用generator,pageHelper外掛
SpringBoot 整合mybatis 及使用generator,pageHelper外掛 一.新建springBoot專案 next next next finish 二.pom檔案配置 <?xml version="1.0" encoding="UTF-8
關於Redis及redis在Linux上的安裝
1.關於關係型資料庫和nosql資料庫 關係型資料庫是基於關係表的資料庫,最終會將資料持久化到磁碟上,而nosql資料 庫是基於特殊的結構,並將資料儲存到記憶體的資料庫。從效能上而言,nosql資料庫 要優於關係型資料庫,從安全性上而言關係型資料庫要優於nosql資料庫,所以在實 際開發中一
2018 - SpringBoot 整合 Mybatis 及 Freemarker 的使用
一、application.yml 配置如下 注:因為格式或編碼問題,如果出現以下錯誤 ERROR : Failed to load property source from location 'classpath:/application.yml' 1. 把註釋刪除 &
2018 - SpringBoot 整合 Mybatis 及 Freemarker 的使用(二)
一、application.yml 配置如下 注:因為格式或編碼問題,如果出現以下錯誤 ERROR : Failed to load property source from location 'classpath:/application.yml' 1. 把註釋刪除 &
springboot整合redisson分散式鎖(叢集模式)
1.maven引入redisson <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</arti
springboot整合mybatis及mybatis generator工具使用
前言 mybatis是一個半自動化的orm框架,所謂半自動化就是mybaitis只支援資料庫查出的資料對映到pojo類上,而實體到資料庫的對映需要自己編寫sql語句實現,相較於hibernate這種完全自動化的框架我更喜歡mybatis,mybatis非常靈活,
SpringBoot 整合 Mybatis 及 Freemarker 的使用
一、application.yml 配置如下 注:因為格式或編碼問題,所以就把註釋去掉了 server: #servlet: #context-path: /springboot p
win10安裝redis及redis客戶端使用方法
目錄結構: readme.md是我自己新增的檔案,不屬於下載內容。 雙擊目錄下的redis-server.exe啟動服務,視窗若關閉,則服務關閉。 也可以使用cmd進到redis目錄輸入 redis-server.exe redis.window
Springboot整合mybatis及分頁查詢、定時任務)
整了一整天,看了一位前輩的部落格,在此基礎上加上本人的理解及創作,哪位前輩忘記了,望諒解!不多說,直接上程式碼。 以上是專案的整體結構,下面是pom.xml檔案資訊:<project xmlns="http://maven.apache.org/POM/4.0.0"
springboot整合thymeleaf及常用標籤的使用方法
請結合springboot學習教程專案github地址 https://github.com/heng1234/spring-boot_one來理解 pom.xml <!-- 引入 thymeleaf 模板依賴 --> <dependency&g