文章大纲

一、课程介绍
二、Redis基础实战
三、Redis之高可用、集群、云平台搭建实战
四、淘淘商城Jedis整合spring
五、项目源码与资料下载
六、参考文章

  ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天 随笔 第1张

一、课程介绍

一共14天课程
(1)第一天:电商行业的背景。淘淘商城的介绍。搭建项目工程。Svn的使用。
(2)第二天:框架的整合。后台管理商品列表的实现。分页插件。
(3)第三天:后台管理。商品添加。商品类目的选择、图片上传、富文本编辑器的使用。
(4)第四天:商品规格的实现。
(5)第五天:商城前台系统的搭建。首页商品分类的展示。Jsonp。
(6)第六天:cms系统的实现。前台大广告位的展示。
(7)第七天:cms系统添加缓存。Redis。缓存同步。
(8)第八天:搜索功能的实现。使用solr实现搜索。
(9)第九天:商品详情页面的展示。
(10)第十天:单点登录系统。Session共享。
(11)第十一天:购物车订单系统的实现。
(12)第十二天:nginx。反向代理工具。
(13)第十三天:redis集群的搭建、solr集群的搭建。系统的部署。
(14)项目总结。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

二、Redis基础实战

Redis的基础包括以下内容,可参考文章https://www.cnblogs.com/WUXIAOCHANG/p/10832330.html进行学习
(1)安装并设置开机自动启动
(2)Redis文件结构
(3)Redis启动方式
(4)Redis持久化
(5)Redis配置文件详解
(7)Redis图形化工具
(8)Java之Jedis连接Redis单机

三、Redis之高可用、集群、云平台搭建实战

https://www.cnblogs.com/WUXIAOCHANG/p/10851334.html

四、淘淘商城Jedis整合spring

1. 添加Reids之后项目架构

  ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天 随笔 第2张

2. Redis在淘淘商城解决方案

在taotao-rest工程中发布一个服务。当后台管理系统修改内容后,调用此服务,同步缓存。

3. Jedis整合spring

3.1 resources文件夹中添加配置文件
在resources文件夹中创建applicationContext-redis.xml配置文件

  ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天 随笔 第3张
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 连接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大连接数 --> <property name="maxTotal" value="30" /> <!-- 最大空闲连接数 --> <property name="maxIdle" value="10" /> <!-- 每次释放连接的最大数目 --> <property name="numTestsPerEvictionRun" value="1024" /> <!-- 释放连接的扫描间隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <!-- 连接最小空闲时间 --> <property name="minEvictableIdleTimeMillis" value="1800000" /> <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在获取连接的时候检查有效性, 默认false --> <property name="testOnBorrow" value="true" /> <!-- 在空闲时检查有效性, 默认false --> <property name="testWhileIdle" value="true" /> <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="false" /> </bean> <!-- 单机版的redis环境配置 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="192.168.3.153"/> <constructor-arg name="port" value="6379"/> <constructor-arg name="poolConfig" ref="jedisPoolConfig"/> </bean> <!-- 集群redis环境的配置,任选其一 --> <!-- bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7006"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7007"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.25.153"></constructor-arg> <constructor-arg index="1" value="7008"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> </bean> --> <!-- redis客户端实现类 --> <!-- 单机版 --> <bean id="jedisClient" class="com.taotao.rest.dao.impl.JedisClientPool"/> <!-- 集群版 --> <!--<bean id="jedisClient" class="com.taotao.rest.dao.impl.JedisClientCluster"/> --> </beans> 

温馨提示:
(1)jedisPoolConfig是连接池的配置信息,无论是单机还是集群都适用
(2)jedisClient为单机版的配置信息
(3)jedisCluster是集群版的配置信息

3.2 单机版整合测试
com.taotao.rest.dao包中创建连接池JedisClientPool.java

/** * redis单机版客户端 */ public class JedisClientPool implements JedisClient{ @Autowired private JedisPool jedisPool; @Override public String get(String key) { Jedis jedis = jedisPool.getResource(); String result = jedis.get(key); jedis.close(); return result; } @Override public String set(String key, String value) { Jedis jedis = jedisPool.getResource(); String string = jedis.set(key, value); jedis.close(); return string; } @Override public long incr(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.incr(key); jedis.close(); return result; } @Override public Long hset(String hkey, String key, String value) { Jedis jedis = jedisPool.getResource(); Long hset = jedis.hset(hkey, key, value); jedis.close(); return hset; } @Override public String hget(String hkey, String key) { Jedis jedis = jedisPool.getResource(); String result = jedis.hget(hkey, key); jedis.close(); return result; } @Override public Long del(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.del(key); jedis.close(); return result; } @Override public Long hdel(String hkey, String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hdel(hkey, key); jedis.close(); return result; } @Override public Long expire(String key, int second) { Jedis jedis = jedisPool.getResource(); Long result = jedis.expire(key, second); return result; } } 

com.taotao.rest.dao包中创建测试类JedisTest.java

public class JedisTest { // 单机版测试Jedis,不使用连接池 public void testJedis() { // 创建Jedis对象 Jedis jedis = new Jedis("127.0.0.1", 6379); // 设置对象 jedis.set("key1", "jedis test"); String string = jedis.get("key1"); System.out.println(string); // 关闭jedis jedis.close(); } // 单机版测试Jedis,这是使用连接池的方式来获取redis的资源 public void testJedispool() { JedisPool pool = new JedisPool("127.0.0.1", 6379); Jedis resource = pool.getResource(); String string = resource.get("key1"); System.out.println(string); // 不要忘记关闭连接池了 pool.close(); resource.close(); } 

创建后项目结构如下

  ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天 随笔 第4张

3.3 集群版整合测试
com.taotao.rest.dao包中创建集群版客户端JedisClientCluster.java

/** * redis集群版客户端 */ public class JedisClientCluster implements JedisClient { @Autowired private JedisCluster jedisCluster; @Override public String get(String key) { String string = jedisCluster.get(key); return string; } @Override public String set(String key, String value) { String string = jedisCluster.set(key, value); return string; } @Override public long incr(String key) { Long result = jedisCluster.incr(key); return result; } @Override public Long hset(String hkey, String key, String value) { Long result = jedisCluster.hset(hkey, key, value); return result; } @Override public String hget(String hkey, String key) { String string = jedisCluster.hget(hkey, key); return string; } @Override public Long del(String key) { Long result = jedisCluster.del(key); return result; } @Override public Long hdel(String hkey, String key) { Long result = jedisCluster.hdel(hkey, key); return result; } @Override public Long expire(String key, int second) { Long result = jedisCluster.expire(key, second); return result; } } 

com.taotao.rest.dao包中使用测试类JedisTest.java

    // 集群版,测试redis集群环境 public void testJiQun(){ HashSet<HostAndPort> nodes=new HashSet<HostAndPort>(); //这里的ip和后面的端口是,在linux系统的ip和配置的不同的redis的端口 nodes.add(new HostAndPort("127.0.0.1",6379)); nodes.add(new HostAndPort("127.0.0.1",6380)); nodes.add(new HostAndPort("127.0.0.1",6381)); nodes.add(new HostAndPort("127.0.0.1",6382)); nodes.add(new HostAndPort("127.0.0.1",6383)); nodes.add(new HostAndPort("127.0.0.1",6384)); JedisCluster cluster=new JedisCluster(nodes); String string=cluster.get("key4"); System.out.println(string); cluster.close(); } 

4. 把缓存添加到业务逻辑

4.1 resources文件夹中添加配置
resources文件夹中新增resource.properties配置文件

#redis中内容缓存的key REDIS_CONTENT_KEY=REDIS_CONTENT #redis中商品信息缓存的key REDIS_TAOTAO_ITEM_KEY=TAOTAO_ITEM #过期时间60*60*24 REDIS_TAOTAO_ITME_EXPIRE=86400 

4.2 创建缓存使用业务逻辑类
com.taotao.rest.service包下创建ContentServiceImpl.java进行缓存使用

package com.taotao.rest.service.impl; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.taotao.common.pojo.TaotaoResult; import com.taotao.common.utils.ExceptionUtil; import com.taotao.common.utils.JsonUtils; import com.taotao.mapper.TbContentMapper; import com.taotao.pojo.TbContent; import com.taotao.pojo.TbContentExample; import com.taotao.pojo.TbContentExample.Criteria; import com.taotao.rest.dao.JedisClient; import com.taotao.rest.service.ContentService; /** * 内容管理服务 */ @Service public class ContentServiceImpl implements ContentService { @Autowired private TbContentMapper contentMapper; @Autowired private JedisClient jedisClient; @Value("${REDIS_CONTENT_KEY}") private String REDIS_CONTENT_KEY; @Override public TaotaoResult getContentList(long categoryId) { //添加取缓存的逻辑 //缓存不能影响正常逻辑 try { String result = jedisClient.hget(REDIS_CONTENT_KEY, categoryId+""); //判断结果是否为空 if (!StringUtils.isBlank(result)) { List<TbContent> list = JsonUtils.jsonToList(result, TbContent.class); return TaotaoResult.ok(list); } } catch (Exception e) { e.printStackTrace(); } //缓存逻辑结束 List<TbContent> list = null; try { TbContentExample example = new TbContentExample(); Criteria criteria = example.createCriteria(); // 设置查询条件 criteria.andCategoryIdEqualTo(categoryId); // 执行查询 list = contentMapper.selectByExample(example); } catch (Exception e) { e.printStackTrace(); return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } //把内容添加到缓存中 try { jedisClient.hset(REDIS_CONTENT_KEY, categoryId + "", JsonUtils.objectToJson(list)); } catch (Exception e) { // TODO: handle exception } //end return TaotaoResult.ok(list); } } 

5. 同步缓存服务

  当数据库中的内容信息发生改变后,例如首页大广告为的广告内容发生变化后,如何实现redis中的数据同步更新呢?可以在taotao-rest工程中发布一个服务,就是专门同步数据用的,其实只需要把缓存中的数据清空即可。当管理后台更新了内容信息后,需要调用此服务。
  此服务不需要mapper内容。只需要JedisCluster对象。

5.1 创建缓存同步服务逻辑类
com.taotao.rest.service包中创建RedisSyncServiceImpl.java

@Service public class RedisSyncServiceImpl implements RedisSyncService { @Autowired private JedisClient jedisClient; @Value("${REDIS_CONTENT_KEY}") private String REDIS_CONTENT_KEY; @Override public TaotaoResult syncContent(String key) { jedisClient.hdel(REDIS_CONTENT_KEY, key); return TaotaoResult.ok(); } } 

在com.taotao.rest.controller包中创建缓存同步的服务类RedisSyncController.java

@Controller @RequestMapping("/sync") public class RedisSyncController { @Autowired private RedisSyncService redisSyncService; @RequestMapping("/content/{categoryId}") @ResponseBody public TaotaoResult syncContent(@PathVariable String categoryId) { try { redisSyncService.syncContent(categoryId); } catch (Exception e) { e.printStackTrace(); return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } return TaotaoResult.ok(); } } 

五、项目源码与资料下载

链接:https://pan.baidu.com/s/1XsTBNB8RCT-c2SaFdOTyqA
提取码:t05n

六、参考文章

http://yun.itheima.com/course?hm

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄