當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster
生活随笔
收集整理的這篇文章主要介紹了
Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- Pre
- Jedis操作Redis Cluster
- 添加依賴
- Code
- Spring Boot 操作Redis Cluster
- 引入 依賴
- application.yml
- Code
Pre
Redis進(jìn)階-5.x 單節(jié)點(diǎn) 及Redis Cluster 3主3從集群部署搭建了Redis Cluster 集群,接下來我們看下如何使用Java 代碼來操作集群
Jedis操作Redis Cluster
添加依賴
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency>Code
import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPoolConfig;import java.io.IOException; import java.util.HashSet; import java.util.Set;public class JedisClusterDemo {public static void main(String[] args) throws IOException {JedisPoolConfig config = new JedisPoolConfig();config.setMaxTotal(20);config.setMaxIdle(10);config.setMinIdle(5);Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();jedisClusterNode.add(new HostAndPort("192.168.18.131", 8001));jedisClusterNode.add(new HostAndPort("192.168.18.131", 8004));jedisClusterNode.add(new HostAndPort("192.168.18.132", 8002));jedisClusterNode.add(new HostAndPort("192.168.18.132", 8005));jedisClusterNode.add(new HostAndPort("192.168.18.133", 8003));jedisClusterNode.add(new HostAndPort("192.168.18.133", 8006));JedisCluster jedisCluster = null;try {//connectionTimeout:指的是連接一個(gè)url的連接等待時(shí)間//soTimeout:指的是連接上一個(gè)url,獲取response的返回等待時(shí)間jedisCluster = new JedisCluster(jedisClusterNode, 6000, 5000, 10, "artisan", config);System.out.println(jedisCluster.set("clusterArtisan", "artisanValue"));System.out.println(jedisCluster.get("clusterArtisan"));} catch (Exception e) {e.printStackTrace();} finally {if (jedisCluster != null)jedisCluster.close();}} }運(yùn)行
去redis中查看下
Spring Boot 操作Redis Cluster
引入 依賴
<dependency> ` <groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId> </dependency>application.yml
server:port: 8080spring:redis:database: 0timeout: 3000password: artisan# sentinel: #哨兵模式# master: mymaster #主服務(wù)器所在集群名稱# nodes: 192.168.18.131:26379,192.168.18.132:26379,192.168.18.133:26379cluster:nodes: 192.168.18.131:8001, 192.168.18.131:8004, 192.168.18.132:8002 ,192.168.18.132:8005, 192.168.18.133:8003 ,192.168.18.133:8006lettuce:pool:max-idle: 50min-idle: 10max-active: 100max-wait: 1000Code
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class IndexController {private static final Logger logger = LoggerFactory.getLogger(IndexController.class);@Autowiredprivate StringRedisTemplate stringRedisTemplate;/*** 測(cè)試節(jié)點(diǎn)掛了哨兵重新選舉新的master節(jié)點(diǎn),客戶端是否能動(dòng)態(tài)感知到** @throws InterruptedException*/@RequestMapping("/test_sentinel")public void testSentinel() throws InterruptedException {int i = 1;while (true) {try {stringRedisTemplate.opsForValue().set("art" + i, i + ""); //jedis.set(key,value);System.out.println("設(shè)置key:" + "zhuge" + i);i++;Thread.sleep(1000);} catch (Exception e) {logger.error("錯(cuò)誤:", e);}}}@RequestMapping("/test_cluster")public void testCluster() throws InterruptedException {stringRedisTemplate.opsForValue().set("SmartArtisan" , "ClusterArtisanValue"); //jedis.set(key,value);System.out.println("設(shè)置key:" + "SmartArtisan" );String value = stringRedisTemplate.opsForValue().get("SmartArtisan");System.out.println("SmartArtisan 對(duì)應(yīng)的Value:" + value);} }訪問 http://127.0.0.1:8080/test_cluster
在redis上查看一下
總結(jié)
以上是生活随笔為你收集整理的Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis进阶-5.x 单节点 及Re
- 下一篇: gradle idea java ssm