目錄結構以下:java
application.properties配置文件(redis的配置):web
spring.redis.host=localhost spring.redis.pool.max-idle=300 spring.redis.pool.max-wait=3000 spring.redis.timeout=3000 spring.redis.port=6379
SbootInitializer.java文件 :redis
該類的做用是爲了讓spingboot項目能夠從個人tomcat服務器啓動 spring
package com.sboot.boot; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import com.sboot.builder.SbootBuilder; /** * 用於從外部容器啓動 * * @author * @version [版本號, 2017年12月21日] * @see [相關類/方法] * @since [產品/模塊版本] */ public class SbootInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SbootBuilder.class); } }
SbootBuilder.java文件apache
package com.sboot.builder; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScans; /** * 工程組建類 * * @author * @version [版本號, 2017年12月21日] * @see [相關類/方法] * @since [產品/模塊版本] */ @SpringBootApplication //掃描包,使類中的註解組建生效 @ComponentScan(value = {"com.sboot"}) public class SbootBuilder { }
RedisConfig.java 文件tomcat
package com.sboot.redis; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.Topic; @Configuration public class RedisConfig extends RedisAutoConfiguration { @Autowired private RedisProperties properties; @Autowired private ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration; @Autowired private ObjectProvider<RedisClusterConfiguration> clusterConfiguration; @Bean public RedisKeyListener redisKeyListener(){ return new RedisKeyListener(); } @Bean public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); List<Topic> topics = new ArrayList<>(); //監聽0號庫和1號庫中的string 鍵的相關操做 topics.add(new PatternTopic("__keyevent@0__:*")); topics.add(new PatternTopic("__keyevent@1__:*")); container.addMessageListener(redisKeyListener(), topics); return container; } }
RedisKeyListener.java 文件springboot
package com.sboot.redis; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; /** * <一句話功能簡述> * <功能詳細描述> * * @version [版本號, 2017年12月29日] * @see [相關類/方法] * @since [產品/模塊版本] */ public class RedisKeyListener implements MessageListener{ /** *當0號庫和1號庫中的鍵發生相關改動該函數會被出發 * @param message * @param pattern */ @Override public void onMessage(Message message, byte[] pattern) { // TODO Auto-generated method stub System.out.println("********hasdfasdf***********"); } }
pom.xml文件服務器
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.adyx.hubin.springboot</groupId> <artifactId>sboot</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>sboot Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- Java操做redis使用的jar包 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.4.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>2.0.2</version> </dependency> <!-- junit單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>sboot</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <!-- <version>2.3.2</version> --> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
最後修改redis的配置文件app
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E Keyevent events, published with __keyevent@<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
#notify-keyspace-events ""
notify-keyspace-events Eg$dom
重啓redis服務器