yeollow / return-home-safely

Apache License 2.0
1 stars 0 forks source link

enhancement: #25 local Spring api module (CCTV/Police) developing 2 #27

Closed yeollow closed 3 years ago

yeollow commented 3 years ago

local api module

구조는 기존 PR인 #26 내용과 동일하지만 comment내용의 비즈니스 로직(GeoHash) 추가 및 GETPOST 방식의 통신을 가능하게 함.

yeollow commented 3 years ago

Spring HTTP 통신

Http Request / Response에 해당하는 header와 body를 포함하는 class

RestTemplate

Spring Parameter Binding Annotation

Servlet

yeollow commented 3 years ago

geoHash in Redis

해당 PR의 geoHash구현 내용은 MySQL DB에 저장되어있는 모든 Topic에 대한 GPS 위치를 뽑아 위치 별로 encoding된 geoHash값을 통해 prefix가 같은 GPS 위치의 경우는 List로 연결하여 HashMap<String,Collection<GPS>>구조를 가지고 있다.

해당 구현 방법은 geoHash값을 요청하는 req가 있을때마다 모든 GPS정보에대한 geoHash값의 계산 이후 HashMap에 저장하는 꼴이니 절대 효율적이지 못하다.

따라서 Redis에서 GeoHash관련된 GPS정보 색인을 제공한다고 하니 아래의 Spring Redis연동을 통해 해결해보자.

yeollow commented 3 years ago

Spring Redis 연동

Redis geohash

yeollow commented 3 years ago

Nginx

Nginx를 통해 request를 redirect하기.

HTTP 는 기본적으로 80, HTTPS 는 기본적으로 443 포트를 통해 통신하므로 Tomcat 의 기본 포트인 8080포트로 접속하기 위해 Nginx 웹서버를 통해 Reverse Proxy 하도록 한다.

  1. apt-get install nginx 을 통해 nginx 설치
  2. /etc/nginx/nginx 경로에 있는 nginx.conf에 아래와 같은 내용을 작성한다.
  3. service nginx start 를 통해 nginx 구동
  4. listen하는 port로 접속하면 proxy_pass 를 통해 redirect 됨을 확인할 수 있음.

    server {
    listen {port};
    listen [::]:{port};
    
    server_name {hostname or ip};
    
    location / {
         proxy_pass http://{hostname or ip}:{port};
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
    }
    }
    • listen을 통해 지정한 포트로의 요청을 받음
    • [::]는 IPv6에 대한 요청을 받음을 의미 참조
    • server_name : 요청하는 host의 이름
    • location : 특정 url을 처리하는 방법을 정의.
    • 위 내용에서는 /로 설정 해놓았기 때문에 모든 request에 대해 적용
    • proxy_pass : request에 대해 어디로 redirect할 것인지를 설정.
    • proxy_set_header : request한 실ㅔ 데이터를 HTTP header 항목에 할당

Proxy란? : client는 proxy server를 통해 다른 network service에 연결하게 중계해줌

톺아보기

  • 위 내용들은 WS인 Nginx 를 이용하여 Reverse Proxy과정을 거쳐 port forwarding을 > 하는 내용에 기반한다.
  • ssl 인증서를 발급받고 443 포트로 https를 적용하는것이나 loadbalancing 내용도 추후 공부해보면 좋을 것 같다.