본문 바로가기

spring/그 외 알게 된 것

프록시란

프록시란?

프록시는 대리자 또는 대리인

프록시는 다른 무언가를 대신해서 일을 해주는 역할

 

프록시 디자인 패턴:

  • 객체 지향 프로그래밍에서, 프록시는 다른 객체에 대한 접근을 제어하거나 추가 기능을 제공
  • 예를 들어, 실제 객체에 접근하기 전에 무언가를 할 필요가 있을 때 프록시를 사용할 수 있음 이를 통해 실제 객체를 감싸고 추가 로직을 추가할 수 있음
public interface Image {
    void display();
}

public class RealImage implements Image {
    private String fileName;

    public RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    private void loadFromDisk(String fileName) {
        System.out.println("Loading " + fileName);
    }

    public void display() {
        System.out.println("Displaying " + fileName);
    }
}

public class ProxyImage implements Image {
    private RealImage realImage;
    private String fileName;

    public ProxyImage(String fileName) {
        this.fileName = fileName;
    }

    public void display() {
        if (realImage == null) {
            realImage = new RealImage(fileName);
        }
        realImage.display();
    }
}

public class ProxyPatternDemo {
    public static void main(String[] args) {
        Image image = new ProxyImage("test_10mb.jpg");

        // 이미지가 실제로 로드되기 전
        image.display();

        // 이미지가 이미 로드된 후
        image.display();
    }
}

이 예시에서 ProxyImage는 RealImage를 대신해서 이미지를 불러오고 표시

처음 이미지를 표시할 때만 실제 이미지를 로드하고, 이후에는 로드된 이미지를 사용

 

네트워크 프록시:

  • 네트워크에서 프록시는 클라이언트와 서버 사이에 중간자 역할을 하는 서버

네트워크 프록시의 예:

  • 캐싱: 자주 요청되는 데이터를 프록시 서버에 저장해 두어, 클라이언트가 동일한 데이터를 요청할 때마다 실제 서버에 다시 요청하지 않고 프록시 서버에서 데이터를 반환
  • 보안: 클라이언트가 실제 서버에 직접 접근하지 못하게 함
  • 익명성: 클라이언트의 IP 주소를 숨기고 프록시 서버의 IP 주소로 요청을 보내어 익명성을 제공

'spring > 그 외 알게 된 것' 카테고리의 다른 글

Gson과 Json은 뭐가 다를까  (0) 2024.07.02
어노테이션 정리  (0) 2024.06.29
왜 System.out 대신 Logger를 사용해야 할까?  (0) 2024.06.25
jar와 war  (0) 2024.06.25
JDBC와 JPA  (0) 2024.06.25