Redission-spring-boot

35次阅读

共计 3141 个字符,预计需要花费 8 分钟才能阅读完成。

redission + spring boot

maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.linseven</groupId>
    <artifactId>cache</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependencies>
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson</artifactId>
        <version>3.13.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.8.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <defaultGoal>dataCache</defaultGoal>
    <resources>
        <resource>
            <directory>src/main/java</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.8.RELEASE</version>
        </plugin>
    </plugins>
</build>
</project>

application.yml

spring:
    redis:
       redisson:
          config: classpath:redisson.yml
server:
    port: 8080

redisson.yml

clusterServersConfig:
  idleConnectionTimeout: 10000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  failedSlaveReconnectionInterval: 3000
  failedSlaveCheckInterval: 60000
  password: null
  subscriptionsPerConnection: 5
  clientName: null
  loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  slaveConnectionMinimumIdleSize: 24
  slaveConnectionPoolSize: 64
  masterConnectionMinimumIdleSize: 24
  masterConnectionPoolSize: 64
  readMode: "SLAVE"
  subscriptionMode: "SLAVE"
  nodeAddresses:
  - "redis://192.168.43.33:7000"
  - "redis://192.168.43.33:7001"
  - "redis://192.168.43.33:7002"
  - "redis://192.168.43.33:7003"
  - "redis://192.168.43.33:7004"
  - "redis://192.168.43.33:7005"
  scanInterval: 1000
  pingConnectionInterval: 0
  keepAlive: false
  tcpNoDelay: false
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.JsonJacksonCodec> {}
transportMode: "NIO"

使用

@RestController
public class TestController {

    @Autowired
    private RedissonClient redisson;
    @GetMapping("/setKey")
    public String setKey(String key,String value){RBucket<String> rBucket =  redisson.getBucket(key);
        rBucket.set(value);
        return "success";
    }
    @GetMapping("/getKey")
    public String getKey(String key){RBucket<String> rBucket =  redisson.getBucket(key);
        return rBucket.get();}

}

集群配置非常重要一点就是需要修改绑定的 ip 地址为内网地址
192.168.43.33
如果 bind 127.0.0.1 的话,在局域网将无法访问 192.168.43.33 局域网的 redis。
将 protected-mode=yes 该为 protected-mode=no
1、修改好后需要将安装目录下的
appendonly.aof、dump.rdb、nodes-7001.conf 删除。
注意删除这个三个文件,仅限于在学习环境,在生产环境不要乱删,应该在应用生产前配置好 bind,protected-mode 等正确配置。

正文完
 0