springboot整合redis集羣
集羣部署看另一篇文章 https://blog.csdn.net/qq_42011565/article/details/101546523
項目圖:
整合開始
1.導入pom.xml依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>szw</groupId>
<artifactId>springboot_redis_sentinel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_redis_sentinel</name>
<description>springboot整合redis哨兵</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>com.sze.redis.SzwRedisApplication</start-class>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencies>
<!-- 使用web啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 使用aop模板啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- redis緩存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<!-- 要將源碼放上去,需要加入這個插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.配置依賴文件application.proterties
##單服務器
# Redis服務器地址
#spring.redis.host=112.74.37.85
# Redis服務器連接端口
#spring.redis.port=6379
# Redis服務器連接密碼(默認爲空)
#spring.redis.password=
#集羣
spring.redis.cluster.nodes=112.74.37.85:7001,112.74.37.85:7002,112.74.37.85:7003,112.74.37.85:7004,112.74.37.85:7005,112.74.37.85:7006
## 連接池最大連接數(使用負值表示沒有限制)
spring.redis.pool.max-active=300
## Redis數據庫索引(默認爲0)
spring.redis.database=0
## 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.pool.max-wait=-1
## 連接池中的最大空閒連接
spring.redis.pool.max-idle=100
## 連接池中的最小空閒連接
spring.redis.pool.min-idle=20
## 連接超時時間(毫秒)
spring.redis.timeout=60000
3.實體類Student
package cn.zds.redis_demo.pojo;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private String studentNo;
public Student(Integer id, String name, String studentNo) {
this.id = id;
this.name = name;
this.studentNo = studentNo;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", studentNo='" + studentNo + '\'' +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getStudentNo() {
return studentNo;
}
}
注意點,因爲使用了jackson,所以實體類提供空構造方法
4.redisService封裝
package cn.zds.redis_demo.service;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* RedisService
*/
@Component
public class RedisService {
@Autowired
StringRedisTemplate redisTemplate; //操作redis服務
@Autowired
private ObjectMapper objectMapper; //jackson
ValueOperations<String, String> stringRedis;
@PostConstruct
public void init(){
stringRedis=redisTemplate.opsForValue();
}
public void setString (String key,String Value){
stringRedis.set(key, Value);
}
public String getString (String key){
String value = stringRedis.get(key);
return value;
}
public <T> T getObject (String key,Class<T> valueType) throws Exception{
String objectJson = stringRedis.get(key);//拿redis數據
T t=null;
try {
t = objectMapper.readValue( objectJson,valueType);//轉成傳入的student類型
}catch (Exception e){
t=null;
System.err.println(e);
}
return t;
}
public void setObject (String key,Object Value)throws Exception{
String studentJson = objectMapper.writeValueAsString( Value);
stringRedis.set(key, studentJson);
}
}
5.啓動器
package cn.zds.redis_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6.測試
package cn.zds.redis_demo;
import cn.zds.redis_demo.pojo.Student;
import cn.zds.redis_demo.service.RedisService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class redisAndJacksonTest {
@Autowired
RedisService redisService;
/**
* 注意點,pojo需要實現無參構造
* @throws Exception
*/
//object
@Test
public void setOrGetObject ()throws Exception{
Student zds = new Student(10086, "zds02", "10014");
redisService.setObject(zds.getName(),zds);
Student name = redisService.getObject("zds02", Student.class);
System.err.println(name);
}
//list
@Test
public void getOrSetList ()throws Exception{
List<Student> students= new ArrayList<>();
Student zds = new Student(10086, "zds02", "10014");
Student zds2 = new Student(100861, "zds023", "100144");
students.add(zds);
students.add(zds2);
redisService.setObject("list",students);
ArrayList<Student> list = redisService.getObject("list", ArrayList.class);
System.err.println(list);
}
//map
@Test
public void getOrSetMap ()throws Exception{
Map<String,Object> students= new HashMap<>();
Student zds = new Student(10086, "zds02", "10014");
Student zds2 = new Student(100861, "zds023", "100144");
students.put(zds.getName(),zds);
students.put(zds2.getName(),zds2);
redisService.setObject("map",students);
HashMap<String,Object> map = redisService.getObject("map", HashMap.class);
System.err.println(map);
}
}