主要介绍用SpringBoot和Redis整合,这里先介绍,单个节点redis整合方式,redis集群可依次类推,但需要自己增加部分代码。
1.引入maven依赖
<!--这个必不可少-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.RedisService接口
package com.yarm.redis.service;
public interface RedisService {
/**
* 插入一条数据
* @param key
* @param value
* @param timeOut
* @throws Exception
*/
public boolean setRedis(String key, String value, Long timeOut) throws Exception;
/**
* 获取一条数据
* @param key
* @return
* @throws Exception
*/
public String getRedis(String key) throws Exception;
/**
* 更新key
* @param key
* @param value
* @param timeOut
* @throws Exception
*/
public boolean updateRedis(String key, String value, Long timeOut) throws Exception;
}
3.redis接口实现
package com.yarm.redis.service.impl;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.yarm.redis.service.RedisService;
@Service
@SuppressWarnings("rawtypes")
public class RedisServiceImpl implements RedisService{
@Autowired
private RedisTemplate redisTemplate;
@SuppressWarnings("unchecked")
@Override
public boolean setRedis(String key, String value, Long timeOut) throws Exception{
boolean result = false;
ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
if (!this.redisTemplate.hasKey(key)) {
ops.set(key, value, timeOut, TimeUnit.SECONDS);
result = true;
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public String getRedis(String key) throws Exception{
ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
return ops.get(key);
}
@SuppressWarnings("unchecked")
@Override
public boolean updateRedis(String key, String value, Long timeOut) throws Exception {
boolean result = false;
if (this.redisTemplate.hasKey(key)) {
ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
ops.set(key, value, timeOut, TimeUnit.SECONDS);
result = true;
}
return result;
}
}
4.调用controller
package com.yarm.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.yarm.service.impl.AppRedisService;
@RestController
@RequestMapping(value="redis")
public class RedisController {
@Autowired
private AppRedisService redisService;
@RequestMapping(value = "{id}.json", method = RequestMethod.GET)
public Map<String, Object> getRedisJson(@PathVariable("id") String id){
Map<String, Object> map = new HashMap<>();
try {
String redis = redisService.getRedis(id);
map.put("code", 200);
map.put("data", redis);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
@RequestMapping(value = "{id}.ctr", method = RequestMethod.GET)
public Map<String, Object> getRedisXml(@PathVariable("id") String id){
Map<String, Object> map = new HashMap<>();
try {
String redis = redisService.getRedis(id);
map.put("code", 200);
map.put("data", redis);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
@RequestMapping(value = "{key}_{value}.ctr", method = RequestMethod.GET)
public Map<String, Object> setRedis(@PathVariable("key") String key, @PathVariable("value") String value){
Map<String, Object> map = new HashMap<>();
try {
redisService.setRedis(key, value);
map.put("code", 200);
map.put("data", "");
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
@RequestMapping(value = "set.ctr", method = RequestMethod.POST)
public Map<String, Object> setRedisMap(@RequestBody Map<String,Object> reqMap){
Map<String, Object> map = new HashMap<>();
try {
String key = reqMap.get("key").toString();
String value = reqMap.get("value").toString();
map.put("code", 200);
map.put("data", this.redisService.setRedis(key, value));
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
@RequestMapping(value = "update.ctr", method = RequestMethod.PUT)
public Map<String, Object> updateRedisMap(@RequestBody Map<String,Object> reqMap){
Map<String, Object> map = new HashMap<>();
try {
String key = reqMap.get("key").toString();
String value = reqMap.get("value").toString();
map.put("code", 200);
map.put("data", this.redisService.updateRedis(key, value));
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
5.springBoot启动main
package com.yarm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6.项目启动之后即可访问相应的controller
如:http://127.0.0.1:8080/redis/2.json等
7.项目地址: