pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>redis-test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from update -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
application.yml
spring:
redis:
password:
cluster:
nodes: 192.168.37.129:7000,192.168.37.129:7001,192.168.37.129:7002,192.168.37.129:7003,192.168.37.129:7004,192.168.37.129:7005
max-redirects: 6
lettuce:
pool:
max-idle: 16
max-active: 32
min-idle: 8
需要搭建6个节点的主从环境,三主三从,参考我的博客
docker版 https://blog.csdn.net/huangbaokang/article/details/115373616?spm=1001.2014.3001.5501
源码安装版
https://blog.csdn.net/huangbaokang/article/details/88416375
启动类
package com.hbk;
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);
}
}
控制器类
package com.hbk.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/test")
public String test(){
redisTemplate.opsForValue().set("spring-r-cluster-1", "黄宝康");
redisTemplate.opsForValue().set("spring-r-cluster-2", 456);
System.out.println(redisTemplate.opsForValue().get("spring-r-cluster-1"));
System.out.println(redisTemplate.opsForValue().get("spring-r-cluster-2"));
return "ok";
}
}
浏览器访问localhost:8080/test
控制台输出了如下
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程
博客若对你有用,欢迎扫码打赏,你的打赏是我写作的动力。