日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster

發(fā)布時(shí)間:2025/3/21 javascript 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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: 1000

Code

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。