• Redisson入门

    Author:Ricky  Date:2017-04-24

  • Redisson概述

Redisson是架设在Redis基础上的一个Java驻内存数据网格(In-Memory Data Grid)。充分的利用了Redis键值数据库提供的一系列优势,基于Java实用工具包中常用接口,为使用者提供了一系列具有分布式特性的常用工具类。使得原本作为协调单机多线程并发程序的工具包获得了协调分布式多机多线程并发系统的能力,大大降低了设计和研发大规模分布式系统的难度。同时结合各富特色的分布式服务,更进一步简化了分布式环境中程序相互之间的协作。

地址:https://github.com/redisson/redisson

  • Redisson功能

  • Redisson功能

    • 支持同步/异步/异步流/管道流方式连接
    • 多样化数据序列化
    • 集合数据分片
    • 分布式对象
    • 分布式集合
    • 分布式锁和同步器
    • 分布式服务
    • 独立节点模式
    • 三方框架整合
  • HelloWorld

引入依赖包

<dependency>

 
<groupId>org.redisson</groupId>

 
<artifactId>redisson</artifactId>

  <version>3.3.2</version>

</dependency>

程序化配置方法

         Config config = new Config();

config.
useSingleServer().setAddress("127.0.0.1:6379");

RedissonClient
redisson = Redisson.create(config);

文件方式配置

Config
config = Config.fromJSON(new File("config-file.json"));

Config
config = Config.fromYAML(new File("config-file.yaml")); RedissonClient
RedissonClient  redisson =
Redisson.create(config);

Spring方式

<redisson:client>

<redisson:single-server
address=“127.0.0.1:6379" />

</redisson:client>

HelloWorld 3

验证是否成功

redisson.getConfig().toJSON().toString()

结果
{"singleServerConfig":{"idleConnectionTimeout":10000,"pingTimeout":1000,"connectTimeout":10000,"timeout":3000,"retryAttempts":3,"retryInterval":1500,"reconnectionTimeout":3000,"failedAttempts":3,"subscriptionsPerConnection":5,"address":"redis://127.0.0.1:6379","subscriptionConnectionMinimumIdleSize":1,"subscriptionConnectionPoolSize":50,"connectionMinimumIdleSize":10,"connectionPoolSize":64,"database":0,"dnsMonitoring":false,"dnsMonitoringInterval":5000},"threads":0,"nettyThreads":0,"codec":{"class":"org.redisson.codec.JsonJacksonCodec"},"codecProvider":{"class":"org.redisson.codec.DefaultCodecProvider"},"resolverProvider":{"class":"org.redisson.liveobject.provider.DefaultResolverProvider"},"redissonReferenceEnabled":true,"useLinuxNativeEpoll":false}

连接方式

RedissonClient
client = Redisson.create(config);

RAtomicLong longObject =
client.getAtomicLong('myLong');

//
同步执行方式

longObject.compareAndSet(3, 401);

//
异步执行方式

longObject.compareAndSetAsync(3, 401);

RedissonReactiveClient client = Redisson.createReactive(config);
RAtomicLongReactive longObject = client.getAtomicLong('myLong');

//
异步流执行方式

longObject.compareAndSet(3,
401);

数据序列化

  

集合数据分片

在集群模式下,Redisson为单个Redis集合类型提供了自动分片的功能。

在自动分片功能的帮助下,单个集合拆分以后均匀的分布在整个集群里,而不是被挤在单一一个节点里。

Redisson通过自身的分片算法,将一个大集合拆分为若干个片段(默认231个,分片数量范围是3 - 16834),然后将拆分后的片段均匀的分布到集群里各个节点里,保证每个节点分配到的片段数量大体相同。比如在默认情况下231个片段分到含有4个主节点的集群里,每个主节点将会分配到大约57个片段,同样的道理如果有5个主节点,每个节点会分配到大约46个片段。

目前支持的数据结构类型包括Set和Map.

分布式对象

通用对象桶(Object
Bucket
)

二进制流(Binary
Stream
)

地理空间对象桶(Geospatial
Bucket
)

 BitSet

原子整长形(AtomicLong)

原子双精度浮点数(AtomicDouble)

话题(订阅分发)

布隆过滤器(Bloom
Filter
)

基数估计算法(HyperLogLog)

示例

通用桶对象

RBucket<AnyObject>
bucket = redisson.getBucket("anyObject"); bucket.set(new AnyObject(1));

AnyObject
obj = bucket.get();

原子整长型

RAtomicLong
atomicLong = redisson.getAtomicLong("myAtomicLong");
atomicLong.set(3);

atomicLong.incrementAndGet();

atomicLong.get();

分布式集合

映射(Map)

多值映射(Multimap)

集(Set)

有序集(SortedSet)

计分排序集(ScoredSortedSet)

字典排序集(LexSortedSet)

列表(List)

列队(Queue)

双端队列(Deque)

阻塞队列(Blocking
Queue
)

有界阻塞列队(Bounded
Blocking Queue
)

 阻塞双端列队(Blocking Deque)

阻塞公平列队(Blocking
Fair Queue
)

延迟列队(Delayed
Queue
)

 优先队列(Priority Queue)

优先双端队列(Priority
Deque
)

分布式集合

示例

         Map

RMap<String,
SomeObject> map = redisson.getMap("anyMap");

SomeObject
prevObject = map.put("123", new SomeObject());

SomeObject currentObject =
map.putIfAbsent("323", new SomeObject());

SomeObject
obj = map.remove("123");

         Set

         RSet<SomeObject> set = redisson.getSet("anySet");

set.add(new
SomeObject());

set.remove(new
SomeObject());

分布式锁

可重入锁(Reentrant
Lock
)

公平锁(Fair Lock)

联锁(MultiLock)

红锁(RedLock)

读写锁(ReadWriteLock)

信号量(Semaphore)

可过期性信号量(PermitExpirableSemaphore)

闭锁(CountDownLatch)

示例

RLock
lock = redisson.getLock("anyLock");

//
最常见的使用方法

lock.lock();

//
支持过期解锁功能
10秒钟以后自动解锁

// 无需调用unlock方法手动解锁

lock.lock(10,
TimeUnit.SECONDS);

//
尝试加锁,最多等待100秒,上锁以后10秒自动解锁
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
lock.unlock();

分布式服务

分布式远程服务(Remote
Service
)

分布式实时对象(Live
Object
)服务

分布式执行服务(Executor
Service
)

分布式调度任务服务(Scheduler
Service
)

分布式映射归纳服务(MapReduce)

服务端(远端)实例

RRemoteService remoteService = redisson.getRemoteService();

SomeServiceImpl someServiceImpl = new SomeServiceImpl();

// 在调用远程方法以前,应该首先注册远程服务

// 只注册了一个服务端工作者实例,只能同时执行一个并发调用

remoteService.register(SomeServiceInterface.class, someServiceImpl);

// 注册了12个服务端工作者实例,可以同时执行12个并发调用

remoteService.register(SomeServiceInterface.class,
someServiceImpl, 12);

客户端(本地)实例

RRemoteService
remoteService = redisson.getRemoteService();

SomeServiceInterface
service = remoteService.get(SomeServiceInterface.class);

String
result = service.doSomeStuff(1L, "secondParam", new AnyParam());

独立节点模式

Redisson Node指的是Redisson在分布式运算环境中作为独立节点运行的一种模式。Redisson Node的功能可以用来执行通过分布式执行服务或分布式调度执行服务发送的远程任务,也可以用来为分布式远程服务提供远端服务。

依赖redisson-all.jar

独立节点模式-配置

// Redisson程序化配置代码

Config config = ...

// Redisson Node 程序化配置方法

RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);

Map<String, Integer> workers = new HashMap<String,
Integer>();

workers.put("test", 1);

nodeConfig.setExecutorServiceWorkers(workers);

// 创建一个Redisson Node实例

RedissonNode node = RedissonNode.create(nodeConfig);

// 或者通过指定的Redisson实例创建Redisson Node实例

RedissonNode node = RedissonNode.create(nodeConfig, redisson);

node.start();

node.shutdown();

三方框架整合

Spring框架整合

Spring Cache整合

Hibernate整合

 Tomcat会话管理器(Tomcat Session Manager)

Spring Session会话管理器

三方框架-Spring整合

基本配置:

<redisson:client
id="myRedisson"> <redisson:single-server
address="127.0.0.1:6379"/> </redisson:client>

完全配置:

参照附件:Redisson说明文档.pdf

    • 三方框架-Spring调用

@Service

public class RedissonUtils2 implements InitializingBean {

    @Autowired

    private RedissonClient
redissonClient;





    public void afterPropertiesSet()
throws Exception {

        Redisson redisson= (Redisson)
redissonClient;

       RAtomicDouble dd=
redissonClient.getAtomicDouble("tt");

       dd.set(1.22);

        System.out.println("bean初始化后置方法"+redisson.getConfig().toJSON().toString());

    }

}

总结

Redisson是redis分布式方向落地的产品,不仅开源免费,而且内置分布式锁,分布式服务等诸多功能,是基于redis实现分布式的最佳选择。

Redisson入门的更多相关文章

  1. Redisson教程

    Redisson入门 Author:Ricky  Date:2017-04-24 Redisson概述 Redisson是架设在Redis基础上的一个Java驻内存数据网格(In-Memory Dat ...

  2. linux系统下安装jdk,mysql,tomcat 和redis 和jedis入门案例

    Day47笔记Linux+redis入门 Day47   知识讲解:Jedis 1.Linux上jdk,mysql,tomcat安装(看着文档安装) 准备工作: 因为JDK,TOMCAT,MYSQL的 ...

  3. 【Redis】349- Redis 入门指南

    点击上方"前端自习课"关注,学习起来~ 1. 概述 1.1. Redis 简介 Redis 是速度非常快的非关系型(NoSQL)内存键值数据库,可以存储键和五种不同类型的值之间的映 ...

  4. redisson spring boot starter 做分布式锁

    使用redisson做分布式锁 分布式锁 在java中单体应用中,我们如果想要保证一个接口或者服务.方法当下只有一个线程在运行,我们可以通过JDK提供的Lock.Semaphore.同步锁等多种方式实 ...

  5. 用了Redisson的Spring Boot Starter搞的我都想重写个

    在对接一个小程序推送的框架时,需要将 access_token 存储到 Redis 中,框架中提供了存储逻辑,只需要将 RedissonClient 对象传进去即可. 框架内部在用 Redisson ...

  6. Redis入门到实战

    一.Redis基础 Redis所有的命令都可以去官方网站查看 1.基本命令 keys * 查找所有符合给定模式pattern(正则表达式)的 key .可以进行模糊匹配 del key1,key2,. ...

  7. 一篇文章带你了解NoSql数据库——Redis简单入门

    一篇文章带你了解NoSql数据库--Redis简单入门 Redis是一个基于内存的key-value结构数据库 我们会利用其内存存储速度快,读写性能高的特点去完成企业中的一些热门数据的储存信息 在本篇 ...

  8. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  9. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

随机推荐

  1. 1218: [HNOI2003]激光炸弹

    1218: [HNOI2003]激光炸弹 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1139  Solved: 542[Submit][Statu ...

  2. kali linux 折腾笔记

    http://xiao106347.blog.163.com/blog/static/215992078201342410347137/ 这里告诉你刚刚装好kali后要做,有些也是不正确的,可能开始你 ...

  3. POJ 2182 解题报告

    Lost Cows Time Limit: 1000 MS Memory Limit: 65536 KB Description N (2 <= N <= 8,000) cows have ...

  4. (转)Uploadify 3.2 参数属性、事件、方法函数详解

    转自http://blog.sina.com.cn/s/blog_5079086b0101fkmh.html Hallelujah博客 一.属性 属性名称 默认值 说明 auto true 设置为tr ...

  5. less可以做什么?less详解!(less嵌套选择器实现纯CSS二级导航)

    前端技术众多,作为一名前端工程师,我们每接触新技术首先要懂得此技术的优势和劣势,这是最基本的.往深入了说还需要懂得技术的应用场景,配合的技术等,方便为以后架构做准备.而less作为一门CSS预处理语言 ...

  6. rgba()和opacity的使用

    rgba()表示 红 绿 蓝 alpha ,W3C指在原有的rgb颜色模型之后增加了 “alpha”参数,“可以让制定的颜色透明化”(rgb()上扩展的,其只可以设置颜色,而不能使设置的颜色透明化) ...

  7. 浅谈MVC缓存

    缓存是将信息放在内存中以避免频繁访问数据库从数据库中提取数据,在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法. 对于MVC有Control缓存和Action缓存. 一.Control缓存 ...

  8. [编织消息框架][JAVA核心技术]jdk动态代理

    需要用到的工具  jdk : javac javap class 反编译 :JD-GUI http://jd.benow.ca/ import java.lang.reflect.Invocation ...

  9. 机器学习:Python中如何使用最小二乘法

    之所以说"使用"而不是"实现",是因为python的相关类库已经帮我们实现了具体算法,而我们只要学会使用就可以了.随着对技术的逐渐掌握及积累,当类库中的算法已经 ...

  10. 老李分享:android手机测试之适配(2)

    但 Android 版本低于 3.2 的设备不支持此技术,原因是这些设备无法将 sw600dp 识别为尺寸限定符,因此我们仍需使用 large 限定符.这样一来,就会有一个名称为 res/layout ...