Spring-Cloud集成Redisson

前言:目前官方罗列的连接redis的Java客户端虽然有六种最近六个月在更新,不过推荐的只有Jedis,lettuce,Redisson,一般用的比较多的是Jedis,不过相对来说Redisson的功能更强大一些,今天尝试了基于spring-cloud集成Redisson
  1. pom文件,我使用的是redisson的spring boot的starterredisson-spring-boot-starter,目前的版本是3.9.1
<dependencies>
	<dependency>
		<groupId>org.redisson</groupId>
		<artifactId>redisson-spring-boot-starter</artifactId>
		<version>3.9.1</version>
	</dependency>
	<dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-data-21</artifactId>
            <version>3.10.0</version>
     </dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		<version>2.0.1.RELEASE</version>
	</dependency>
</dependencies>
  1. 添加配置到配置文件中application.yml,官方提供了两种方式,第一种是新建一个配置文件,添加连接Redis的配置,然后把对应的配置文件在application.yml引用,例如spring.redis.redisson.config=classpath:redisson.yaml,就是引用Redisson.yaml中的配置,第二种就是直接在application.yml配置
server:
  port: 8003
spring:
  application:
    name: redis-server
  redis:
    cluster:
      nodes: 192.168.1.154:6379,192.168.1.154:6380,192.168.1.155:6379,192.168.1.155:6380,192.168.1.156:6379,192.168.1.156:6380
    password: admin

或者使用外置配置文件的方法

clusterServersConfig:
  idleConnectionTimeout: 10000
  pingTimeout: 1000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  reconnectionTimeout: 3000
  failedAttempts: 3
  password: admin
  subscriptionsPerConnection: 5
  clientName: "cluster"
  loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
  slaveSubscriptionConnectionMinimumIdleSize: 1
  slaveSubscriptionConnectionPoolSize: 50
  slaveConnectionMinimumIdleSize: 32
  slaveConnectionPoolSize: 64
  masterConnectionMinimumIdleSize: 32
  masterConnectionPoolSize: 64
  readMode: "SLAVE"
  nodeAddresses:
  - "redis://192.168.1.154:6379"
  - "redis://192.168.1.154:6380"
  - "redis://192.168.1.155:6379"
  - "redis://192.168.1.155:6380"
  - "redis://192.168.1.156:6379"
  - "redis://192.168.1.156:6380"
  scanInterval: 1000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
transportMode: "NIO"

使用JAVA创建Bean对象

@Configuration
public class RedissonSpringDataConfig {

	@Bean(name = "redissonConnectionFactory")
	public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
		return new RedissonConnectionFactory(redisson);
	}

	@Bean(destroyMethod = "shutdown", name = "redissonClient")
	public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
		Config config = Config.fromYAML(configFile.getInputStream());
		return Redisson.create(config);
	}
}
  1. 添加启动类型
@EnableEurekaClient
@SpringBootApplication
public class RedisApplication {
	public static void main(String[] args) {
		SpringApplication.run(RedisApplication.class, args);
	}
}
  1. 启动项目,查看启动的结果

启动成功并连接redis建立连接

  1. 使用方法
RedissonConnection connection = new RedissonConnection(redissonClient);
connection.zAdd("abc".getBytes(),123,"1".getBytes());
connection.zAdd("abc".getBytes(),124,"2".getBytes());
connection.zAdd("abc".getBytes(),125,"3".getBytes());
connection.zAdd("abc".getBytes(),126,"4".getBytes());
connection.zAdd("abc".getBytes(),127,"5".getBytes());
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值