当前位置: 首页 > >

Spring Cloud+Redis cluster+Spring Cache配置

发布时间:

项目结构

personal
+- example
+- cacheTest
+- config
| +- RedisClusterConfig.java
|
+- controller
| +- TestController.java
|
+- model
| +- TestModel.java |
|
+- service
| +- TestService.java
| |
| +- impl
| +- TestServiceImpl.java
|
+- Application.java

pom依赖


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-data-redis

Redis Cluster配置

spring:
redis:
cluster:
nodes: 192.168.10.33:7000,192.168.10.33:7001,192.168.10.33:7002
timeout: 2000
max-redirects: 7

RedisClusterConfig.java

/**
* 配置文件 - Redis Cluster + Spring Cache
* Created by xiepengcheng on 2017/9/13.
*/
@Configuration
@EnableCaching
public class RedisClusterConfig extends CachingConfigurerSupport {


/**
* 初始化 RedisTemplate
* Spring 使用 StringRedisTemplate 封装了 RedisTemplate 对象来进行对redis的各种操作,它支持所有的 redis 原生的 api。
*
* @param clusterNodes
* @param timeout
* @param redirects
* @return
*/
@SuppressWarnings("rawtypes")
@Bean(name = "redisTemplate")
public RedisTemplate redisTemplate(@Value("${spring.redis.cluster.nodes}") String clusterNodes,
@Value("${spring.redis.cluster.timeout}") Long timeout,
@Value("${spring.redis.cluster.max-redirects}") int redirects) {

StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(connectionFactory(getClusterConfiguration(clusterNodes, timeout, redirects)));
setSerializer(template);

return template;
}

/**
* Redis Cluster参数配置
*
* @param clusterNodes
* @param timeout
* @param redirects
* @return
*/
public RedisClusterConfiguration getClusterConfiguration(String clusterNodes, Long timeout, int redirects) {
Map source = new HashMap();
source.put("spring.redis.cluster.nodes", clusterNodes);
source.put("spring.redis.cluster.timeout", timeout);
source.put("spring.redis.cluster.max-redirects", redirects);
return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
}


/**
* 连接池设置
*
* @param configuration
* @return
*/
private RedisConnectionFactory connectionFactory(RedisClusterConfiguration configuration) {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration);
connectionFactory.afterPropertiesSet();
return connectionFactory;
}

/**
* 序列化工具
* 使用 Spring 提供的序列化工具替换 Java 原生的序列化工具,这样 ReportBean 不需要实现 Serializable 接口
*
* @param template
*/
private void setSerializer(StringRedisTemplate template) {
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();
}

/**
* 管理缓存
*
* @param redisTemplate
* @return
*/
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// Number of seconds before expiration. Defaults to unlimited (0)
cacheManager.setDefaultExpiration(600); //设置key-value超时时间,时间单位是秒。
return cacheManager;
}


/**
* 生产key的策略
*
* @return
*/
@Bean
public KeyGenerator wiselyKeyGenerator() {
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();
}
};
}
}

TestController.java

/**
* Web层 - Test
* Created by xiepengcheng on 2017/9/12.
*/
@RestController
@RequestMapping("/test")
public class TestController {

@Autowired
TestService testService;

@GetMapping("/v1/testmodel/{key}")
public List getTestmodel(@PathVariable String key) {

List TestModelList = testService.getTestmodel(key);

return TestModelList;
}

@PutMapping("/v1/testmodel/{key}")
public String updateTestmodel(@PathVariable String key) {
List testModelList = new ArrayList();
TestModel testModel = new TestModel();
testModel.setInfo("缓存测试");
testModelList.add(testModel);
testService.updateTestmodel(key, testModelList);

return "OK";
}
}

TestService.java

/**
* 服务层接口 - Test
* Created by xiepengcheng on 2017/9/12.
*/
public interface TestService {

/**
* 获取TestModel
*
* @param key
* @return
*/
List getTestmodel(String key);

/**
* 更新TestModel
*
* @param key
* @param testModelList
* @return
*/
List updateTestmodel(String key, List testModelList);
}

TestServiceImpl.java

/**
* 接口实现类 - Test
* Created by xiepengcheng on 2017/9/13.
*/
@Service
public class TestServiceImpl implements TestService {

@Override
@Cacheable(value = "TestmodelCache", key = "#key")
public List getTestmodel(String key) {

return new ArrayList();
}

@Override
@CachePut(value = "TestmodelCache", key = "#key")
public List updateTestmodel(String key, List testModelList) {

return testModelList;
}
}

TestModel.java

/**
* Model - 测试
* Created by xiepengcheng on 2017/9/12.
*/
public class TestModel {

/**
* 测试信息
*/
private String info;

public String getInfo() {
return info;
}

public void setInfo(String info) {
this.info = info;
}
}

使用PostMan工具测试




友情链接: