一、相關(guān)鏈接
hm-dianping項目倉庫地址:https://gitee.com/huyi612/hm-dianping
項目對應(yīng)教學視頻:https://www.bilibili.com/video/BV1cr4y1671t?p=24(p24-p95)
二、下載代碼
hm-dianping項目倉庫地址:https://gitee.com/huyi612/hm-dianping
方法一:使用git clone
git clone https://gitee.com/huyi612/hm-dianping.git
方法二:直接下載程序zip壓縮包
三、如何運行這份代碼
運行sql文件
以Navicat為例
1、先新建數(shù)據(jù)庫hmdp
2、導入項目中的hmdp.sql文件
修改application.yaml配置文件
配置Mysql
要注意配置文件中默認的mysql配置是mysql5版本的配置
因此若使用的是mysql8.0+版本的mysql需要做以下修改;
1、配置驅(qū)動
driver-class-name: com.mysql.jdbc.Driver
改成
driver-class-name: com.mysql.cj.jdbc.Driver
2、配置url(這個不一定要改)
url: jdbc:mysql://127.0.0.1:3306/hmdp?useSSL=false&serverTimezone=UTC
改成
url: jdbc:mysql://localhost:3306/hmdp?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true
3、配置密碼
將password改成自己mysql的password
配置redis
這里的redis是單節(jié)點的redis,若沒有單節(jié)點的redis建議在docker里面新建一個(ps:redis版本要5.0+,因此windows中的redis可能用不了,后面會提到)
在redis配置中配置好host(宿主機ip),端口,密碼(如果有的話需要配置,沒有的話可以空著或者不寫)
完整配置文件參考
#server:
# port: 8081
spring:
application:
name: hmdp
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/hmdp?useSSL=false&serverTimezone=UTC
url: jdbc:mysql://localhost:3306/hmdp?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true
username: root
password: pwd
redis:
host: ip
port: 6379
password:
lettuce:
pool:
max-active: 10
max-idle: 10
min-idle: 1
time-between-eviction-runs: 10s
jackson:
default-property-inclusion: non_null # JSON處理時忽略非空字段
mybatis-plus:
type-aliases-package: com.hmdp.entity # 別名掃描包
logging:
level:
com.hmdp: debug
pattern:
dateformat: mm:ss.SSS
pom.xml文件修改
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.47</version>
</dependency>
注意這里的mysql驅(qū)動是mysql5版本的驅(qū)動,若使用的是mysql8.0+,則需要修改這里的版本號,對于版本號的處理可以注釋掉(因為spring-boot-starter-parent2.3.12.RELEASE默認配置的是mysql8.0+版本)或者自行指定對應(yīng)的版本號
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<!-- <version>5.1.47</version>-->
</dependency>
修改com.hmdp.config.RedissonConfig
這個程序中也需要按照自己的redis的ip和密碼進行配置
package com.hmdp.config;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redissonClient(){
// 配置
Config config = new Config();
// config.useSingleServer().setAddress("redis://192.168.150.101:6379").setPassword("123321");
// config.useSingleServer().setAddress("redis://ip:6379"); //沒有設(shè)置密碼可以省略setPassword
config.useSingleServer().setAddress("redis://ip:6379").setPassword("pwd");
// 創(chuàng)建RedissonClient對象
return Redisson.create(config);
}
}
可能出現(xiàn)的報錯解決
配置好以上幾個文件后就可以嘗試啟動項目了
對于可能出現(xiàn)的報錯可以參考以下解決方法:
1、ERR unknown command ‘XREADGROUP’. channel:
這個報錯可能是因為redis版本太低了
redis 要求版本5.0+因為程序中使用到了 stream 特性。(https://gitee.com/zhijiantianya/ruoyi-vue-pro/issues/I3QISB)
因此windows中的redis可能用不了,建議在docker中新建一個單節(jié)點的redis
參考配置文件與docker運行命令:
redis.conf:
requirepass為設(shè)置redis的密碼,可以根據(jù)需要進行設(shè)置或去掉
appendonly yes
requirepass pwd
文件結(jié)構(gòu):
docker運行命令:
注意這里的掛載地址需要根據(jù)實際的地址進行修改
docker run --name redis_6379_single -p 6379:6379 --privileged=true \
-v /mydata/redis_single_6379/data:/data \
-v /mydata/redis_single_6379/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf
新建5.0+版本的redis后記得修改配置文件與RedissonConfig中的相關(guān)信息
2、NOGROUP No such key ‘stream.orders’ or consumer group ‘g1’ in XREADGROUP with GROUP option
出現(xiàn)這個報錯是因為redis中需要先設(shè)置一個鍵,根據(jù)官方的倉庫中解決方法:
在redis中運行以下命令:
XGROUP CREATE stream.orders g1 $ MKSTREAM
docker中運行方法:
進入對應(yīng)redis容器:
docker exec -it redis_6379_single /bin/bash
使用redis-cli客戶端連接:
redis-cli -p 6379
在客戶端中輸入命令:
XGROUP CREATE stream.orders g1 $ MKSTREAM
若redis設(shè)置了密碼則可能報以下錯誤:
(error) NOAUTH Authentication required.
需要先進行身份驗證:
auth 密碼
四、接口測試注意事項
當使用postman等測試工具對部分接口進行接口測試時可能會出現(xiàn)401的錯誤,且什么都沒有返回,原因是因為請求被攔截了
而被攔截的原因是沒有在header中攜帶token,這里要注意?。?mark>header中token值對應(yīng)的參數(shù)是authorization而不是token如下圖所示:
正確的測試流程
1、localhost:8080/user/code
拿到手機驗證碼
(這里的手機號是數(shù)據(jù)庫中tb_user表中的手機號,貌似也可以使用數(shù)據(jù)庫里沒有的手機號)
后臺中拿到驗證碼:
2、localhost:8080/user/login
登錄拿到token
(返回數(shù)據(jù)中的data中的即為token)文章來源:http://www.zghlxwxcb.cn/news/detail-799201.html
3、在測試其他方法前在header中添加參數(shù)authorization,參數(shù)值為token
文章來源地址http://www.zghlxwxcb.cn/news/detail-799201.html
到了這里,關(guān)于如何運行黑馬程序員redis項目黑馬點評(hm-dianping)、常見報錯解決與部分接口的測試方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!