Redis笔记-数据类型

基本数据类型及应用场景 类型 简介 特性 场景 String(字符串) 二进制安全 可以包含任何数据,比如jpg图片或者序列化的对象,一个键最大能存储512M — Hash(字典) 键值对集合,即编程语言中的Map类型 适合存储对象,并且可以像数据库中update一个属性一样只修改某一项属性值(Memcached中需要取出整个字符串反序列化成对象修改完再序列化存回去) 存储、读取、修改用户属性 List(列表) 链表(双向链表) 增删快,提供了操作某一段元素的API 1,最新消息排行等功能(比如朋友圈的时间线) 2,消息队列 Set(集合) 哈希表实现,元素不重复 1、添加、删除,查找的复杂度都是O(1) 2、为集合提供了求交集、并集、差集等操作 1、共同好友 2、利用唯一性,统计访问网站的所有独立ip 3、好友推荐时,根据tag求交集,大于某个阈值就可以推荐 Sorted Set(有序集合) 将Set中的元素增加一个权重参数score,元素按score有序排列 数据插入集合时,已经进行天然排序 1、排行榜 2、带权重的消息队列 字符串(String) 设置值 set命令:为键设置值. set key value setex命令:为键设置值,并设置秒级的过期时间. setex key value seconds setpx命令:为键设置值,并设置毫秒秒级的过期时间. setpx key value millisecond setnx命令:向键设置值前判断键是否存在,不存在则设置值存在则不设置. setnx key value setxx命令:与setnx相反,向键设置值前判断键是否存在,存在则设置值存在则不设置. setxx key value setnx和setxx的使用场景:由于redis是单线程命令处理机制,如果多个客户端同时向redis服务使用setnx命令也只会有一个客户端执行成功.setnx可以用于分布式锁的一个解决方案. 计数 incr命令:用于对键的值进行自增操作,返回结果为三种: 值不是整数,返回错误. 值是整数,返回自增后的结果. 值不存在,按照值为0自增,返回1. incr key incrby命令:用于对键的值进行指定自增量的自增操作,返回结果与incr相同. incrby key incrbyfloat命令:用于对键的值进行指定浮点型自增量的自增操作. incrbyfloat key decr命令:用于对键的值进行自减操作. decr key decrby命令:用于对键的值进行指定值进行减值操作. decrby key append命令:用于对键的值进行追加操作,返回追加后的值的得长度. ...

September 27, 2024

spring boot运行非web项目

前言 一直以来,一直使用springboot来创建基于web的项目,今天突然想到,springboot是否可以用于非web项目的使用。 非web运行办法 启动类main方法后面执行 @SpringBootApplication public class StudyLockApp { public static void main(String[] args) { ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(StudyLockApp.class, args); RedisService redisService = configurableApplicationContext.getBean(RedisService.class); Boolean lock = redisService.getLock("lock", 1); System.out.println(lock); } } 通过SpringApplication.run拿到bean容器 获取到已经注入进去的业务bean 执行相应的方法 实现ApplicationRunner接口 @SpringBootApplication public class StudyLockApp implements ApplicationRunner, ApplicationContextAware { private ApplicationContext applicationContext; public static void main(String[] args) { SpringApplication.run(StudyLockApp.class, args); } @Override public void run(ApplicationArguments args) throws Exception { RedisService redisService = applicationContext.getBean(RedisService.class); for(int i = 0;i<10;i++){ Thread thread = new Thread(new ConsumerThread(i,redisService)); thread.start(); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } } 实现CommandLineRunner接口 @SpringBootApplication public class StudyLockApp implements CommandLineRunner, ApplicationContextAware { private ApplicationContext applicationContext; public static void main(String[] args) { SpringApplication.run(StudyLockApp.class, args); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } @Override public void run(String... args) throws Exception { RedisService redisService = applicationContext.getBean(RedisService.class); for(int i = 0;i<10;i++){ Thread thread = new Thread(new ConsumerThread(i,redisService)); thread.start(); } } } Springboot源码 public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } return context; } private void callRunners(ApplicationContext context, ApplicationArguments args) { List<Object> runners = new ArrayList<>(); runners.addAll(context.getBeansOfType(ApplicationRunner.class).values()); runners.addAll(context.getBeansOfType(CommandLineRunner.class).values()); AnnotationAwareOrderComparator.sort(runners); for (Object runner : new LinkedHashSet<>(runners)) { if (runner instanceof ApplicationRunner) { callRunner((ApplicationRunner) runner, args); } if (runner instanceof CommandLineRunner) { callRunner((CommandLineRunner) runner, args); } } } private void callRunner(ApplicationRunner runner, ApplicationArguments args) { try { (runner).run(args); } catch (Exception ex) { throw new IllegalStateException("Failed to execute ApplicationRunner", ex); } } private void callRunner(CommandLineRunner runner, ApplicationArguments args) { try { (runner).run(args.getSourceArgs()); } catch (Exception ex) { throw new IllegalStateException("Failed to execute CommandLineRunner", ex); } } 其实就是在执行容器初始化后再调用实现的run方法而已。

September 27, 2024

Zookeeper概述与安装

特性 ZooKeeper 解决的本质问题 消息传递:延迟性,先发送的消息,不一定先到达; 消息传递:丢失性,发送的消息,可能丢失; 节点崩溃:分布式系统内,任何一个节点都可能崩溃。 如何保证分布式一致性 顺序一致性:同一个 Client 发起的事务请求,严格按照发起顺序执行 原子性:事务请求,要么应用到所有节点,要么一个节点都没有应用 单一视图:Client 无论连接到哪个节点,看到的服务端数据都是一致的(Note:不准确,其实是最终一致性) 可靠性:事务一旦执行成功,状态永久保留 实时性:事务一旦执行成功,Client 并不能立即看到最新数据,但 ZooKeeper 保证最终一致性 官方文档:https://zookeeper.apache.org/doc/r3.6.2/zookeeperStarted.html 安装(centos 7) 下载 wget https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz tar zvxf apache-zookeeper-3.6.2-bin.tar.gz cd apache-zookeeper-3.6.2-bin 配置 单机模式 创建配置文件 conf/zoo.cfg vi conf/zoo.cfg 写入配置 tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181 tickTime : the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime. dataDir : the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database. clientPort : the port to listen for client connections 启动 bin/zkServer.sh start 连接 bin/zkCli.sh -server 127.0.0.1:2181 Connecting to localhost:2181 log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). log4j:WARN Please initialize the log4j system properly. Welcome to ZooKeeper! JLine support is enabled [zkshell: 0]

September 27, 2024
苏ICP备2021016520号-1