Redission +spring boot

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>

Redission +spring boot

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等正确配置。

原文 

https://segmentfault.com/a/1190000023021355

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » Redission +spring boot

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址