1、遠程服務(wù)器端要關(guān)閉防火墻
systemctl stop firewalld.service
或者關(guān)閉后設(shè)置 禁止防火墻開機啟動。
systemctl disable firewalld.service
2.關(guān)閉 redis 保護模式,在 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)了這三步之后,就可以遠程連接 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: 遠程ip地址
port: 6379
logging:
level:
org.springframework: debug
com.xx.dao: debug
#能看到控制臺sql語句日志,方便區(qū)分是從緩存中讀數(shù)據(jù)還是訪問mapper都mysql數(shù)據(jù)
mybatis:
# mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xx.boot.po
主啟動類上加@EnableCaching注解啟用緩存
@SpringBootApplication
@EnableCaching
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
實體類 需要實現(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層實現(xiàn)類
@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) {
//可通過控制臺日志查看是否有sql輸出,有就證明讀mysql了,沒有就證明從緩存中讀的
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取出的意思,每次訪問該方法時,注解會到緩存中檢查是否有value 和 key 值作為組合的鍵值存在,若存在,則不會調(diào)用該方法,也就不會執(zhí)行mapper的方法,否則就會調(diào)用方法,并把返回值存入緩存作為value,key值則是 value = "user",key = "#id"的組合
unless是緩存條件,上例中是結(jié)果不為空時進行緩存
@CacheEvict 注解是刪除功能,當(dāng)訪問該方法時,若存在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);
}
}