1.首先準備好相應的jar包
2.修改apache-tomcat/conf/context.xml,在后面加上以下部分,以下部分可以在里面添加redis服務器密碼,但是在tomcat配置文件中也要相對應
3.如何打算把項目做集群的話,需要修改tomcat的server.xml配置文件,保證集群里的tomcat服務器的端口都是唯一的
4.把準備好的Redis的jar包導入tomcat的lib文件夾里,把需要跑的項目打包成war包,放到tomcat的webapps目錄下,需要保證各個tomcat的war包是相同的。5.配置Nginx:打開conf/nginx.conf文件。我以兩臺tomcat服務器為例,需要在該文件添加以下代碼
即配置對應的端口和需要跑的項目。6.接下來配置項目里的sPRing-config.xml:在里面加入掃包的路徑<context:component-scan base-package="com.xxl.common.redis" />,在對應的路徑加入以下代碼。
package com.xxl.common.redis;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.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.lang.reflect.Method;/** * 定義配置類注入容器(@Configuration等價于xml的beans) 啟用緩存支持,定義一個RedisCacheManager管理bean * * @author Administrator * */@Configuration@EnableCachingpublic class RedisCacheConfig extends CachingConfigurerSupport { private static final String host = "127.0.0.1"; private static final int port = 6379; //private static final String pwd = "abcd6666"; @Bean(name = "redisConnectionFactory") public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName(host); redisConnectionFactory.setPort(port); //redisConnectionFactory.setPassWord(pwd); return redisConnectionFactory; } @Bean(name = "redisTemplate") public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(cf); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); return redisTemplate; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(7200); return cacheManager; } /** * 自定義緩存策略(保證key值的唯一性) * * @return */ @Bean public KeyGenerator customKeyGenerator() { 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()); } return sb.toString(); } }; }}package com.xxl.common.redis;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;public class RedisService { public static ApplicationContext ctx; public static RedisConnectionFactory redisConnectionFactory; public static RedisTemplate<String, String> redis; public static void main(String[] args) { ctx = new ClassPathXmlApplicationContext("spring-config.xml"); redis = ctx.getBean("redisTemplate", RedisTemplate.class); redis.opsForHash().put("b", "1", "fff"); String key = (String) redis.opsForHash().get("b","1"); System.err.println(key); //redis.delete(key); //System.out.println(redis); //redis.opsForValue().set("xman", "abc"); //System.err.println(redis.opsForValue().get("xman")); } }然后把兩個tomcat跑起來,再啟動nginx和Redis服務器,進入redis客戶端:redis-cli.exe。如果想做到登錄的唯一性可以在登錄里加入以下代碼/** * 緩存session到redis */ void redisSession(UserInfo userInfoDB, String sessionId) { // 將sessionid緩存到redis,防止一個賬戶多次登陸 if (null != redisTemplate.opsForValue().get(userInfoDB.getId() + "XxlUid")) { String redisValue = (String) redisTemplate.opsForValue().get(userInfoDB.getId() + "XxlUid"); // 刪除之前用戶sessionid redisTemplate.delete(redisValue); } redisTemplate.opsForValue().set(userInfoDB.getId() + "XxlUid", sessionId); }
如果為了減輕數據庫端的負擔,優化性能可以在業務層加入以下注解:
package com.xxl.common.bo;import java.util.List;import javax.annotation.Resource;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import com.xxl.common.dao.IRedisTestDao;import com.xxl.common.entity.RedisTest;@Service("redisTestBo")public class RedisTestBo implements IRedisTestBo { private final Log logger = LogFactory.getLog(this.getClass()); @Resource private IRedisTestDao redisTestDao;//下面注解表示除了第一次直接查詢數據庫以外,以后都是從redis數據庫里面讀取數據而不是MySQL數據庫,allEntries=true的//意思是對數據進行了增刪改后更新到mysql數據庫 @CacheEvict(value = { "selectByCode"}, allEntries = true) public int deleteByCode(String code) { logger.debug("every body 一起嗨 // delete"); redisTestDao.deleteByCode(code); return 1; } @CacheEvict(value = { "selectByCode"}, allEntries = true) public int insert(RedisTest redisTest) { logger.debug("every body 一起嗨 // add"); return redisTestDao.insert(redisTest); } @Cacheable("selectByCode") public List<RedisTest> selectByCode(String code) { logger.debug("every body 一起嗨 // read"); return redisTestDao.selectByCode(code); } @CacheEvict(value = { "selectByCode"}, allEntries = true) public int updateById(RedisTest redisTest) { logger.debug("every body 一起嗨 // update"); return redisTestDao.updateById(redisTest); } }
新聞熱點
疑難解答