Skip to content

Commit

Permalink
Minimal sample with Hazelcast and native-image
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Oct 24, 2024
1 parent 1309cf9 commit 71b4f87
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 78 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.4'
id 'org.springframework.boot' version '3.3.5'
id 'io.spring.dependency-management' version '1.1.6'
id 'com.gorylenko.gradle-git-properties' version '2.4.2'
id 'org.graalvm.buildtools.native' version '0.10.3'
}

group = 'org.example'
version = '0.0.1-SNAPSHOT'

java {
toolchain { languageVersion = JavaLanguageVersion.of(23) }
toolchain { languageVersion = JavaLanguageVersion.of(22) }
}

springBoot {
Expand All @@ -23,8 +23,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'io.micrometer:micrometer-tracing-bridge-brave'
// implementation 'io.zipkin.reporter2:zipkin-reporter-brave'
implementation 'com.hazelcast:hazelcast-spring'

runtimeOnly 'io.micrometer:micrometer-registry-prometheus'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
hazelcast:
container_name: hazelcast
image: hazelcast/hazelcast
extra_hosts: [ 'host.docker.internal:host-gateway' ]
restart: always
ports:
- 5701:5701
11 changes: 11 additions & 0 deletions src/main/java/org/example/ecoli/EcoliApplication.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package org.example.ecoli;

import com.hazelcast.client.config.ClientConfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

@EnableCaching
@SpringBootApplication
public class EcoliApplication {
public static void main(String[] args) {
SpringApplication.run(EcoliApplication.class, args);
}

@Bean
ClientConfig hazelcastClientConfig() {
return new ClientConfig();
}

}
73 changes: 4 additions & 69 deletions src/main/java/org/example/ecoli/EcoliRestController.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,17 @@
package org.example.ecoli;

import java.util.List;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.Tracer;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EcoliRestController {
private final MeterRegistry meterRegistry;
private final Tracer tracer;

public EcoliRestController(MeterRegistry meterRegistry, Tracer tracer) {
this.meterRegistry = meterRegistry;
this.tracer = tracer;
}

@Cacheable("echo")
@GetMapping("/echo/{message}")
public EchoResponse echo(@PathVariable String message) {
return new EchoResponse(message);
public String echo(@PathVariable String message) {
return message;
}

@GetMapping("/throughput")
public ThroughputResponse throughput() {
List<String> counters = meterRegistry.find("http.server.requests").timers().stream()
.map(timer -> String.format("%s%s %d", timer.getId().getName(), timer.getId().getTags(), timer.count()))
.toList();
return new ThroughputResponse(counters);
}

@GetMapping("/span")
public SpanResponse span() {
return new SpanResponse(tracer.currentSpan());
}

public record EchoResponse(String message) {
}

public record ThroughputResponse(List<String> counters) {
}

public static class SpanResponse {
private final String traceId;
private final String spanId;
private final String parentSpanId;

SpanResponse(Span span) {
if (span != null) {
TraceContext traceContext = span.context();
this.traceId = traceContext.traceId();
this.spanId = traceContext.spanId();
this.parentSpanId = span.context().parentId();
}
else {
this.traceId = null;
this.spanId = null;
this.parentSpanId = null;
}
}

@SuppressWarnings("unused")
public String getTraceId() {
return this.traceId;
}

@SuppressWarnings("unused")
public String getSpanId() {
return this.spanId;
}

@SuppressWarnings("unused")
public String getParentSpanId() {
return this.parentSpanId;
}
}
}
4 changes: 0 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
spring.application.name=ecoli

management.endpoints.web.exposure.include=*
management.tracing.sampling.probability=1.0
management.metrics.distribution.percentiles-histogram.http.server.requests=true

logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG

0 comments on commit 71b4f87

Please sign in to comment.