kafka.entity.changelog.Server Maven / Gradle / Ivy
package kafka.entity.changelog;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.server.ServerBuilder;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import java.time.Duration;
import kafka.entity.changelog.notification.ConsoleNotificationProvider;
import kafka.entity.changelog.notification.NotificationProvider;
import kafka.entity.changelog.topic.TopicChangelog;
import kafka.entity.changelog.topic.TopicHttpService;
public class Server {
public static void main(String[] args) {
Config config = ConfigFactory.load();
Config changelogConfig = config.getConfig("kafka-entity-changelog");
Config topicChangelogConfig = changelogConfig.getConfig("topic");
PrometheusMeterRegistry meterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
new JvmMemoryMetrics().bindTo(meterRegistry);
new JvmGcMetrics().bindTo(meterRegistry);
new JvmThreadMetrics().bindTo(meterRegistry);
new ProcessorMetrics().bindTo(meterRegistry);
Metrics.addRegistry(meterRegistry);
NotificationProvider notificationProvider = new ConsoleNotificationProvider();
TopicChangelog topicChangelog = TopicChangelog.newBuilder()
.syncInterval(Duration.ofMillis(topicChangelogConfig.getLong("sync-interval")))
.creationEventTopicName(topicChangelogConfig.getString("event-topic"))
.commandTopicName(topicChangelogConfig.getString("command-topic"))
.notificationProvider(notificationProvider)
.bootstrapServers(changelogConfig.getString("bootstrap-servers"))
.stateDir(changelogConfig.getString("state-dir") + "/topic-changelog")
.build();
com.linecorp.armeria.server.Server server = new ServerBuilder()
.http(changelogConfig.getInt("http.port"))
.service("/", (ctx, req) -> HttpResponse.of("OK"))
.service("/config/topics", (ctx, req) -> HttpResponse.of(topicChangelog.toString()))
.annotatedService(new TopicHttpService(topicChangelog))
.service("/metrics", (ctx, req) -> HttpResponse.of(meterRegistry.scrape()))
.build();
topicChangelog.run();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.close();
topicChangelog.close();
}));
server.start().join();
}
}