This is the fourth day of my participation in Gwen Challenge

Maven project integration with Redisson

<! -- JDK 1.8+ compatible --> <dependency> <groupId>org.redisson</groupId> <artifactId> The < version > 3.5.4 < / version > < / dependency >Copy the code

Support Redis multiple connection modes

There are three redis deployment modes: single-node deployment, cluster deployment, and sentinel deployment

You can check the Config class provided by Redisson to see if you want to add additional parameters. In addition to the CONFIGURATION in YAML format, configuration files in JSON format are also supported. For details, see the Redisson configuration method.

1. Configure a single node

SingleServerConfig singleConfig = config.useSingleServer(); Setting parameters for the SingleServerConfig class

Pure Java operation

@Test public void test04(){ Config config = new Config(); / / the specified encoding, the default encoding for org. Redisson. Codec. JsonJacksonCodec / / before the use of spring - data - redis, use client jedis, Coding for org. Springframework. Data. Redis. Serializer. StringRedisSerializer / / after the switch to redisson to between data can be compatible, Modify the code here is org. Redisson. Client. The codec. StringCodec config. SetCodec (new StringCodec ()); The config. UseSingleServer (.) setAddress (" redis: / / 47.108.105.159:6379 "). The setPassword (" cen66lyz ") / / set the password . SetConnectionPoolSize (50) / / set the number of connections in the pool for the master node 50. The maximum setIdleConnectionTimeout (10000) / / if the current connection pool connection number more than the minimum number of idle connections, If a connection idle time exceeds this value, the connection will be automatically closed and removed from the connection pool. The time unit is milliseconds. .setConnectTimeout(3000)// Wait timeout when establishing a connection with any node. The time unit is milliseconds. .settimeout (3000)// The time to wait for the node to reply to the command. The time starts when the command is successfully sent. .setPingTimeout(30000).setreConnectionTimeout (3000)// Interval for reestablishing a connection with a node when it is disconnected. The time unit is milliseconds. .setDatabase(5); Db5 RedissonClient redisson = redisson.create (config); RBucket<String> key = redisson.getBucket("key"); key.set("value"); redisson.shutdown(); }Copy the code

The configuration file in yamL format is redisson-config.yml

In the Resources directory of the project, add redisson’s configuration file

#Redisson config singleServerConfig: address: "redis://127.0.0.1:6379" password: null clientName: null database: 0~15 idleConnectionTimeout: 10000 pingTimeout: 1000 connectTimeout: 10000 timeout: 3000 retryAttempts: 3 retryInterval: 1500 reconnectionTimeout: 3000 failedAttempts: 3 subscriptionsPerConnection: 5 subscriptionConnectionMinimumIdleSize: 1 subscriptionConnectionPoolSize: 50 connectionMinimumIdleSize: 32 connectionPoolSize: 64 dnsMonitoringInterval: 5000 #dnsMonitoring: false threads: 0 nettyThreads: 0 codec: class: "org.redisson.codec.JsonJacksonCodec"Copy the code
Public RedissonClient redisson() throws IOException {// This example uses a configuration file in YAML format. The configuration file is read using config. fromYAML. FromJSON Config Config = Config.fromYAML(RedisPinpline.class.getClassLoader().getResource("redis/redisson-config.yml")); return Redisson.create(config); } @Test public void test06() throws IOException { RedissonClient redisson = redisson(); RBucket<String> key = redisson.getBucket("key"); key.set("value"); redisson.shutdown(); }Copy the code

2. Cluster mode

Programmatic configuration master/slave mode usage:

Config config = new Config(); The config. UseMasterSlaveServers () / / "redniss: / /" can be used to enable the SSL connection. SetMasterAddress (" redis: / / 127.0.0.1:6379)" AddSlaveAddress (" redis: / / 127.0.0.1:6389 ", "redis: / / 127.0.0.1:6332", Redis: / / 127.0.0.1: "6419"). The addSlaveAddress (" redis: / / 127.0.0.1:6399 "); RedissonClient redisson = Redisson.create(config);Copy the code

Configure the cluster mode using the YAML file

--- masterSlaveServersConfig: idleConnectionTimeout: 10000 connectTimeout: 10000 timeout: 3000 retryAttempts: 3 retryInterval: 1500 failedAttempts: 3 password: null subscriptionsPerConnection: 5 clientName: null loadBalancer: ! <org.redisson.connection.balancer.RoundRobinLoadBalancer> {} slaveSubscriptionConnectionMinimumIdleSize: 1 slaveSubscriptionConnectionPoolSize: 50 slaveConnectionMinimumIdleSize: 32 slaveConnectionPoolSize: 64 masterConnectionMinimumIdleSize: 32 masterConnectionPoolSize: 64 readMode: "SLAVE" slaveAddresses: - "redis://127.0.0.1:6381" - "redis://127.0.0.1:6380" masterAddress: "redis://127.0.0.1:6379" database: 0 threads: 0 nettyThreads: 0 codec: ! <org.redisson.codec.JsonJacksonCodec> {} "transportMode":"NIO"Copy the code

3. Sentinel mode

The sentry mode can be configured programmatically as follows:

Config config = new Config(); Config. UseSentinelServers (.) setMasterName (" mymaster) "/ /" redniss: / / "can be used to enable the SSL connection. AddSentinelAddress (127.0.0.1:" 26389 ", "127.0.0.1:26379)." addSentinelAddress (127.0.0.1: "26319"); RedissonClient redisson = Redisson.create(config);Copy the code

Configure the cluster mode using the YAML file

--- sentinelServersConfig: idleConnectionTimeout: 10000 connectTimeout: 10000 timeout: 3000 retryAttempts: 3 retryInterval: 1500 password: null subscriptionsPerConnection: 5 clientName: null loadBalancer: ! <org.redisson.connection.balancer.RoundRobinLoadBalancer> {} slaveSubscriptionConnectionMinimumIdleSize: 1 slaveSubscriptionConnectionPoolSize: 50 slaveConnectionMinimumIdleSize: 32 slaveConnectionPoolSize: 64 masterConnectionMinimumIdleSize: 32 masterConnectionPoolSize: 64 readMode: "SLAVE" sentinelAddresses: - "redis://127.0.0.1:26379" - "redis://127.0.0.1:26389" masterName: "myMaster" database: 0 threads: 0 nettyThreads: 0 codec: ! <org.redisson.codec.JsonJacksonCodec> {} "transportMode":"NIO"Copy the code