元月's blog 元月's blog
首页
  • 基础
  • 并发编程
  • JVM
  • Spring
  • Redis篇
  • Nginx篇
  • Kafka篇
  • Otter篇
  • Shardingsphere篇
  • 设计模式
  • MySQL
  • Oracle
  • 基础
  • 操作系统
  • 网络
  • 数据结构
  • 技术文档
  • Git常用命令
  • GitHub技巧
  • 博客搭建
  • 开发工具
更多

元月

临渊羡鱼,不如退而结网
首页
  • 基础
  • 并发编程
  • JVM
  • Spring
  • Redis篇
  • Nginx篇
  • Kafka篇
  • Otter篇
  • Shardingsphere篇
  • 设计模式
  • MySQL
  • Oracle
  • 基础
  • 操作系统
  • 网络
  • 数据结构
  • 技术文档
  • Git常用命令
  • GitHub技巧
  • 博客搭建
  • 开发工具
更多
  • Redis

    • Redis安装
    • Redis持久化+灾难恢复方案
    • Redis常见数据类型和使用场景
    • Redis数据结构(一)Hyperloglog的实现原理
    • Redis数据结构(二)Bitmap位图的实现原理
    • Redis高可用
    • Redis缓存过期策略以及淘汰策略是什么?
    • 缓存穿透、缓存击穿和缓存雪崩的解决方案
    • Redis分布式锁的演变历史
    • Redis的集成与应用
      • 一、Jedis集成Redis
        • 1、引入相关依赖
        • 2、代码测试
      • 二、Springboot集成Redis
        • 1、引入相关依赖
        • 2、Springboot的application.yml增加配置
        • 3、代码测试
        • 4、在jedis中使用pipeline示例
      • 三、Redisson集成Redis
        • 1、引入相关依赖
        • 2、Springboot的application.yml增加配置
        • 3、代码测试
  • Nginx

  • Kafka

  • Shardingsphere

  • Otter

  • nexus

  • 中间件
  • Redis
元月
2022-07-01
目录

Redis的集成与应用

# Redis的集成与应用

# 一、Jedis集成Redis

参考:jedis (opens new window)

# 1、引入相关依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
1
2
3
4
5

# 2、代码测试

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
JedisCluster jedis = new JedisCluster(jedisClusterNodes);

jedis.sadd("planets", "Mars");
1
2
3
4
5
6

# 二、Springboot集成Redis

# 1、引入相关依赖

<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>
1
2
3
4
5
6
7
8

# 2、Springboot的application.yml增加配置

server:
  port: 8080

spring:
  redis:
    database: 0
    timeout: 3000
    password: foobared
    cluster:
      nodes: 10.66.50.127:7001,10.66.50.127:7002,10.66.50.131:7001,10.66.50.131:7002,10.66.50.132:7001,10.66.50.132:7002
    lettuce:
      pool:
        max-idle: 50
        min-idle: 10
        max-active: 100
        max-wait: 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 3、代码测试

@RestController
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/test")
    public void test() throws InterruptedException {
        stringRedisTemplate.opsForValue().set("100", "1");
        System.out.println(stringRedisTemplate.opsForValue().get("100"));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 4、在jedis中使用pipeline示例

Redis Pipeline是一种可以在一次网络往返中向Redis服务器发送多个命令的技术,可以显著提高Redis客户端的性能。

在Java中,可以使用Jedis库来与Redis进行交互,并使用它的Pipeline类来实现Redis Pipeline功能。下面是一个使用Jedis库实现Redis Pipeline的示例代码:

Jedis jedis = new Jedis("localhost", 6379);

// 开始Pipeline
Pipeline pipeline = jedis.pipelined();

// 向Pipeline中添加多个命令
pipeline.set("key1", "value1");
pipeline.set("key2", "value2");
pipeline.set("key3", "value3");
pipeline.incr("counter");

// 执行Pipeline中的所有命令,并获取结果
List<Object> results = pipeline.syncAndReturnAll();

// 输出执行结果
for (Object result : results) {
    System.out.println(result);
}

// 关闭Jedis连接
jedis.close();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 三、Redisson集成Redis

参考 Spring Boot Starter (opens new window) 集成

# 1、引入相关依赖

<dependency>
			<groupId>org.redisson</groupId>
			<artifactId>redisson-spring-boot-starter</artifactId>
			<version>3.17.4</version>
</dependency>
1
2
3
4
5

# 2、Springboot的application.yml增加配置

使用Springboot的配置

server:
  port: 8080

spring:
  redis:
    database: 0
    timeout: 3000
    password: foobared
    cluster:
      nodes: 10.66.50.127:7001,10.66.50.127:7002,10.66.50.131:7001,10.66.50.131:7002,10.66.50.132:7001,10.66.50.132:7002
    lettuce:
      pool:
        max-idle: 50
        min-idle: 10
        max-active: 100
        max-wait: 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

使用redisson的配置

spring:
  redis:
   redisson: 
      file: classpath:redisson.yaml
      config: |
        clusterServersConfig:
          idleConnectionTimeout: 10000
          connectTimeout: 10000
          timeout: 3000
          retryAttempts: 3
          retryInterval: 1500
          failedSlaveReconnectionInterval: 3000
          failedSlaveCheckInterval: 60000
          password: null
          subscriptionsPerConnection: 5
          clientName: null
          loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
          subscriptionConnectionMinimumIdleSize: 1
          subscriptionConnectionPoolSize: 50
          slaveConnectionMinimumIdleSize: 24
          slaveConnectionPoolSize: 64
          masterConnectionMinimumIdleSize: 24
          masterConnectionPoolSize: 64
          readMode: "SLAVE"
          subscriptionMode: "SLAVE"
          nodeAddresses:
          - "redis://127.0.0.1:7004"
          - "redis://127.0.0.1:7001"
          - "redis://127.0.0.1:7000"
          scanInterval: 1000
          pingConnectionInterval: 0
          keepAlive: false
          tcpNoDelay: false
        threads: 16
        nettyThreads: 32
        codec: !<org.redisson.codec.MarshallingCodec> {}
        transportMode: "NIO"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# 3、代码测试

@RestController
public class RedissonController {

    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/test_redisson")
    public void testRedisson() throws InterruptedException {
        RLock lock = redissonClient.getLock("myLock");
        String threadName = Thread.currentThread().getName();
        try {
            lock.lock();
            //TODO 处理业务逻辑
            log.info("{}开始加锁", threadName);
            Thread.sleep(10000);
        } catch (Exception e) {
            throw e;
        } finally {
            log.info("{}开始释放锁", threadName);
            lock.unlock();
        }
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

4、测试结果

同时请求接口两次,输出如下

c.r.redis.controller.IndexController     : http-nio-8080-exec-1开始加锁
c.r.redis.controller.IndexController     : http-nio-8080-exec-1开始释放锁
c.r.redis.controller.IndexController     : http-nio-8080-exec-3开始加锁
c.r.redis.controller.IndexController     : http-nio-8080-exec-3开始释放锁
1
2
3
4
#Redis
Redis分布式锁的演变历史
Nginx安装和基本使用

← Redis分布式锁的演变历史 Nginx安装和基本使用→

最近更新
01
otter二次开发-支持按目标端主键索引Load数据
08-03
02
mvnw简介
06-21
03
gor流量复制工具
06-03
更多文章>
Theme by Vdoing | Copyright © 2022-2024 元月 | 粤ICP备2022071877号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式