文章大纲

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

 

一、课程介绍

一共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)项目总结。

二、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之后项目架构

 

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

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

3. Jedis整合spring

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

 
<?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(); }

创建后项目结构如下

 

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

ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天(非原创)的更多相关文章

  1. SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

    SSM Spring+SpringMVC+mybatis+maven环境搭建 1.首先右键点击项目区空白处,选择new->other..在弹出框中输入maven,选择Maven Project. ...

  2. SSM(Spring + Springmvc + Mybatis)框架面试题

    JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...

  3. SSM(Spring +SpringMVC + Mybatis)框架搭建

    SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...

  4. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...

  5. SSM Spring +SpringMVC+Mybatis 整合配置 及pom.xml

    SSM Spring +SpringMVC+Mybatis 配置 及pom.xml SSM框架(spring+springMVC+Mybatis) pom.xml文件 maven下的ssm整合配置步骤

  6. SSM(Spring,SpringMVC,Mybatis)框架整合项目

    快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...

  7. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释(转)

    原文:https://blog.csdn.net/yijiemamin/article/details/51156189# 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文 ...

  8. 0927-转载:SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中用到了三个XML配置文件:web ...

  9. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿 ...

随机推荐

  1. P1115最大子段和

    题目:https://www.luogu.org/problemnew/show/P1115 很简明的一道题: 这里用了递归分治,然而似乎还有更简单的做法(贪心). 代码如下: #include< ...

  2. myeclipse配置

    windows->preference->MyEclipse->servers->tomcat 选项下 Tomcat 6.x 点 enable 设置tomcat directo ...

  3. zabbix server、agent安装及使用

    先准备yum源,当然你有打好的rpm包那更好 [root@linux-node1 ~]# cat /etc/yum.repos.d/zabbix.repo [zabbix] name=Zabbix O ...

  4. Firebug 的脚本页面不能用

    1.遇到这种情况,一般重置firebug,然后开启“脚本“功能,刷新页面,就能显示正常 2.要不就是 版本问题,50.0不行,下载回49版本就可以了

  5. ubuntu 安装配置 JDK7和Android Studio(apt-get方式)

    Ubuntu 安装配置JKD 7 $ sudo add-apt-repository ppa:webupd8team/java $ sudo apt-get update $ sudo apt-get ...

  6. 安装 Sublime Text 3及其插件

    1. 安装 Sublime Text 3 虽然现在的 Sublime 3 还处于 beta 阶段, 但已经非常稳定了, 而且速度比 Sublime 2 得到了增强. Sublime 3 可以到官网下载 ...

  7. windows设置远程连接

    两台windows机器: 1台用于开放远程连接,供其他机器连接(通常指服务器) 1台用于连接到那台机器(通常指的客户机) 一.服务器配置 1.设置开放远程连接 2.开放端口(其中windows远程桌面 ...

  8. POJ-3629

    Card Stacking Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3927   Accepted: 1541 Des ...

  9. sql之临时表

    select * from (select * from tb where id<10) as B

  10. Linux SecureCRT 完全破解

    相关说明: 上篇发了个Linux(Ubuntu) 下 SecureCRT 7 30天循环破解在启动的时候会多输入一次确认窗口, 后来maz-1网友留言说可以用Windows破解后程序替换Linux下的 ...