day105-Cache-Distributed Lock-Redisson Introduction & Integration

1 Introduction

I have said before that we need to use distributed locks in a distributed environment, and the implementation of distributed locks in the java language is Redisson. Like jedis and lettuce, they are also a client for redis, but they are more powerful.

After https://github.com/redisson/redisson/wiki/Table-of-Content is  opened, the Chinese document directory is on the right

2. Integration

(1) Introduce dependency

https://mvnrepository.com/search?q=redisson  copy dependency added to the product module of the project

(2) Add configuration class and test

Reference document https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#26-%E5%8D %95redis%E8%8A%82%E7%82%B9%E6%A8%A1%E5%BC%8F

Configuration code Note that redis:// or rediss:// should be added before the address of the redis server

package com.atguigu.gulimall.product.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author rengang
 * @version 1.0
 * @date 2021/2/10 16:47
 */
@Configuration
public class MyRedissonConfig {

    @Bean
    RedissonClient redisson() throws IOException {
        //集群方式
//        Config config = new Config();
//        config.useClusterServers()
//                .addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");
//        return Redisson.create(config);

        //单Redis节点模式
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.56.10:6379");
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}

Unit test code

Successful printing indicates successful configuration

Now the integration is complete

 

 

 

Guess you like

Origin blog.csdn.net/JavaCoder_juejue/article/details/113783376