1.首先準(zhǔn)備好相應(yīng)的jar包

2.修改apache-tomcat/conf/context.xml,在后面加上以下部分,以下部分可以在里面添加redis服務(wù)器密碼,但是在tomcat配置文件中也要相對(duì)應(yīng)

3.如何打算把項(xiàng)目做集群的話,需要修改tomcat的server.xml配置文件,保證集群里的tomcat服務(wù)器的端口都是唯一的

4.把準(zhǔn)備好的Redis的jar包導(dǎo)入tomcat的lib文件夾里,把需要跑的項(xiàng)目打包成war包,放到tomcat的webapps目錄下,需要保證各個(gè)tomcat的war包是相同的。5.配置Nginx:打開(kāi)conf/nginx.conf文件。我以?xún)膳_(tái)tomcat服務(wù)器為例,需要在該文件添加以下代碼

即配置對(duì)應(yīng)的端口和需要跑的項(xiàng)目。6.接下來(lái)配置項(xiàng)目里的sPRing-config.xml:在里面加入掃包的路徑<context:component-scan base-package="com.xxl.common.redis" />,在對(duì)應(yīng)的路徑加入以下代碼。
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;/** * 定義配置類(lèi)注入容器(@Configuration等價(jià)于xml的beans) 啟用緩存支持,定義一個(gè)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")); } }然后把兩個(gè)tomcat跑起來(lái),再啟動(dòng)nginx和Redis服務(wù)器,進(jìn)入redis客戶(hù)端:redis-cli.exe。如果想做到登錄的唯一性可以在登錄里加入以下代碼/** * 緩存session到redis */ void redisSession(UserInfo userInfoDB, String sessionId) { // 將sessionid緩存到redis,防止一個(gè)賬戶(hù)多次登陸 if (null != redisTemplate.opsForValue().get(userInfoDB.getId() + "XxlUid")) { String redisValue = (String) redisTemplate.opsForValue().get(userInfoDB.getId() + "XxlUid"); // 刪除之前用戶(hù)sessionid redisTemplate.delete(redisValue); } redisTemplate.opsForValue().set(userInfoDB.getId() + "XxlUid", sessionId); }
如果為了減輕數(shù)據(jù)庫(kù)端的負(fù)擔(dān),優(yōu)化性能可以在業(yè)務(wù)層加入以下注解:
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;//下面注解表示除了第一次直接查詢(xún)數(shù)據(jù)庫(kù)以外,以后都是從redis數(shù)據(jù)庫(kù)里面讀取數(shù)據(jù)而不是MySQL數(shù)據(jù)庫(kù),allEntries=true的//意思是對(duì)數(shù)據(jù)進(jìn)行了增刪改后更新到mysql數(shù)據(jù)庫(kù) @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); } }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注