window下redis的常用命令、spring結合redis實現訊息釋出監聽系統
redis是近幾年比較流行的基於記憶體的高效能key-value持久化非關係型資料庫,redis也可與spring結合做非同步消費釋出監聽系統
redis下載地址:
https://download.csdn.net/download/higherzjm/10762200
解壓即可使用,點開redis-server.exe即做服務,點開redis-cli.exe即做客戶端命令視窗
1 redis常用命令
1.1 info 獲取Redis的相關資訊
1.2 keys * 獲取當前redis資料庫中所有的key名稱
1.3 select 1 切換資料庫,1 是資料庫索引
1.4 設定和獲取普通的key-value值
命令設定: set name "Saturday"
命令獲取: get name
使用Java設定:redisTemplate.opsForValue().set("name","sunday") (redisTemplate初始化方式後面會說明)
使用java獲取:redisTemplate.opsForValue().get("name");
1.5 設定或獲取set值
命令設定:sadd set_123 aaaa bbbb cccc (set_123 是key名稱)
命令獲期:smembers set_123
使用Java設定:redisTemplate.opsForSet().add("set_123", "set1","set2","set3")
使用java獲取:redisTemplate.opsForSet().members("set_123")
1.6 設定或獲取list值
命令設定:lpush mylist list1 list2 list3 (mylist 是key名稱)
命令獲期:lrange mylist 0 -1 (第二個數值表示結束的索引,如果為-1表示所有)
使用Java設定:redisTemplate.opsForList().leftPush("mylist ", String.valueOf(Arrays.asList("list1","list2","list3")) );
使用java獲取:redisTemplate.opsForList().leftPop("mylist ");
1.7 設定或獲取lHash值(其實這邊所說的hash值就是我們日常遇到的key-value鍵值對)
命令設定:hmset myhash k1 "v1" k2 "v2" (k1,k2是key名稱)
命令獲期:hmget myhash k1 k2(獲取k1,k2鍵的值)
使用Java設定:
Map<String,String> map=new HashMap<>();
map.put("key1","value1"); map.put("key2","value2");
map.put("key3","value3"); map.put("key4","value4");
redisTemplate.opsForHash().putAll("map",map);
使用java獲取:redisTemplate.opsForHash().entries("map")
2 redis 與spring結合實現訊息傳送監聽系統
訊息傳送監聽系統主要spring結合redis非同步推送訊息到redis佇列,並進行監聽處理
2.1 xml配置: 初始化redisTemplat和其他監聽資訊
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:redis="http://www.springframework.org/schema/redis"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<description>spring-data-redis配置</description>
<util:properties id="redisConfig" location="classpath:redis.properties" />
<context:property-placeholder ignore-unresolvable="true" properties-ref="redisConfig"/>
<context:component-scan base-package="com.j2ee.*" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"/><!--是否使用連線池-->
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="database" value="1" /><!--配置資料庫名稱-->
<property name="timeout" value="${redis.timeout}" />
<property name="poolConfig" ref="poolConfig"/><!--配置連線池-->
</bean>
<!--初始化redisTemplate-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"/>
</bean>
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<!--初始化監聽bean-->
<bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="adapterMessageDelegateListener" /> <!--監聽bean的依賴-->
<property name="serializer" ref="jdkSerializer" /><!--系列化模式-->
</bean>
<redis:listener-container>
<redis:listener ref="messageListener" method="handleMessage" serializer="jdkSerializer" topic="msgname"/>
</redis:listener-container><!--messageListener:監聽適配的bean;handleMessage:監聽適配的方法,
msgname:監聽的channel,必須與傳送訊息時設定的一樣-->
</beans>
redis.properties
redis.host=127.0.0.1
#redis的服務埠
redis.port=6379
#客戶端超時時間單位是毫秒
redis.timeout=100000
#最大連線數
redis.maxTotal=300
#最大空閒數
redis.maxIdle=100
#最大建立連線等待時間
redis.maxWait=1000
2.2 從control實際運用
@Controller
@RequestMapping(value ="/send_redis_merssagecontroller")
public class Send_REDIS_MessageController {
@Resource(name="redisTemplate")
private RedisTemplate<String, String> redisTemplate;
/**
*
* @param channel 監聽的channel名稱
* @param message 監聽的訊息內容
*/
public void sendMessage(String channel, Serializable message) {
//channel訊息監聽名稱必須與配置檔案配置的一樣
redisTemplate.convertAndSend(channel, message);
}
//http://localhost:8080/repository2018_2/send_redis_merssagecontroller/sendmsg.do
@RequestMapping(value = "sendmsg")
@ResponseBody
public String sendMsg() {
try {
MessageVo messageVo = new MessageVo();
messageVo.setDate("20171207");
messageVo.setKey("tuofuwebservices");
List<String> webmatids = new ArrayList<String>();
webmatids.add("123");
webmatids.add("456");
Map<String, List<String>> webmaidmtaids = new HashMap<String, List<String>>();
webmaidmtaids.put("1147", webmatids);
messageVo.setWebsitematids(webmaidmtaids);
//非同步傳送簡訊到redis佇列
sendMessage("msgname", messageVo);
return "訊息傳送成功";
}catch (Exception e){
e.printStackTrace();
return "訊息傳送失敗";
}
}
}
2.3 監聽類
@Component("adapterMessageDelegateListener")
public class AdapterMessageDelegateListener {
//監聽Redis訊息,監聽方法名稱需要與配置的一樣
public void handleMessage(Serializable message){
System.out.println("訊息監聽----redis訊息佇列接收訊息");
if(message instanceof MessageVo){
MessageVo messageVo = (MessageVo) message;//強制轉化為傳送的訊息model
System.out.println(messageVo.getKey()+":"+messageVo.getDate());
Map<String,List<String>> webmatids= messageVo.getWebsitematids();
for (Map.Entry<String,List<String>> data:webmatids.entrySet()){
System.out.println(data.getKey());
System.out.println(data.getValue());
}
}
}
}
model 類
public class MessageVo implements Serializable {
private String key;
private String date;
private Map<String,List<String>> websitematids;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Map<String, List<String>> getWebsitematids() {
return websitematids;
}
public void setWebsitematids(Map<String, List<String>> websitematids) {
this.websitematids = websitematids;
}
}
以上講的兩部分內容更偏向於實際應用,因研究有限,原理性的東西沒有進一步闡述,如閱讀到此博文的博友們發現有講得不到位,或講錯的部分,敬請諒解,同時麻煩指出來