瑞吉外卖项目-2:项目优化

缓存优化(对应V1.0)

问题说明

image-20220515151150315

环境搭建

1.在码云平台新建一个私人仓库

2.IDEA -> VCS -> Create Git Repository -> 选择本项目根目录创建本地仓库

3.配置 .gitignore 文件,即配置不需要 Git 管理的文件

4.通过 add 将项目添加到 Git 仓库

5.通过 commit 和 submit 将项目提交到远程仓库

6.创建新的分支,把缓存代码都放入该分支中

7.导入 Maven 坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

8.在 application.yml 中添加 Redis 配置信息

9.添加自定义得 Redis 配置信息,反序列化

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();

        //默认的Key序列化器为:JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化

        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

缓存短信验证码

image-20220526222354992

Redis 缓存的使用和数据库功能差不多,都是进行数据存储查询的,因此操作也是类似的。

操作:在UserConroller中进行改造

1.注入RedisTemplate
@Autowired
private RedisTemplate redisTemplate;
2.将生成的验证码缓存到Redis,并设置有效期为5分钟
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
3.Redis中获取验证码
Object code1 = redisTemplate.opsForValue().get(phone);
4.如果登录成功,删除Redis中缓存的验证码
redisTemplate.delete(phone);

缓存菜品信息

image-20220526224936074 image-20220527201918957 image-20220527202006695

Spring Cache

image-20220527154350970 image-20220527154421590

SpEL:Spring Expression Language (SpEL) expression for computing the key dynamically.

/**
 * CachePut:将方法返回值放入缓存
 * value:缓存的名称,每个缓存名称下面可以有多个key
 * key:缓存的key
 */
@CachePut(value = "userCache",key = "#user.id")
@PostMapping
public User save(User user){
    userService.save(user);
    return user;
}
/**
 * CacheEvict:清理指定缓存
 * value:缓存的名称,每个缓存名称下面可以有多个key
 * key:缓存的key
 */
@CacheEvict(value = "userCache",key = "#p0")
//@CacheEvict(value = "userCache",key = "#root.args[0]")
//@CacheEvict(value = "userCache",key = "#id")
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){
    userService.removeById(id);
}
//@CacheEvict(value = "userCache",key = "#p0.id")
//@CacheEvict(value = "userCache",key = "#user.id")
//@CacheEvict(value = "userCache",key = "#root.args[0].id")
@CacheEvict(value = "userCache",key = "#result.id")
@PutMapping
public User update(User user){
    userService.updateById(user);
    return user;
}
/**
 * Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
 * value:缓存的名称,每个缓存名称下面可以有多个key
 * key:缓存的key
 * condition:条件,满足条件时才缓存数据
 * unless:满足条件则不缓存
 */
@Cacheable(value = "userCache",key = "#id",unless = "#result == null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){
    User user = userService.getById(id);
    return user;

@Cacheable(value = "userCache",key = "#user.id + '_' + #user.name")
@GetMapping("/list")
public List<User> list(User user){
    LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(user.getId() != null,User::getId,user.getId());
    queryWrapper.eq(user.getName() != null,User::getName,user.getName());
    List<User> list = userService.list(queryWrapper);
    return list;
}
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

缓存套餐数据

image-20220527195029145 image-20220527195138125

注意:返回对象是R,R需要实现序列化,否则无法进行缓存

数据库优化(对应V1.1)

问题描述

image-20220527202159788 image-20220527202321558

MySQL主从复制

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KofIY4Ti-1654599930791)(E:\CodeStudy\学习笔记\笔记图库\博客项目图库\image-20220527203558853.png)]

image-20220603095337247

1.可以在虚拟机通过克隆形成两台服务器,但是除了配置 server-id 不一样,还要修改 server-uuid(在 /var/lib/mysql/auto.cnf 下),其中两个细节要注意,就是修改后要重启 mysql 服务,以及要先 stop slave,再 start slave,即进行刷新,否则还是会有问题。

2.本地连接虚拟机中的 mysql : 创建新用户并授权(之前是1035错误,因为一般mysql是不允许除了本机用户以外的用户进行访问的,所以需要给特定ip的用户开放权限,通过这个用户去访问连接创建用户并附有所有权限),从库将用户名改为root3。

image-20220603225006458image-20220603225142281

image-20220603104232124 image-20220603104317718

image-20220603104542909我的用户名为yawn,密码一样

image-20220603105132577 image-20220603124513186 image-20220603124737009

image-20220603124818936具体的值根据具体状态设定

image-20220603200728933

读写分离

image-20220604150334720 image-20220604153244357 image-20220604153425984

在项目中这样配置即可,先建立新的分支,再在新的分支中修改。

<dependency>
   <groupId>org.apache.shardingsphere</groupId>
   <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
   <version>4.0.0-RC1</version>
</dependency>
spring:
  shardingsphere:
    datasource:
      names:
        master,slave
      # 主数据源
      master:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.98.132:3306/reggie?characterEncoding=utf-8
        username: root2
        password: Root@123456
      # 从数据源
      slave:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.98.133:3306/reggie?characterEncoding=utf-8
        username: root3
        password: Root@123456
    masterslave:
      # 读写分离配置
      load-balance-algorithm-type: round_robin
      # 最终的数据源名称
      name: dataSource
      # 主库数据源名称
      master-data-source-name: master
      # 从库数据源名称列表,多个逗号分隔
      slave-data-source-names: slave
    props:
      sql:
        show: true #开启SQL显示,默认false
  main:
    allow-bean-definition-overriding: true #允许bean定义覆盖

Nginx

image-20220604164655106 image-20220604165026181 image-20220604191844577

常用命令:

image-20220604192527093

image-20220604192731254在/sbin目录下执行

在 /etc/profile 的 PATH 路径下添加 /usr/local/nginx/sbin:(冒号是串联作用),即可在任意位置使用nginx常用命令

Nginx配置文件结构

image-20220604214427635

Nginx 具体应用

image-20220604213555779 image-20220604214828387 image-20220604215536150 image-20220604215625918 image-20220604220119203 image-20220604221014300

前后端分离及项目部署(对应V1.2)

image-20220604224244542 image-20220604224400854

这里的接口指的是一个 http 请求地址,主要就是去定义:请求路径、请求方式、请求参数响应数据内容等。

image-20220604225321657

YApi

image-20220604225906344 image-20220604225940669

接口的定义可以自己写,也可以通过 Swagger、Postman 生成的 Json 文件统一导入,更加方便。

详细使用方式可以使用时再查询网上资源。

Swagger

image-20220605140747244 image-20220605140924320
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>
image-20220606104419325
image-20220606104728899
image-20220606104950895 image-20220606154744048

项目部署

image-20220607001253858 image-20220607001500395

项目部署就是把整个项目部署在 Linux 系统上,这样整个服务都是运行在 Linux 系统上了。

部署的步骤没有编写,实际需要时可以再查阅资料,部署算是相对固定的流程。

;" />

项目部署

image-20220607001253858 image-20220607001500395

项目部署就是把整个项目部署在 Linux 系统上,这样整个服务都是运行在 Linux 系统上了。

部署的步骤没有编写,实际需要时可以再查阅资料,部署算是相对固定的流程。

至此,整个项目全部已经结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值