배치를 돌리면서 consumer하는 속도보다 producer 하는 속도가 더 빨라 설정된 kafka 메모리 초과로 배치가 제대로 실행되지 않았던 경험이있어, 카프카 모니터링 툴을 찾던 중 오픈소스인 CMAK를 설치하기로 했다.
카프카 모니터링 툴은 CMAK외에도 Kafdrop, Burrow 등 여러 오픈소스가 존재하지만,
설치가 간단하고, GUI로 토픽을 생성 및 변경할 수 있으면 좋을 것 같아 CMAK를 선택했다.
CMAK의 주요 기능
CMAK에서 제공하는 기능은 다음과 같다.
1. Kafka Cluster 관리
2. Consumer Lag 관리
3. GUI로 토픽 생성 및 변경
4. 파티션 추가
2. CMAK 설치
설치할 서버환경
CentOS 7
CMAK 설치 전, 기본 환경
JDK 11 이상
kafka 0.8 이상
1) tar.gz 파일 다운로드
# wget https://github.com/yahoo/CMAK/archive/refs/tags/3.0.0.6.tar.gz
# tar -zxvf 3.0.0.6.tar..gz
2) 현재 서버는 jdk1.8이지만 CMAK는 최소 JDK11 이상이기 때문에 sbt 파일을 수정했다.
- 1)에서 압축을 푼 파일로 이동하면 sbt 파일이 있다.
# vi sbt
[sbt]
-- 35번째 java_cmd 경로를 설치한 jdk11 위치로 변경
declare sbt_jar sbt_dir sbt_create sbt_version sbt_script sbt_new
declare sbt_explicit_version
declare verbose noshare batch trace_level
# declare java_cmd="java"
declare java_cmd="/usr/lib/jvm/jdk-11/bin/java" --이렇게!
declare sbt_launch_dir="$HOME/.sbt/launchers"
declare sbt_launch_repo
3) 빌드
# ./sbt clean dist
- 설치경로/CMAK-3.0.0.6/target/universal/cmak-3.0.0.6 생성된걸 확인 할 수 있다.
4) cofing 파일 수정
# cd /빌드한 파일 경로/confi/
-- application.conf 파일 수정
# Settings prefixed with 'kafka-manager.' will be deprecated, use 'cmak.' instead.
# https://github.com/yahoo/CMAK/issues/713
# kafka-manager.zkhosts="kafka-manager-zookeeper:2181"
kafka-manager.zkhosts="localhost:2181"
kafka-manager.zkhosts=${?ZK_HOSTS}
# cmak.zkhosts="kafka-manager-zookeeper:2181"
cmak.zkhosts="localhost:2181"
cmak.zkhosts=${?ZK_HOSTS}
5) 실행
# cd /app/kafkaManager/CMAK-3.0.0.6/target/univeral/cmak-3.0.0.6
# bin/cmak -Dhttp.port=9003 -java-home /usr/lib/jvm/jdk-11
동일하거나 유사한 객체들 사이에 가능한 많은 데이터를 서로 공유하여 사용하도록 하여 메모리 사용량을 최소화하는 디자인 패턴이다.
즉, 자주 변하는 속성과 변하지 않는 속성을 분리하고, 변하지 않는 속성은 재사용하여 메모리 사용을 줄이는 방식이다.
2. Flyweight Pattern의 구성
Flyweight : 공유에 사용할 클래스
FlyweightFactory : Flyweight 인스턴스를 생성 또는 공유
Client : Flyweight : 해당 패턴의 사용자
3. 실습
Shape (공유에 사용할 클래스들의 인터페이스)
public interface Shape {
public void draw();
}
Circle (인터페이스 내용 및 필요한 속성 정의)
public class Circle implements Shape {
private String color;
private int x;
private int y;
private int radius;
public Circle(String color) {
this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Circle [color= " + color +" , x= "+ x + " , y= "+ y +" , radius= "+ radius + " ]" );
}
}
ShapeFactory (객체의 생성 또는 공유의 역할)
import java.util.HashMap;
public class ShapeFactory {
public static final HashMap<String, Circle> circleMap = new HashMap<>();
public static Shape getCircle(String color) {
Circle circle = circleMap.get(color);
if(circle == null) {
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("---- 새로운 객체 생성 " + color +"색 원 ----" );
}
return circle;
}
}
Main 클래스
public class Main {
public static void main(String[] args) {
String[] colors = {"Red", "Yellow", "Pink", "Blue"};
for(int i=0;i<10;i++) {
Circle circle = (Circle) ShapeFactory.getCircle(colors[(int) (Math.random()*4)]);
circle.setX((int) (Math.random()*10));
circle.setY((int) (Math.random()*20));
circle.setRadius((int) (Math.random()*10));
circle.draw();
}
}
}
실행결과
---- 새로운 객체 생성 Blue색 ----
Circle [color= Blue , x= 8 , y= 5 , radius= 0 ]
---- 새로운 객체 생성 Red색 ----
Circle [color= Red , x= 5 , y= 5 , radius= 5 ]
---- 새로운 객체 생성 Pink색 ----
Circle [color= Pink , x= 6 , y= 17 , radius= 4 ]
Circle [color= Blue , x= 0 , y= 1 , radius= 6 ]
---- 새로운 객체 생성 Yellow색 ----
Circle [color= Yellow , x= 7 , y= 1 , radius= 4 ]
Circle [color= Yellow , x= 1 , y= 2 , radius= 0 ]
Circle [color= Red , x= 0 , y= 13 , radius= 3 ]
Circle [color= Yellow , x= 9 , y= 5 , radius= 1 ]
Circle [color= Pink , x= 1 , y= 16 , radius= 0 ]
Circle [color= Red , x= 7 , y= 11 , radius= 2 ]