作者:微信小助手
发布时间:2023-10-23T09:53:05
STRAT 乘风破浪 | 直挂云帆 Redisson是基于NIO的Netty框架上,充分的利用了Redis键值数据库提供的一系列优势,在Java实用工具包中常用接口的基础上,为使用者提供了一系列具有分布式特性的常用工具类。能够为我们提供多服务版并发解决的能力,大大降低分布式系统的难度。 主要的使用时集成到Springboot中,首先引入pom依赖,然后引入配置。
</ul>
<pre class="code-snippet__js" data-lang="xml"><code><span class="code-snippet_outer"><dependency></span></code><code><span class="code-snippet_outer"> <groupId>org.redisson</groupId></span></code><code><span class="code-snippet_outer"> <artifactId>redisson</artifactId></span></code><code><span class="code-snippet_outer"> <version>3.11.1</version></span></code><code><span class="code-snippet_outer"></dependency></span></code></pre>
</section>
<section class="code-snippet__fix code-snippet__js">
<ul class="code-snippet__line-index code-snippet__js">
</ul>
<pre class="code-snippet__js" data-lang="kotlin"><code><span class="code-snippet_outer" style="color: rgb(175, 175, 175);">@Configuration</span></code><code><span class="code-snippet_outer"><span style="color: rgb(202, 125, 55);">public</span> <span style="color: rgb(202, 125, 55);">class</span> <span style="color: rgb(14, 156, 229);">RedissonConfig</span> {</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);">@Value("<span style="color: rgb(202, 125, 55);">${spring.redis.host}</span>")</span></span></code><code><span class="code-snippet_outer"> <span style="color: rgb(202, 125, 55);">private</span> String host;</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);">@Value("<span style="color: rgb(202, 125, 55);">${spring.redis.port}</span>")</span></span></code><code><span class="code-snippet_outer"> <span style="color: rgb(202, 125, 55);">private</span> String port;</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);">@Value("<span style="color: rgb(202, 125, 55);">${spring.redis.password}</span>")</span></span></code><code><span class="code-snippet_outer"> <span style="color: rgb(202, 125, 55);">private</span> String password;</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);">@Value("<span style="color: rgb(202, 125, 55);">${spring.redis.database}</span>")</span></span></code><code><span class="code-snippet_outer"> <span style="color: rgb(202, 125, 55);">private</span> int database;</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);">@Bean</span></span></code><code><span class="code-snippet_outer"> <span style="color: rgb(202, 125, 55);">public</span> RedissonClient getRedisson() {</span></code><code><span class="code-snippet_outer"> Config config = new Config();</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);font-style: italic;">//线程定时间隔时间10s</span></span></code><code><span class="code-snippet_outer"> config.setLockWatchdogTimeout(<span style="color: rgb(14, 156, 229);">10000</span>);</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);font-style: italic;">//设置单机版本redis</span></span></code><code><span class="code-snippet_outer"> config.useSingleServer().setAddress(<span style="color: rgb(221, 17, 68);">"redis://"</span> + host + <span style="color: rgb(221, 17, 68);">":"</span> + port).setPassword(password).setDatabase(database);</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);font-style: italic;">//设置集群的方式</span></span></code><code><span class="code-snippet_outer"> config.useClusterServers().addNodeAddress(<span style="color: rgb(221, 17, 68);">"redis://"</span> + host + <span style="color: rgb(221, 17, 68);">":"</span> + port);</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(175, 175, 175);font-style: italic;">//添加主从配置</span></span></code><code><span class="code-snippet_outer"> config.useMasterSlaveServers().setMasterAddress(<span style="color: rgb(221, 17, 68);">""</span>).setPassword(<span style="color: rgb(221, 17, 68);">""</span>).addSlaveAddress(new String[]{<span style="color: rgb(221, 17, 68);">""</span>,<span style="color: rgb(221, 17, 68);">""</span>});</span></code><code><span class="code-snippet_outer"> <span style="color: rgb(202, 125, 55);">return</span> Redisson.create(config);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span></code></pre>
</section>
<p style="margin: 24px 8px;line-height: 1.75em;"><span style="letter-spacing: 1px;font-size: 16px;color: rgb(61, 170, 214);">底层结构:</span></p>
<section data-tools="135编辑器" data-id="119830">
<section style="margin: 10px auto;">
<section>
<section style="display: flex;">
<section style="flex-shrink:0;">
<section style="background-color: #7f7f7f;height: 100%;">
<section style="padding-top: 20px;padding-right: 2px;padding-left: 2px;">
<section style="width: 15px;">
<img class="rich_pages wxw-img" data-ratio="2.0987124463519313" data-type="gif" data-w="466" data-width="100%" style="vertical-align: inherit;width: 100%;display: block;height: auto !important;" src="/upload/edf29291b98f0aee1f8d6e3fd69b2f30.png">
</section>
</section>
</section>
</section>
<section style="background-color: rgb(242, 249, 255);width: 100%;" data-width="100%">
<section style="padding-top: 15px;padding-right: 15px;padding-left: 15px;">
<section data-autoskip="1" style="text-align: justify;line-height:1.75em;letter-spacing: 1.5px;font-size:14px;color:#333333;background: transparent;">
<p style="margin: 8px;line-height: 1.75em;"><span style="max-inline-size: 100%;cursor: text;text-align: left;caret-color: rgb(255, 0, 0);font-size: 15px;letter-spacing: 1px;color: rgb(255, 76, 0);outline: none 0px !important;">一个分布式锁对应一个hash</span><span style="font-size: 15px;color: rgb(96, 96, 96);letter-spacing: 1px;"><span style="max-inline-size: 100%;cursor: text;text-align: left;caret-color: rgb(255, 0, 0);outline: none 0px !important;">,</span><span style="max-inline-size: 100%;cursor: text;text-align: left;caret-color: red;font-family: 微软雅黑, "Microsoft YaHei", sans-serif;outline: none 0px !important;">hash 对应的 key