Spring-Boot框架整合Lettuce解决Jedis需要反复创建和关闭连接和线程不安全问题(只需要一个Lettuce连接)------Spring-Boot框架

189 篇文章 0 订阅
79 篇文章 0 订阅
package com.atguigu.boot.demo;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.util.List;

public class LettuceDemo
{
    public static void main(String[] args) {
        StatefulRedisConnection conn = null;
        RedisClient redisClient = null;
        try{
//        使用构建器链式编程来builder我们RedisURI
            RedisURI uri = RedisURI.builder().redis("192.168.92.129")
                    .withPort(6379)
                    .withAuthentication("default","abc123")
                    .build();
//        创建连接客户端
            redisClient = RedisClient.create(uri);
            conn = redisClient.connect();
//        创建操作的command
            RedisCommands commands = conn.sync();
//        业务操作
            System.out.println(commands.ping());
            List keys = commands.keys("*");
            for (int i = 0; i < keys.size(); i++) {
                System.out.println(keys.get(i));
            }
            System.out.println(commands.set("k5", "helloLettuce"));
            System.out.println(commands.get("k5"));
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally {
            //        各种关闭释放资源
            if (conn != null) {
                conn.close();
            }
            if (redisClient != null) {
                redisClient.shutdown();
            }
        }
    }
}
package com.atguigu.boot.demo;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.util.List;

public class LettuceDemo
{
    public static void main(String[] args) {
        StatefulRedisConnection conn = null;
        RedisClient redisClient = null;
        try{
//        使用构建器链式编程来builder我们RedisURI
            RedisURI uri = RedisURI.builder().redis("192.168.92.129")
                    .withPort(6379)
                    .withAuthentication("default","abc123")
                    .build();
//        创建连接客户端
            redisClient = RedisClient.create(uri);
            conn = redisClient.connect();
//        创建操作的command
            RedisCommands commands = conn.sync();
//        业务操作
            System.out.println(commands.ping());
            List keys = commands.keys("*");
            for (int i = 0; i < keys.size(); i++) {
                System.out.println(keys.get(i));
            }
            System.out.println(commands.set("k5", "helloLettuce"));
            System.out.println(commands.get("k5"));
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally {
            //        各种关闭释放资源
            if (conn != null) {
                conn.close();
            }
            if (redisClient != null) {
                redisClient.shutdown();
            }
        }
    }
}
<?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>com.atguigu</groupId>
    <artifactId>boot3-01</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    所有SpringBoot都必须继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.4</version>
    </parent>
    <dependencies>
<!--        redis相关场景-->
        <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>
<!--        引入redis-template-->
        <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>
<!--        引入swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
<!--        引入junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
<!--        引入jedis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.4.3</version>
        </dependency>
<!--        引入lettuce-->
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>6.2.6.RELEASE</version>
        </dependency>
    </dependencies>
<!--    SpringBoot打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
<?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>com.atguigu</groupId>
    <artifactId>boot3-01</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    所有SpringBoot都必须继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.4</version>
    </parent>
    <dependencies>
<!--        redis相关场景-->
        <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>
<!--        引入redis-template-->
        <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>
<!--        引入swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
<!--        引入junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
<!--        引入jedis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.4.3</version>
        </dependency>
<!--        引入lettuce-->
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>6.2.6.RELEASE</version>
        </dependency>
    </dependencies>
<!--    SpringBoot打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值