只需简单两步,便可以配置好redis cluster连接,然后方便地使用RedisTemplate来存取数据:

1、引入依赖:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

2、配置文件application.yml在添加配置(假设有6个nodes):

spring:
  redis:
    cluster:
      nodes:
        - 192.168.0.17:6390
        - 192.168.0.17:6391
        - 192.168.0.17:6392
        - 192.168.0.9:6390
        - 192.168.0.9:6391
        - 192.168.0.9:6392

3、测试:

	@Autowired
	RedisTemplate<String, String> redisTemplate;
	
	@Test
	public void redisTest() {
		String key = "redisTestKey";
		String value = "I am test value";
		
		ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
		
		//数据插入测试:
		opsForValue.set(key, value);
		String valueFromRedis = opsForValue.get(key);
		logger.info("redis value after set: {}", valueFromRedis);
		assertThat(valueFromRedis, is(value));
		
		//数据删除测试:
		redisTemplate.delete(key);
		valueFromRedis = opsForValue.get(key);
		logger.info("redis value after delete: {}", valueFromRedis);
		assertThat(valueFromRedis, equalTo(null));
	}
使用RedisTemplate,可以方便的存取redis中的内容。


GitHub 加速计划 / sp / spring-boot
40
8
下载
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:3 个月前 )
da516741 Introduce a strategy to `WebApplicationType` to allow modules to implement deduction logic. Prior to this commit, modules played no part in deducing the `WebApplicationType`. This meant that a user with `spring-webflux` for client purposes would deduce `REACTIVE` despite no `spring-boot-webflux` module being present. The following deduction logic order is now implemented: 1) If the `spring-boot-webmvc` module is being used and Spring MVC classes are found then `SERVLET` is used. 2) If the `spring-boot-webflux` module is being used and Spring WebFlux classes are found then `REACTIVE` is used. 3) If `spring-web` is found and servlet classes are available then `SERVLET` is used. 4) If none of the above are satisfied, `NONE` is used. This commit also updates `SpringBootTestContextBootstrapper` to use the same deduction logic. Fixes gh-48517 6 小时前
f9fa1346 Closes gh-48444 11 小时前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐