1、遠(yuǎn)程服務(wù)器端要關(guān)閉防火墻
systemctl stop firewalld.service
或者關(guān)閉后設(shè)置 禁止防火墻開(kāi)機(jī)啟動(dòng)章郁。
systemctl disable firewalld.service
2.關(guān)閉 redis 保護(hù)模式枉氮,在 redis.conf 文件中
[root@localhost redis-7.0.7]# vi redis.conf
修改 protected 為 no,如下:
image.png
注釋掉 redis 的 ip 地址綁定暖庄,還是在 redis.conf 中聊替,將 bind:127.0.0.1 注釋掉,如下:
image.png
確認(rèn)了這三步之后培廓,就可以遠(yuǎn)程連接 redis 了
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--熱部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<!-- redis相關(guān) -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8089
servlet:
context-path: /elm
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/emp?characterEncoding=utf-8&useSSL=false
username: root
password: root
redis:
database: 0
host: 遠(yuǎn)程ip地址
port: 6379
logging:
level:
org.springframework: debug
com.xx.dao: debug
#能看到控制臺(tái)sql語(yǔ)句日志惹悄,方便區(qū)分是從緩存中讀數(shù)據(jù)還是訪問(wèn)mapper都mysql數(shù)據(jù)
mybatis:
# mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xx.boot.po
主啟動(dòng)類(lèi)上加@EnableCaching注解啟用緩存
@SpringBootApplication
@EnableCaching
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
實(shí)體類(lèi) 需要實(shí)現(xiàn) Serializable 序列化
public class Dept implements Serializable{
private Integer deptno;
private String dname;
private String loc;
//getter,setter省略
}
mapper層接口
@Mapper
public interface DeptMapper {
@Select("select * from dept")
public List<Dept> getDept();
@Select("select * from dept where deptno = #{deptno}")
public Dept getDeptById(Integer deptno);
@Delete("delete from dept where deptno = #{deptno}")
public Integer delDeptById(Integer deptno);
}
service層接口
public interface DeptService {
public List<Dept> getDept();
public Dept getDeptById(Integer deptno);
public Integer delDeptById(Integer deptno);
}
service層實(shí)現(xiàn)類(lèi)
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
DeptMapper deptMapper;
@Autowired
RedisTemplate<Object, Object> redisTemplate;
public List<Dept> getDept() {
List<Dept> list = null;
ValueOperations<Object, Object> op = redisTemplate.opsForValue();
if(op.get("user")==null) {
list = deptMapper.getDept( );
op.set("user", list);
}else {
list = (List<Dept>) op.get("user");
System.out.println(list);
}
return list;
}
@Cacheable(value = "dept",key = "#deptno",unless = "#result==null")
public Dept getDeptById(Integer deptno) {
//可通過(guò)控制臺(tái)日志查看是否有sql輸出,有就證明讀mysql了肩钠,沒(méi)有就證明從緩存中讀的
Dept dept = deptMapper.getDeptById(deptno);
return dept;
}
@CacheEvict(value = "dept",key = "#deptno")
public Integer delDeptById(Integer deptno) {
return deptMapper.delDeptById(deptno);
}
}
@Cacheable解的作用是將 value = "dept",key = "#deptno" 值作為組合泣港,作為緩存數(shù)據(jù)的鍵值 , #的參數(shù)是將方法參數(shù)deptno取出的意思,每次訪問(wèn)該方法時(shí)价匠,注解會(huì)到緩存中檢查是否有value 和 key 值作為組合的鍵值存在当纱,若存在,則不會(huì)調(diào)用該方法踩窖,也就不會(huì)執(zhí)行mapper的方法坡氯,否則就會(huì)調(diào)用方法,并把返回值存入緩存作為value,key值則是 value = "user",key = "#id"的組合
unless是緩存條件毙石,上例中是結(jié)果不為空時(shí)進(jìn)行緩存
@CacheEvict 注解是刪除功能廉沮,當(dāng)訪問(wèn)該方法時(shí),若存在value = "user",key = "#id" 組合而成的鍵值徐矩,就把該緩存數(shù)據(jù)刪除
controller層
@RestController
public class HelloController {
@Autowired
DeptService deptService;
@RequestMapping("/getDept")
public List<Dept> getDept(){
return deptService.getDept();
}
@RequestMapping("/getDeptById")
public Dept getDeptById(Integer deptno) {
return deptService.getDeptById(deptno);
}
@RequestMapping("/delDeptById")
public Integer delDeptById(Integer deptno) {
return deptService.delDeptById(deptno);
}
}