Springboot接続redis_clusterクラスタ
2322 ワード
pom.xml
まずはアプリケーション.properties
Controller
スター起動
テスト:
http://localhost:8080/set/key1/value123
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.data
spring-data-redis
まずはアプリケーション.properties
server.port=8080
spring.redis.database=0
spring.redis.cluster.nodes=192.168.228.130:8001,192.168.228.130:8002,192.168.228.131:8003,192.168.228.131:8004,192.168.228.132:8005,192.168.228.132:8006
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
Controller
package org.howie.redis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private RedisTemplate rt;
@RequestMapping(value="/set/{key}/{value}")
public String set(@PathVariable String key, @PathVariable String value){
ValueOperations redisValue=rt.opsForValue();
redisValue.set(key, value);
return redisValue.get(key);
}
@RequestMapping(value="/get/{key}")
public String get(@PathVariable String key){
ValueOperations redisValue=rt.opsForValue();
return redisValue.get(key);
}
}
スター起動
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Starter {
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
}
}
テスト:
http://localhost:8080/set/key1/value123