9 changed files with 144 additions and 30 deletions
@ -0,0 +1,5 @@
|
||||
FROM openjdk:17-jdk-slim |
||||
VOLUME /tmp |
||||
ARG JAR_FILE=build/libs/*.jar |
||||
COPY ${JAR_FILE} app.jar |
||||
ENTRYPOINT ["java","-jar","/app.jar"] |
@ -0,0 +1,38 @@
|
||||
package com.redis.ha.api; |
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
@RestController |
||||
@Slf4j |
||||
@RequiredArgsConstructor |
||||
@RequestMapping("/v1/redis") |
||||
public class RedisController { |
||||
|
||||
private final RedisTemplate<String, String> redisTemplate; |
||||
|
||||
@GetMapping("/put") |
||||
public ResponseEntity<Void> putData(@RequestParam(name = "key") String key){ |
||||
|
||||
redisTemplate.opsForValue().set(key, "DATA"); |
||||
|
||||
return ResponseEntity.ok().build(); |
||||
} |
||||
|
||||
@GetMapping("/get") |
||||
public ResponseEntity<String> getData(@RequestParam(name = "key") String key){ |
||||
|
||||
String data = redisTemplate.opsForValue().get(key); |
||||
log.info(data); |
||||
|
||||
return ResponseEntity.ok().body(data); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,47 @@
|
||||
package com.redis.ha.config; |
||||
|
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.redis.connection.RedisConnectionFactory; |
||||
import org.springframework.data.redis.connection.RedisPassword; |
||||
import org.springframework.data.redis.connection.RedisSentinelConfiguration; |
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.data.redis.serializer.StringRedisSerializer; |
||||
|
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
@Configuration |
||||
@Slf4j |
||||
@RequiredArgsConstructor |
||||
public class RedisSentinelConfig { |
||||
|
||||
@Value("${spring.data.redis.password}") |
||||
private String password; |
||||
|
||||
@Bean |
||||
public RedisConnectionFactory redisConnectionFactory(){ |
||||
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration() |
||||
.master("mymaster") |
||||
.sentinel("172.23.0.7", 26379) |
||||
.sentinel("172.23.0.8", 26379) |
||||
.sentinel("172.23.0.9", 26379) |
||||
; |
||||
|
||||
redisSentinelConfiguration.setPassword(RedisPassword.of(password)); |
||||
|
||||
return new LettuceConnectionFactory(redisSentinelConfiguration); |
||||
} |
||||
|
||||
@Bean |
||||
public RedisTemplate<String, String> redisTemplate(){ |
||||
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); |
||||
redisTemplate.setConnectionFactory(redisConnectionFactory()); |
||||
redisTemplate.setKeySerializer(new StringRedisSerializer()); |
||||
|
||||
return redisTemplate; |
||||
} |
||||
|
||||
} |
@ -1,7 +1,16 @@
|
||||
spring: |
||||
application: |
||||
name: redis-ha |
||||
|
||||
data: |
||||
redis: |
||||
sentinel: |
||||
master: mymaster |
||||
nodes: |
||||
- 172.23.0.7:26379 |
||||
- 172.23.0.8:26379 |
||||
- 172.23.0.9:26379 |
||||
password: palnet!234 |
||||
timeout: 3000 |
||||
|
||||
server: |
||||
port: 8060 |
Loading…
Reference in new issue