Spring boot Redis 2

https://beanbroker.github.io/2018/08/04/Spring/spring_session2/

위의 올린 내용을 이어서 가겠다.

우선 레디스를 날려보자
docker -ps를 통해 보이는 names

docker stop name
docker rm name

우선 간단한 컨트롤러 추가와 dependency추가

pom.xml

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

TripController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


@RestController
public class TripController {

@Autowired
TripRepository tripRepository;

@RequestMapping("/all")
public Object getAllTrip(){


return tripRepository.findAll();

}


}

localhost:8080/all 을 하게되면 현재 redis에 있는 모든 trip Object를 반환한다.
(이를 통해 유저들의 간단한 state정도는 확인 할수 있다. 너무 많은 정보를 담지말자! 그냥 key,value만으로 사용하자)

/all 을 호출하게 될경우 아래의 결과를 얻을 수 있다.
[
{
“id”: “ae9b225e-5184-4258-bc08-5b56d6526c56”,
“title”: “Test Trip”,
“startedAt”: “2018-08-05T07:07:57.389+0000”
}
]

로컬서버를 셧다운 한 후 redis cli를 통해 현재 무슨 데이터가 있는지 확인 하더라도 정상적으로 데아터가 담겨져있다.

현재 소스 기준에서 TripController에 코드를 추가해보자

1
2
3
4
5
6
@GetMapping(value = "/{id}")
public Object getTripById(@PathVariable String id){

return tripRepository.findById(id);

}

레디스cli를 통해 찾을수 있는 id로 localhost:8080/id 로 진행하게 될경우 현재 레디스에 올라가 있는 특정 id값의 데이터를 return받는다.(없으면 null 을 리턴한다.. 예제다.. 완벽을……….. ㅠ)

다음에 이어서 springboot에서 쓰이는 redis client jedis 를 통해 예제를 만들어 볼것이다!

미리 디펜던시를 추가하자

1
2
3
4
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>