栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

redis学习

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

redis学习

jedis
学习了狂神的redis
导入jedis包
建一个maven项目

 
        
            redis.clients
            jedis
            3.2.0
        

        
            com.alibaba
            fastjson
            1.2.62
        
    

写了两行代码

  public static void main(String[] args) {
        Jedis jedis = new Jedis("xxx.xxx.xxx.xxx", 6379);
        //写自己服务器的地址
        System.out.println(jedis.ping());
    }

我是虚拟机做服务器的,发现无法访问。。

systemctl status firewalld

查看了一下防火墙的状态,发现是running
关闭一下防火墙

systemcl stop firewalld

Jedis使用测试——连接本地及远程的Redis
后面找到了这篇博客,跟着更改了配置redis.conf 的配置。就输出PONG

 Jedis jedis = new Jedis("xxx.xxx.xxx.xxx", 6379);

        jedis.flushDB();
        HashMap hashmap = new HashMap<>();
        hashmap.put("key1","value1");
        hashmap.put("key2","value2");
        hashmap.put("key3","value3");
//        jedis.mset("hash", hashmap);
        jedis.hset("hash",  hashmap);
        jedis.hset("hash","key4","value4");
        System.out.println(jedis.hgetAll("hash"));
         System.out.println(jedis.hkeys("hash"));
        System.out.println(jedis.hvals("hash"));
    }
    输出
    {key1=value1, key2=value2, 
    key3=value3, key4=value4}
    [key1, key2, key3, key4]
	[value2, value1, value3, value4]

事务

 public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.249.134", 6379);
        jedis.flushDB();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("name","zhangsan");
        jsonObject.put("age","111");


//        开启事务
        Transaction multi = jedis.multi();
        String result = jsonObject.toJSONString();
//        jedis.watch(result);
        try {
            multi.set("user1",result);
            multi.set("user2",result);
            multi.exec();
//            执行事务
        }catch (Exception e){
            multi.discard();
//            放弃事务
            e.printStackTrace();
        }finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();
        }
    }

整合springboot
新建springboot项目 需要点击web和redis
在springboot2.x之后采用lettuce
jedis:采用的直连,多个线程操作,是不安全的,如果避免不安全,使用jedis pool连接池
lettuce:采用netty 实例可以多线程共享,不存在线程不安全

@Bean
    @ConditionalOnMissingBean(
        name = {"redisTemplate"}
    )
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
    //默认的redistemplate
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    //最常用的string类型
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

配置文件可以配置的redis

public class RedisProperties {
    private int database = 0;
    private String url;
    private String host = "localhost";
    private String password;
    private int port = 6379;
    private boolean ssl;
    private Duration timeout;
    private String clientName;
    private RedisProperties.Sentinel sentinel;
    private RedisProperties.Cluster cluster;
    private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
    private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
	....................

配置

# 应用名称
spring.application.name=springboot-redis
# 应用服务 WEB 访问端口
server.port=8080
//写自己的服务器ip
spring.redis.host=xxx.xxx.xxx.xxx
spring.redis.port=6379

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/828276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号