跳到主要内容

34 篇博文 含有标签「spring-boot」

查看所有标签

Spark as a Service with Spring Boot

· 阅读需 13 分钟
  1. Tech Stack and Target Environment
  2. Architecture Design
  3. Flow Descriptions
  4. Sequence Diagrams
  5. Components Design
  6. Configuration and Code
  7. Job Queue Management
  8. Spark Job Submission
  9. Test Cases
  10. Global Exception Handler
  11. Project Structure

In this article, we will explore the development of a stateless Spring Boot application using Spring WebFlux 3.3.5 and Apache Spark 3.5.4. This application will serve as a Spark-as-a-Service, designed to run in a Kubernetes (K8s) environment with multiple replicas. The application will use a centralized queue in Redis to manage Spark job requests, with a copy of the job queue also maintained in PostgreSQL for job history and tracking. The application will ensure that each job is executed only once, manage job states and statuses correctly, and provide RESTful endpoints for client interaction and monitoring.

Tech Stack and Target Environment​

Spark Cluster​

  • Version: 3.5.4
  • Deployment: Kubernetes namespace whereq-saas-spark
  • Configuration: Default Bitnami Helm chart (1 master, 2 workers)

Spring Boot WebFlux Application​

  • Version: 3.3.5
  • Deployment: Kubernetes namespace whereq-saas-web
  • Replicas: 2
  • Ingress: Exposed for frontend access

Queue and Database​

  • Queue: Redis (for job queue management)
  • Database: PostgreSQL with R2DBC driver
  • Container Orchestration: Kubernetes

Architecture Design​

The application is structured to be stateless and scalable. It integrates with Redis to queue job submissions, PostgreSQL for persistent storage, and the Spark REST API for job submission and tracking.

High-Level Architecture​

graph TD
A[Client] -->|Submit Task| B[Spring Boot Application]
B -->|Store Task Request| C[Redis Queue]
C -->|Sync Job Request| D[PostgreSQL]
D -->|Fetch Job| B
B -->|Check Cluster Status| E[Spark REST API]
E -->|Determine Resources| B
B -->|Submit Job| F[Spark Master]
F -->|Execute Job| G[Spark Worker]
G -->|Store Result| D
B -->|Query Job History| D
B -->|Monitor Job Queue| C

Components​

  • Job Controller: Accepts job submissions via HTTP POST and returns a task ID.
  • Job Queue Service: Handles job insertion into Redis and PostgreSQL.
  • Job Monitor Service: Monitors Redis, submits jobs to Spark, and tracks job status.
  • Spark Service: Interfaces with the Spark REST API for job submission and status updates.
  • State Machine: Manages job state transitions (e.g., ready, running, completed) and status updates (success, failed).

Architecture Diagram​

+---------------+ +---------------------+ +----------------+
| Job Controller| ----> | Job Queue Service | -----> | Redis Queue |
+---------------+ +---------------------+ +----------------+
|
+--------------------------------------------------------+
|
+-------------------+ +------------------+ +------------------+
| Job Monitor | -----> | Spark Service | <-----> | Spark Master Pod |
| (Background Task) | +------------------+ +------------------+
+-------------------+ |
|
+-------------------------------+
| PostgreSQL (Sync Job State) |
+-------------------------------+

Detailed Architecture​

  1. Client: Submits Spark job requests via RESTful endpoints.
  2. Spring Boot Application:
    • Receives job requests.
    • Stores job requests in the Redis queue.
    • Syncs job requests with PostgreSQL.
    • Checks cluster status via Spark REST API.
    • Submits jobs to Spark Master based on available resources.
    • Manages job states and statuses.
    • Provides RESTful endpoints for job history queries and monitoring.
  3. Redis Queue: Stores job requests.
  4. PostgreSQL: Stores job requests and history.
  5. Spark REST API: Provides cluster status information.
  6. Spark Master: Receives job submissions.
  7. Spark Worker: Executes Spark jobs.
  8. PostgreSQL: Stores job results and history.

Flow Descriptions​

Task Submission Flow​

  1. Task Controller receives task requests via HTTP POST.
  2. Task Queue Service validates the request, assigns a task ID, stores it in Redis with a READY state, and syncs it to PostgreSQL.
  3. The service returns a task ID to the client.

Spark Job Submission Flow​

  1. Job Monitor Service picks up jobs in the READY state from the Redis queue using FIFO.
  2. The job is submitted to the Spark master via the Spark Service, and the job state is updated to SUBMITTED.
  3. The state is synced to PostgreSQL.
  4. The Job Monitor Service starts tracking the job status until completion or failure.
  5. The final state (COMPLETED or FAILED) is updated in both Redis and PostgreSQL.

sequenceDiagram
participant Client
participant SpringBootApp
participant RedisQueue
participant PostgreSQL
participant SparkRESTAPI
participant SparkMaster
participant SparkWorker

Client ->> SpringBootApp: Submit Job Request
SpringBootApp ->> RedisQueue: Store Job Request
RedisQueue ->> PostgreSQL: Sync Job Request
SpringBootApp ->> SparkRESTAPI: Check Cluster Status
SparkRESTAPI -->> SpringBootApp: Cluster Status
SpringBootApp ->> SparkMaster: Submit Job
SparkMaster ->> SparkWorker: Execute Job
SparkWorker -->> PostgreSQL: Store Job Result
SpringBootApp -->> Client: Job Status

Job History Query​

sequenceDiagram
participant Client
participant SpringBootApp
participant PostgreSQL

Client ->> SpringBootApp: Query Job History
SpringBootApp ->> PostgreSQL: Fetch Job History
PostgreSQL -->> SpringBootApp: Job History
SpringBootApp -->> Client: Job History

​

Components Design​

1. Job Request Model​

public class JobRequest {
private String id;
private String jobName;
private String jobConfig;
private JobState state;
private JobStatus status;
// Getters and Setters
}

2. Job State and Status Enums​

public enum JobState {
READY, PENDING, RUNNING, COMPLETED
}

public enum JobStatus {
SUCCESS, FAILED
}

3. Redis Queue Service​

@Service
public class RedisQueueService {
private final ReactiveRedisTemplate<String, JobRequest> redisTemplate;

public RedisQueueService(ReactiveRedisTemplate<String, JobRequest> redisTemplate) {
this.redisTemplate = redisTemplate;
}

public Mono<Boolean> enqueueJob(JobRequest jobRequest) {
return redisTemplate.opsForList().rightPush("jobQueue", jobRequest);
}

public Mono<JobRequest> dequeueJob() {
return redisTemplate.opsForList().leftPop("jobQueue");
}

public Mono<Boolean> updateJobStatusInQueue(String jobId, JobState state, JobStatus status) {
return redisTemplate.opsForList().indexOf("jobQueue", jobId)
.flatMap(index -> redisTemplate.opsForList().set(index, new JobRequest(jobId, state, status)));
}
}

4. PostgreSQL Service​

@Service
public class PostgresService {
private final R2dbcEntityTemplate r2dbcEntityTemplate;

public PostgresService(R2dbcEntityTemplate r2dbcEntityTemplate) {
this.r2dbcEntityTemplate = r2dbcEntityTemplate;
}

public Mono<JobRequest> saveJobRequest(JobRequest jobRequest) {
return r2dbcEntityTemplate.insert(jobRequest);
}

public Mono<JobRequest> getJobById(String jobId) {
return r2dbcEntityTemplate.selectOne(Query.query(Criteria.where("id").is(jobId)), JobRequest.class);
}

public Mono<Integer> updateJobStatus(String jobId, JobState state, JobStatus status) {
return r2dbcEntityTemplate.update(JobRequest.class)
.matching(Query.query(Criteria.where("id").is(jobId)))
.apply(Update.update("state", state).set("status", status));
}

public Flux<JobRequest> getJobHistory(Map<String, String> queryParams) {
// Fetch job history from PostgreSQL
return r2dbcEntityTemplate.select(JobRequest.class).all();
}
}

5. Spark REST API Service​

@Service
public class SparkRestApiService {
private final WebClient webClient;

public SparkRestApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://spark-master:8080").build();
}

public Mono<ClusterStatus> getClusterStatus() {
return webClient.get()
.uri("/api/v1/applications")
.retrieve()
.bodyToMono(String.class)
.map(response -> {
// Parse response and return ClusterStatus
return new ClusterStatus();
});
}

public Mono<JobStatus> monitorJobStatus(String jobId) {
return webClient.get()
.uri("/api/v1/submissions/status/{jobId}", jobId)
.retrieve()
.bodyToMono(String.class)
.map(response -> {
// Parse response and return JobStatus
return JobStatus.SUCCESS; // Example
});
}
}

6. Job Executor Service​

@Service
public class JobExecutorService {
private final RedisQueueService redisQueueService;
private final PostgresService postgresService;
private final SparkRestApiService sparkRestApiService;
private final WebClient webClient;

public JobExecutorService(RedisQueueService redisQueueService, PostgresService postgresService, SparkRestApiService sparkRestApiService, WebClient.Builder webClientBuilder) {
this.redisQueueService = redisQueueService;
this.postgresService = postgresService;
this.sparkRestApiService = sparkRestApiService;
this.webClient = webClientBuilder.baseUrl("http://spark-master:8080").build();
}

public Mono<Void> executeNextJob() {
return redisQueueService.dequeueJob()
.flatMap(jobRequest -> {
jobRequest.setState(JobState.PENDING);
return postgresService.updateJobStatus(jobRequest.getId(), JobState.PENDING, JobStatus.SUCCESS)
.then(sparkRestApiService.getClusterStatus())
.flatMap(clusterStatus -> {
if (clusterStatus.hasAvailableResources()) {
return submitJobToSpark(jobRequest);
} else {
return redisQueueService.enqueueJob(jobRequest).then();
}
});
});
}

private Mono<Void> submitJobToSpark(JobRequest jobRequest) {
return webClient.post()
.uri("/v1/submissions/create")
.bodyValue(jobRequest)
.retrieve()
.toBodilessEntity()
.flatMap(response -> {
if (response.getStatusCode().is2xxSuccessful()) {
jobRequest.setState(JobState.RUNNING);
return postgresService.updateJobStatus(jobRequest.getId(), JobState.RUNNING, JobStatus.SUCCESS)
.then(monitorJobResult(jobRequest));
} else {
jobRequest.setState(JobState.COMPLETED);
jobRequest.setStatus(JobStatus.FAILED);
return updateJobStatus(jobRequest);
}
});
}

private Mono<Void> monitorJobResult(JobRequest jobRequest) {
return sparkRestApiService.monitorJobStatus(jobRequest.getId())
.flatMap(jobStatus -> {
jobRequest.setState(JobState.COMPLETED);
jobRequest.setStatus(jobStatus);
return updateJobStatus(jobRequest);
});
}

private Mono<Void> updateJobStatus(JobRequest jobRequest) {
return redisQueueService.updateJobStatusInQueue(jobRequest.getId(), jobRequest.getState(), jobRequest.getStatus())
.then(postgresService.updateJobStatus(jobRequest.getId(), jobRequest.getState(), jobRequest.getStatus()))
.then();
}
}

7. RESTful Endpoints​

@RestController
@RequestMapping("/api/jobs")
public class JobController {
private final JobExecutorService jobExecutorService;
private final PostgresService postgresService;

public JobController(JobExecutorService jobExecutorService, PostgresService postgresService) {
this.jobExecutorService = jobExecutorService;
this.postgresService = postgresService;
}

@PostMapping
public Mono<ResponseEntity<String>> submitJob(@RequestBody JobRequest jobRequest) {
return jobExecutorService.submitJob(jobRequest)
.thenReturn(ResponseEntity.ok("Job submitted successfully"));
}

@GetMapping("/history")
public Flux<JobRequest> getJobHistory(@RequestParam Map<String, String> queryParams) {
return postgresService.getJobHistory(queryParams);
}

@GetMapping("/status")
public Mono<ResponseEntity<String>> getJobStatus(@RequestParam String jobId) {
return postgresService.getJobById(jobId)
.map(jobRequest -> ResponseEntity.ok(jobRequest.getStatus().name()));
}
}

​

Configuration and Code​

1. Spring Boot Application Configuration​

spring:
redis:
host: localhost
port: 6379
r2dbc:
url: r2dbc:postgresql://localhost:5432/spark_jobs
username: postgres
password: postgres
webclient:
baseUrl: http://spark-master:8080

2. Spark REST API Configuration​

@Configuration
public class SparkRestApiConfig {
@Bean
public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
}

3. Redis Configuration​

@Configuration
public class RedisConfig {
@Bean
public ReactiveRedisTemplate<String, JobRequest> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
RedisSerializationContext<String, JobRequest> serializationContext = RedisSerializationContext
.newSerializationContext(new StringRedisSerializer())
.value(new Jackson2JsonRedisSerializer<>(JobRequest.class))
.build();
return new ReactiveRedisTemplate<>(factory, serializationContext);
}
}

4. R2DBC Configuration​

@Configuration
public class R2dbcConfig {
@Bean
public ConnectionFactory connectionFactory(R2dbcProperties properties) {
return ConnectionFactories.get(properties.getUrl());
}

@Bean
public R2dbcEntityTemplate r2dbcEntityTemplate(ConnectionFactory connectionFactory) {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
return new R2dbcEntityTemplate(databaseClient);
}
}

5. Swagger Configuration​

@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.sparkasaservice.controller"))
.paths(PathSelectors.any())
.build();
}
}

​

Job Queue Management​

1. Job Queue Service​

The RedisQueueService class is responsible for managing the job queue in Redis.

@Service
public class RedisQueueService {
private final ReactiveRedisTemplate<String, JobRequest> redisTemplate;

public RedisQueueService(ReactiveRedisTemplate<String, JobRequest> redisTemplate) {
this.redisTemplate = redisTemplate;
}

public Mono<Boolean> enqueueJob(JobRequest jobRequest) {
return redisTemplate.opsForList().rightPush("jobQueue", jobRequest);
}

public Mono<JobRequest> dequeueJob() {
return redisTemplate.opsForList().leftPop("jobQueue");
}

public Mono<Boolean> updateJobStatusInQueue(String jobId, JobState state, JobStatus status) {
return redisTemplate.opsForList().indexOf("jobQueue", jobId)
.flatMap(index -> redisTemplate.opsForList().set(index, new JobRequest(jobId, state, status)));
}
}

2. Job State Management​

The JobRequest class includes fields for JobState and JobStatus to manage the state and status of each job.

public class JobRequest {
private String id;
private String jobName;
private String jobConfig;
private JobState state;
private JobStatus status;
// Getters and Setters
}

3. Job Execution​

The JobExecutorService class is responsible for executing jobs from the queue. It ensures that each job is executed only once and updates the job state and status accordingly.

@Service
public class JobExecutorService {
private final RedisQueueService redisQueueService;
private final PostgresService postgresService;
private final SparkRestApiService sparkRestApiService;
private final WebClient webClient;

public JobExecutorService(RedisQueueService redisQueueService, PostgresService postgresService, SparkRestApiService sparkRestApiService, WebClient.Builder webClientBuilder) {
this.redisQueueService = redisQueueService;
this.postgresService = postgresService;
this.sparkRestApiService = sparkRestApiService;
this.webClient = webClientBuilder.baseUrl("http://spark-master:8080").build();
}

public Mono<Void> executeNextJob() {
return redisQueueService.dequeueJob()
.flatMap(jobRequest -> {
jobRequest.setState(JobState.PENDING);
return postgresService.updateJobStatus(jobRequest.getId(), JobState.PENDING, JobStatus.SUCCESS)
.then(sparkRestApiService.getClusterStatus())
.flatMap(clusterStatus -> {
if (clusterStatus.hasAvailableResources()) {
return submitJobToSpark(jobRequest);
} else {
return redisQueueService.enqueueJob(jobRequest).then();
}
});
});
}

private Mono<Void> submitJobToSpark(JobRequest jobRequest) {
return webClient.post()
.uri("/v1/submissions/create")
.bodyValue(jobRequest)
.retrieve()
.toBodilessEntity()
.flatMap(response -> {
if (response.getStatusCode().is2xxSuccessful()) {
jobRequest.setState(JobState.RUNNING);
return postgresService.updateJobStatus(jobRequest.getId(), JobState.RUNNING, JobStatus.SUCCESS)
.then(monitorJobResult(jobRequest));
} else {
jobRequest.setState(JobState.COMPLETED);
jobRequest.setStatus(JobStatus.FAILED);
return updateJobStatus(jobRequest);
}
});
}

private Mono<Void> monitorJobResult(JobRequest jobRequest) {
return sparkRestApiService.monitorJobStatus(jobRequest.getId())
.flatMap(jobStatus -> {
jobRequest.setState(JobState.COMPLETED);
jobRequest.setStatus(jobStatus);
return updateJobStatus(jobRequest);
});
}

private Mono<Void> updateJobStatus(JobRequest jobRequest) {
return redisQueueService.updateJobStatusInQueue(jobRequest.getId(), jobRequest.getState(), jobRequest.getStatus())
.then(postgresService.updateJobStatus(jobRequest.getId(), jobRequest.getState(), jobRequest.getStatus()))
.then();
}
}

​

Spark Job Submission​

1. Job Submission Endpoint​

The JobController class provides an endpoint for submitting jobs. It uses the JobExecutorService to handle job submission.

@RestController
@RequestMapping("/api/jobs")
public class JobController {
private final JobExecutorService jobExecutorService;
private final PostgresService postgresService;

public JobController(JobExecutorService jobExecutorService, PostgresService postgresService) {
this.jobExecutorService = jobExecutorService;
this.postgresService = postgresService;
}

@PostMapping
public Mono<ResponseEntity<String>> submitJob(@RequestBody JobRequest jobRequest) {
return jobExecutorService.submitJob(jobRequest)
.thenReturn(ResponseEntity.ok("Job submitted successfully"));
}

@GetMapping("/history")
public Flux<JobRequest> getJobHistory(@RequestParam Map<String, String> queryParams) {
return postgresService.getJobHistory(queryParams);
}

@GetMapping("/status")
public Mono<ResponseEntity<String>> getJobStatus(@RequestParam String jobId) {
return postgresService.getJobById(jobId)
.map(jobRequest -> ResponseEntity.ok(jobRequest.getStatus().name()));
}
}

2. Job Execution​

The JobExecutorService class is responsible for executing jobs from the queue. It ensures that each job is executed only once and updates the job state and status accordingly.

@Service
public class JobExecutorService {
private final RedisQueueService redisQueueService;
private final PostgresService postgresService;
private final SparkRestApiService sparkRestApiService;
private final WebClient webClient;

public JobExecutorService(RedisQueueService redisQueueService, PostgresService postgresService, SparkRestApiService sparkRestApiService, WebClient.Builder webClientBuilder) {
this.redisQueueService = redisQueueService;
this.postgresService = postgresService;
this.sparkRestApiService = sparkRestApiService;
this.webClient = webClientBuilder.baseUrl("http://spark-master:8080").build();
}

public Mono<Void> executeNextJob() {
return redisQueueService.dequeueJob()
.flatMap(jobRequest -> {
jobRequest.setState(JobState.PENDING);
return postgresService.updateJobStatus(jobRequest.getId(), JobState.PENDING, JobStatus.SUCCESS)
.then(sparkRestApiService.getClusterStatus())
.flatMap(clusterStatus -> {
if (clusterStatus.hasAvailableResources()) {
return submitJobToSpark(jobRequest);
} else {
return redisQueueService.enqueueJob(jobRequest).then();
}
});
});
}

private Mono<Void> submitJobToSpark(JobRequest jobRequest) {
return webClient.post()
.uri("/v1/submissions/create")
.bodyValue(jobRequest)
.retrieve()
.toBodilessEntity()
.flatMap(response -> {
if (response.getStatusCode().is2xxSuccessful()) {
jobRequest.setState(JobState.RUNNING);
return postgresService.updateJobStatus(jobRequest.getId(), JobState.RUNNING, JobStatus.SUCCESS)
.then(monitorJobResult(jobRequest));
} else {
jobRequest.setState(JobState.COMPLETED);
jobRequest.setStatus(JobStatus.FAILED);
return updateJobStatus(jobRequest);
}
});
}

private Mono<Void> monitorJobResult(JobRequest jobRequest) {
return sparkRestApiService.monitorJobStatus(jobRequest.getId())
.flatMap(jobStatus -> {
jobRequest.setState(JobState.COMPLETED);
jobRequest.setStatus(jobStatus);
return updateJobStatus(jobRequest);
});
}

private Mono<Void> updateJobStatus(JobRequest jobRequest) {
return redisQueueService.updateJobStatusInQueue(jobRequest.getId(), jobRequest.getState(), jobRequest.getStatus())
.then(postgresService.updateJobStatus(jobRequest.getId(), jobRequest.getState(), jobRequest.getStatus()))
.then();
}
}

​

Test Cases​

1. Job Submission Test​

@SpringBootTest
@AutoConfigureWebTestClient
public class JobControllerTest {

@Autowired
private WebTestClient webTestClient;

@Test
public void testJobSubmission() {
JobRequest jobRequest = new JobRequest();
jobRequest.setId("1");
jobRequest.setJobName("TestJob");
jobRequest.setJobConfig("SELECT * FROM test_table");

webTestClient.post().uri("/api/jobs")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(jobRequest)
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("Job submitted successfully");
}
}

2. Job Execution Test​

@SpringBootTest
public class JobExecutorServiceTest {

@Autowired
private JobExecutorService jobExecutorService;

@Autowired
private RedisQueueService redisQueueService;

@Autowired
private PostgresService postgresService;

@Test
public void testJobExecution() {
JobRequest jobRequest = new JobRequest();
jobRequest.setId("1");
jobRequest.setJobName("TestJob");
jobRequest.setJobConfig("SELECT * FROM test_table");

StepVerifier.create(redisQueueService.enqueueJob(jobRequest)
.then(jobExecutorService.executeNextJob())
.then(postgresService.getJobById("1")))
.expectNextMatches(executedJob -> {
assertNotNull(executedJob);
assertEquals(JobState.COMPLETED, executedJob.getState());
assertEquals(JobStatus.SUCCESS, executedJob.getStatus());
return true;
})
.verifyComplete();
}
}

3. Job History Query Test​

@SpringBootTest
@AutoConfigureWebTestClient
public class JobControllerTest {

@Autowired
private WebTestClient webTestClient;

@Test
public void testJobHistoryQuery() {
webTestClient.get().uri(uriBuilder -> uriBuilder
.path("/api/jobs/history")
.queryParam("jobName", "TestJob")
.build())
.exchange()
.expectStatus().isOk()
.expectBodyList(JobRequest.class)
.value(jobRequests -> {
assertNotNull(jobRequests);
assertTrue(jobRequests.stream().anyMatch(jobRequest -> "TestJob".equals(jobRequest.getJobName())));
});
}
}

4. Job Status Query Test​

@SpringBootTest
@AutoConfigureWebTestClient
public class JobControllerTest {

@Autowired
private WebTestClient webTestClient;

@Test
public void testJobStatusQuery() {
webTestClient.get().uri("/api/jobs/status?jobId=1")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("SUCCESS");
}
}

​

Global Exception Handler​

1. Global Exception Handler Configuration​

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred: " + ex.getMessage());
}

@ExceptionHandler(WebClientResponseException.class)
public ResponseEntity<String> handleWebClientResponseException(WebClientResponseException ex) {
return ResponseEntity.status(ex.getStatusCode()).body("WebClient error: " + ex.getResponseBodyAsString());
}

@ExceptionHandler(R2dbcDataIntegrityViolationException.class)
public ResponseEntity<String> handleDataIntegrityViolationException(R2dbcDataIntegrityViolationException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Data integrity violation: " + ex.getMessage());
}
}

​

Project Structure​

Project Structure Diagram​

spark-as-a-service/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── sparkasaservice/
│ │ │ ├── config/
│ │ │ │ ├── RedisConfig.java
│ │ │ │ ├── SparkRestApiConfig.java
│ │ │ │ ├── R2dbcConfig.java
│ │ │ │ └── SwaggerConfig.java
│ │ │ ├── controller/
│ │ │ │ └── JobController.java
│ │ │ ├── exception/
│ │ │ │ └── GlobalExceptionHandler.java
│ │ │ ├── model/
│ │ │ │ ├── JobRequest.java
│ │ │ │ ├── JobState.java
│ │ │ │ └── JobStatus.java
│ │ │ ├── repository/
│ │ │ │ └── JobRepository.java
│ │ │ ├── service/
│ │ │ │ ├── JobExecutorService.java
│ │ │ │ ├── PostgresService.java
│ │ │ │ ├── RedisQueueService.java
│ │ │ │ └── SparkRestApiService.java
│ │ │ └── SparkAsAServiceApplication.java
│ │ └── resources/
│ │ ├── application.yml
│ │ └── schema.sql
│ └── test/
│ ├── java/
│ │ └── com/
│ │ └── example/
│ │ └── sparkasaservice/
│ │ ├── controller/
│ │ │ └── JobControllerTest.java
│ │ ├── service/
│ │ │ └── JobExecutorServiceTest.java
│ │ └── SparkAsAServiceApplicationTests.java
│ └── resources/
│ └── application-test.yml
└── pom.xml

Listening to Redis Queue​

1. Redis Queue Listener Configuration​

To listen to the Redis queue and trigger the executeNextJob method, we can use Spring Data Redis's reactive support with Lettuce as the Redis client.

2. Redis Queue Listener Service​

@Service
public class RedisQueueListenerService {
private final JobExecutorService jobExecutorService;
private final ReactiveRedisTemplate<String, JobRequest> redisTemplate;

public RedisQueueListenerService(JobExecutorService jobExecutorService, ReactiveRedisTemplate<String, JobRequest> redisTemplate) {
this.jobExecutorService = jobExecutorService;
this.redisTemplate = redisTemplate;
}

@PostConstruct
public void startListening() {
redisTemplate.listenToPattern("__keyevent@*__:rpush")
.doOnNext(message -> {
if (message.getChannel().equals("__keyevent@0__:rpush") && message.getMessage().equals("jobQueue")) {
jobExecutorService.executeNextJob().subscribe();
}
})
.subscribe();
}
}

3. Redis Configuration for Pub/Sub​

@Configuration
public class RedisConfig {
@Bean
public ReactiveRedisTemplate<String, JobRequest> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
RedisSerializationContext<String, JobRequest> serializationContext = RedisSerializationContext
.newSerializationContext(new StringRedisSerializer())
.value(new Jackson2JsonRedisSerializer<>(JobRequest.class))
.build();
return new ReactiveRedisTemplate<>(factory, serializationContext);
}

@Bean
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(RedisProperties redisProperties) {
return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());
}
}

This article provides a comprehensive guide to developing a stateless Spring Boot application for Spark as a Service, along with detailed explanations of job queue management, Spark job submission, and test cases. Each section is designed to be detailed and actionable, ensuring that readers can implement the concepts effectively.

Best Practices for Spring Application Health Check and Actuator

· 阅读需 8 分钟

Introduction​

Spring Actuator is a powerful tool for monitoring and managing Spring Boot applications. It provides production-ready features such as health checks, metrics, and tracing. Properly configuring and securing Actuator endpoints is crucial for ensuring the reliability and security of your application.

Key Benefits:

  • Helps detect issues before they affect users.
  • Provides an easy integration with monitoring systems like Prometheus, Grafana, and ELK stack.
  • Customizable to expose only the needed endpoints.

Spring Actuator Basics​

2.1 What is Spring Actuator?​

Spring Actuator is a sub-project of the Spring Boot framework that provides production-ready features for monitoring and managing applications. It exposes various endpoints that can be used to check the health of the application, gather metrics, environment and perform other administrative tasks.

2.2 Key Features​

Spring Boot Actuator offers various endpoints for different purposes:

Endpoint
Health Checks/actuator/healthProvides information about the health status of the application.
Metrics/actuator/metricsExposes various metrics such as memory usage, active threads, HTTP requests, and more.
Info Endpoint/actuator/infoProvides general or arbitrary information about the application.
Environment Endpoint/actuator/envShows properties from the Environment.
Shutdown Endpoint/actuator/shutdownAllows for graceful shutdown of the application.

2.3. Architecture Overview​

The architecture of a Spring Boot application with Actuator enabled can be visualized as follows:

+---------------------+
| Spring Boot App |
| |
| +---------------+ |
| | Actuator | |
| +---------------+ |
| Health Checks |
| Metrics |
| Info Endpoint |
+---------------------+
|
v
+-----------------------------+
| Monitoring System (e.g., |
| Prometheus, Grafana, ELK) |
+-----------------------------+
sequenceDiagram
participant Client
participant Actuator Endpoint
participant Health Indicator(s)

Client->>Actuator Endpoint: GET /actuator/health
Actuator Endpoint->>Health Indicator(s): Evaluate Health
Health Indicator(s)-->>Actuator Endpoint: Return Status
Actuator Endpoint-->>Client: Respond with Health Status

Best Practices​

3.1 Configuring Actuator Endpoints​

Add the following dependency to your pom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Configure Actuator endpoints to expose only the necessary information. Use the management.endpoints.web.exposure.include property to specify which endpoints should be exposed.

Configure Actuator in application.properties or application.yml:

application.properties

management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=always

application.yaml

management:
endpoints:
web:
exposure:
include: health, metrics, info
endpoint:
health:
show-details: always

3.2 Securing Actuator Endpoints​

It is essential to secure your Actuator endpoints to prevent unauthorized access. Use Spring Security to restrict access to specific endpoints.

application.properties

management.endpoints.web.exposure.include=health,info
management.endpoints.web.base-path=/actuator
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/actuator/health").permitAll()
.antMatchers("/actuator/**").hasRole("ADMIN")
.and()
.httpBasic();
}
}

3.3 Custom Health Indicators​

Spring Boot allows you to create custom health indicators for specific checks, such as database connectivity or third-party service availability. Implement custom health indicators to monitor specific components of your application. Custom health indicators should extend the HealthIndicator interface.

CustomHealthIndicator

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {

@Override
public Health health() {
// Perform custom health check
boolean isHealthy = checkCustomComponent();
if (isHealthy) {
return Health.up().withDetail("Custom Service", "Available").build();
} else {
return Health.down().withDetail("Custom Service", "Custom component is down").build();
}
}

private boolean checkCustomComponent() {
// Custom health check logic
return true;
}
}

Unit Test for CustomHealthIndicator

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.actuate.health.Health;

@SpringBootTest
public class CustomHealthIndicatorTest {

@Autowired
private CustomHealthIndicator customHealthIndicator;

@Test
public void healthIndicatorStatusShouldBeUp() {
Health health = customHealthIndicator.health();
assertThat(health.getStatus().getCode()).isEqualTo("UP");
}
}

3.4 Monitoring and Metrics​

Enable and configure metrics to monitor the performance and health of your application. Use the micrometer library to expose metrics to various monitoring systems.

management:
metrics:
export:
prometheus:
enabled: true

3.5 Graceful Shutdown​

Configure graceful shutdown to ensure that your application shuts down cleanly without interrupting ongoing requests.

server:
shutdown: graceful

Production Scenarios​

4.1 Scenario 1: Database Health Check​

Implement a health check for the database to ensure that the application can connect to the database.

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Component
public class DatabaseHealthIndicator implements HealthIndicator {

private final DataSource dataSource;

public DatabaseHealthIndicator(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
public Health health() {
try (Connection connection = dataSource.getConnection()) {
if (connection.isValid(1000)) {
return Health.up().build();
} else {
return Health.down().withDetail("Error", "Database connection is not valid").build();
}
} catch (SQLException e) {
return Health.down(e).build();
}
}
}

4.2 Scenario 2: Custom Health Indicator​

Implement a custom health indicator to monitor a specific external service.

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class ExternalServiceHealthIndicator implements HealthIndicator {

private final RestTemplate restTemplate;

public ExternalServiceHealthIndicator(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Override
public Health health() {
try {
restTemplate.getForEntity("https://external-service.com/health", String.class);
return Health.up().build();
} catch (Exception e) {
return Health.down(e).withDetail("Error", "External service is down").build();
}
}
}

5. Architecture Diagrams​

5.1 Component Diagram​

+-------------------+ +-------------------+ +-------------------+
| Spring Boot App | | Spring Actuator | | Monitoring System |
+-------------------+ +-------------------+ +-------------------+
| | |
| (1) Health Check | |
|--------------------------->| |
| | (2) Metrics |
| |-------------------------->|
| | |
| (3) Info Endpoint | |
|<---------------------------| |
| | |
+-------------------+ +-------------------+ +-------------------+
| Spring Boot App | | Spring Actuator | | Monitoring System |
+-------------------+ +-------------------+ +-------------------+

5.2 Sequence Diagram​

+-------------------+ +-------------------+ +-------------------+
| Client | | Spring Boot App | | Spring Actuator |
+-------------------+ +-------------------+ +-------------------+
| | |
| (1) Health Check Request | |
|--------------------------->| |
| | (2) Perform Health Check |
| |-------------------------->|
| | |
| (3) Health Check Response | |
|<---------------------------| |
| | |
+-------------------+ +-------------------+ +-------------------+
| Client | | Spring Boot App | | Spring Actuator |
+-------------------+ +-------------------+ +-------------------+

6. Sample Code and Configuration​

6.1 Dependency Configuration​

Add the necessary dependencies to your pom.xml file.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>

6.2 Actuator Configuration​

Configure Actuator endpoints in your application.yml file.

management:
endpoints:
web:
exposure:
include: health, metrics, info
metrics:
export:
prometheus:
enabled: true
server:
shutdown: graceful

6.3 Custom Health Indicator Implementation​

Implement a custom health indicator to monitor a specific component.

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {

@Override
public Health health() {
// Perform custom health check
boolean isHealthy = checkCustomComponent();
if (isHealthy) {
return Health.up().build();
} else {
return Health.down().withDetail("Error", "Custom component is down").build();
}
}

private boolean checkCustomComponent() {
// Custom health check logic
return true;
}
}

7. Key Takeaways​

  • Granular Health Checks: Implement separate health indicators for databases, message brokers, and other critical components.
  • Monitoring Integration: Integrate with monitoring tools like Prometheus and Grafana for visualizing metrics and health data.
  • Security Measures: Ensure only trusted sources can access sensitive health information.
  • Detailed Health Information: Enable detailed health information only for internal access to protect sensitive data.
  • Cluster-Aware Checks: In distributed environments, ensure health checks are cluster-aware and provide insights into individual instance health.

Conclusion​

Spring Actuator is a powerful tool for monitoring and managing Spring Boot applications. By following best practices such as configuring and securing Actuator endpoints, implementing custom health indicators, and enabling monitoring and metrics, you can ensure the reliability and security of your application. This article provided a comprehensive guide to best practices for Spring application health checks and Actuator, including architecture diagrams, sample code, and configuration details.

References​

Spark in Spring Boot Application

· 阅读需 11 分钟

Introduction​

This article provides a comprehensive guide to developing a Spark application with Spring Boot 3.3.5 to communicate with Kafka and Elasticsearch. The application will handle Avro records in Kafka, JSON data in Elasticsearch, and Parquet files in MinIO using Spark DataFrame and Spark SQL. The article covers the setup, integration, and advanced scenarios for handling large-scale data processing, including the use of Avro Schema Registry when communicating with Kafka.


Prerequisites​

  • Java 17 or later
  • Apache Spark 3.5.4
  • Spring Boot 3.3.5
  • Kafka 3.x
  • Elasticsearch 8.x
  • MinIO
  • Maven or Gradle
  • Confluent Schema Registry

Project Folder Structure​

spark-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── config/
│ │ │ │ │ │ ├── SparkConfig.java
│ │ │ │ │ ├── service/
│ │ │ │ │ │ ├── KafkaService.java
│ │ │ │ │ │ ├── ElasticsearchService.java
│ │ │ │ │ │ ├── MinIOService.java
│ │ │ │ │ │ ├── DataFrameService.java
│ │ │ │ │ ├── SparkSpringBootConsoleApplication.java
│ │ ├── resources/
│ │ │ ├── application.properties
│ ├── test/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── service/
│ │ │ │ │ │ ├── KafkaServiceTest.java
│ │ │ │ │ │ ├── ElasticsearchServiceTest.java
│ │ │ │ │ │ ├── MinIOServiceTest.java
│ │ │ │ │ │ ├── DataFrameServiceTest.java
├── pom.xml (or build.gradle)

Setting Up the Spring Boot Application​

Step 1: Create a Spring Boot Project​

Create a new Spring Boot project using Spring Initializr or your preferred IDE. Include the following dependencies:

  • Spring for Apache Kafka
  • Spring Data Elasticsearch
  • Spring for Apache Spark

Step 2: Configure Application Properties​

Configure the application properties for Kafka, Elasticsearch, and MinIO:

# Kafka Configuration
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=my-group
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.ByteArrayDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.ByteArraySerializer

# Schema Registry Configuration
schema.registry.url=http://localhost:8081

# Elasticsearch Configuration
spring.elasticsearch.rest.uris=http://localhost:9200

# MinIO Configuration
minio.endpoint=http://localhost:9000
minio.accessKey=your-access-key
minio.secretKey=your-secret-key
minio.bucket=your-bucket

Integrating Spark with Spring Boot​

Step 1: Add Spark Dependencies​

Add the following dependencies to your pom.xml or build.gradle file:

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-avro_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql-kafka-0-10_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>7.0.1</version>
</dependency>

Step 2: Configure Spark​

Configure Spark in your Spring Boot application using annotation-style configuration:

import org.apache.spark.sql.SparkSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SparkConfig {

@Bean
public SparkSession sparkSession() {
return SparkSession.builder()
.appName("SparkSpringBootApp")
.master("local[*]")
.config("spark.sql.shuffle.partitions", "10")
.getOrCreate();
}
}

Communicating with Kafka​

Reading Avro Records from Kafka​

To read Avro records from Kafka, use the spark-avro and spark-sql-kafka-0-10 libraries:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> readAvroFromKafka(String topic) {
return sparkSession.read()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("subscribe", topic)
.load()
.selectExpr("CAST(value AS BINARY)")
.select(org.apache.spark.sql.avro.functions.from_avro(col("value"), "your_avro_schema"));
}
}

Reading Avro Records from Kafka with Schema Registry​

To read Avro records from Kafka using the Schema Registry, use the spark-avro and spark-sql-kafka-0-10 libraries:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

@Value("${schema.registry.url}")
private String schemaRegistryUrl;

public Dataset<Row> readAvroFromKafka(String topic) {
return sparkSession.read()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("subscribe", topic)
.load()
.selectExpr("CAST(value AS BINARY)")
.select(org.apache.spark.sql.avro.functions.from_avro(col("value"), schemaRegistryUrl + "/subjects/" + topic + "-value/versions/latest"));
}
}

Writing Data to Kafka​

To write data to Kafka, use the spark-sql-kafka-0-10 library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

public void writeToKafka(Dataset<Row> dataset, String topic) {
dataset.selectExpr("to_json(struct(*)) AS value")
.write()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("topic", topic)
.save();
}
}

Writing Avro Records to Kafka with Schema Registry​

To write Avro records to Kafka using the Schema Registry, use the spark-avro and spark-sql-kafka-0-10 libraries:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

@Value("${schema.registry.url}")
private String schemaRegistryUrl;

public void writeAvroToKafka(Dataset<Row> dataset, String topic) {
dataset.select(org.apache.spark.sql.avro.functions.to_avro(struct("*")).as("value"))
.write()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("topic", topic)
.option("schema.registry.url", schemaRegistryUrl)
.save();
}
}

Communicating with Elasticsearch​

Reading JSON Data from Elasticsearch​

To read JSON data from Elasticsearch, use the elasticsearch-hadoop library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ElasticsearchService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> readFromElasticsearch(String index) {
return sparkSession.read()
.format("org.elasticsearch.spark.sql")
.option("es.nodes", "localhost")
.option("es.port", "9200")
.option("es.resource", index)
.load();
}
}

Writing Data to Elasticsearch​

To write data to Elasticsearch, use the elasticsearch-hadoop library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ElasticsearchService {

@Autowired
private SparkSession sparkSession;

public void writeToElasticsearch(Dataset<Row> dataset, String index) {
dataset.write()
.format("org.elasticsearch.spark.sql")
.option("es.nodes", "localhost")
.option("es.port", "9200")
.option("es.resource", index)
.save();
}
}

Handling Parquet Files in MinIO​

Reading Parquet Files from MinIO​

To read Parquet files from MinIO, use the spark-hadoop-cloud library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MinIOService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> readParquetFromMinIO(String bucket, String path) {
return sparkSession.read()
.format("parquet")
.option("spark.hadoop.fs.s3a.endpoint", "http://localhost:9000")
.option("spark.hadoop.fs.s3a.access.key", "your-access-key")
.option("spark.hadoop.fs.s3a.secret.key", "your-secret-key")
.option("spark.hadoop.fs.s3a.path.style.access", "true")
.load("s3a://" + bucket + "/" + path);
}
}

Writing Parquet Files to MinIO​

To write Parquet files to MinIO, use the spark-hadoop-cloud library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MinIOService {

@Autowired
private SparkSession sparkSession;

public void writeParquetToMinIO(Dataset<Row> dataset, String bucket, String path) {
dataset.write()
.format("parquet")
.option("spark.hadoop.fs.s3a.endpoint", "http://localhost:9000")
.option("spark.hadoop.fs.s3a.access.key", "your-access-key")
.option("spark.hadoop.fs.s3a.secret.key", "your-secret-key")
.option("spark.hadoop.fs.s3a.path.style.access", "true")
.save("s3a://" + bucket + "/" + path);
}
}

Using Spark DataFrame and Spark SQL​

Creating DataFrames​

To create DataFrames, use the SparkSession object:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataFrameService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> createDataFrame() {
return sparkSession.read()
.format("csv")
.option("header", "true")
.load("path/to/csv/file");
}
}

Querying DataFrames with Spark SQL​

To query DataFrames using Spark SQL, register the DataFrame as a temporary view:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataFrameService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> queryDataFrame(Dataset<Row> df) {
df.createOrReplaceTempView("my_table");
return sparkSession.sql("SELECT * FROM my_table WHERE column = 'value'");
}
}

Unit Test Cases​

KafkaServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class KafkaServiceTest {

@Autowired
private KafkaService kafkaService;

@Autowired
private SparkSession sparkSession;

@Test
public void testReadAvroFromKafka() {
Dataset<Row> dataset = kafkaService.readAvroFromKafka("test-topic");
assertNotNull(dataset);
}

@Test
public void testWriteAvroToKafka() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
kafkaService.writeAvroToKafka(dataset, "test-topic");
}
}

ElasticsearchServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class ElasticsearchServiceTest {

@Autowired
private ElasticsearchService elasticsearchService;

@Autowired
private SparkSession sparkSession;

@Test
public void testReadFromElasticsearch() {
Dataset<Row> dataset = elasticsearchService.readFromElasticsearch("test-index");
assertNotNull(dataset);
}

@Test
public void testWriteToElasticsearch() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
elasticsearchService.writeToElasticsearch(dataset, "test-index");
}
}

MinIOServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class MinIOServiceTest {

@Autowired
private MinIOService minIOService;

@Autowired
private SparkSession sparkSession;

@Test
public void testReadParquetFromMinIO() {
Dataset<Row> dataset = minIOService.readParquetFromMinIO("test-bucket", "test-path");
assertNotNull(dataset);
}

@Test
public void testWriteParquetToMinIO() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
minIOService.writeParquetToMinIO(dataset, "test-bucket", "test-path");
}
}

DataFrameServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class DataFrameServiceTest {

@Autowired
private DataFrameService dataFrameService;

@Autowired
private SparkSession sparkSession;

@Test
public void testCreateDataFrame() {
Dataset<Row> dataset = dataFrameService.createDataFrame();
assertNotNull(dataset);
}

@Test
public void testQueryDataFrame() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
Dataset<Row> result = dataFrameService.queryDataFrame(dataset);
assertNotNull(result);
}
}

Design Diagrams​

Component Diagram​

+-------------------+ +-------------------+ +-------------------+
| | | | | |
| Spring Boot | | Apache Spark | | Kafka |
| | | | | |
+--------+----------+ +--------+----------+ +--------+----------+
| | |
| | |
v v v
+-------------------+ +-------------------+ +-------------------+
| | | | | |
| KafkaService | | Elasticsearch | | MinIOService |
| | | | | |
+--------+----------+ +--------+----------+ +--------+----------+
| | |
| | |
v v v
+--------------------+ +-------------------+ +---------------------------------------+
| | | | | |
| DataFrameService | | SparkConfig | | SparkSpringBootConsoleApplication |
| | | | | |
+--------------------+ +-------------------+ +---------------------------------------+

Sequence Diagram​

+---------+ +---------+ +---------+ +---------+
| | | | | | | |
| Client | | Spring | | Spark | | Kafka |
| | | Boot | | | | |
+---------+ +---------+ +---------+ +---------+
| | | |
| Request | | |
|----------------->| | |
| | | |
| | Read Avro | |
| | from Kafka | |
| |-------------->| |
| | | |
| | | Send Avro |
| | |<------------->|
| | | |
| | Process | |
| | Data | |
| |-------------->| |
| | | |
| | Write to | |
| | Elasticsearch| |
| |-------------->| |
| | | |
| | Write to | |
| | MinIO | |
| |-------------->| |
| | | |
| Response | | |
|<-----------------| | |
| | | |

Advanced Scenarios​

Complex Scenario 1: Flow Control and Backpressure Handling​

In scenarios where your application needs to handle flow control and backpressure, you can use Spark's built-in mechanisms for handling large-scale data processing.

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FlowControlService {

@Autowired
private SparkSession sparkSession;

public void handleFlowControl(Dataset<Row> dataset) {
dataset.writeStream()
.format("console")
.option("checkpointLocation", "path/to/checkpoint")
.start()
.awaitTermination();
}
}

Complex Scenario 2: Handling Large-Scale Data Processing​

For large-scale data processing, use Spark's distributed processing capabilities and optimize resource usage.

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LargeScaleProcessingService {

@Autowired
private SparkSession sparkSession;

public void processLargeScaleData(Dataset<Row> dataset) {
dataset.repartition(100)
.write()
.format("parquet")
.save("path/to/output");
}
}

Conclusion​

Developing a Spark application with Spring Boot 3.3.5 to communicate with Kafka and Elasticsearch involves setting up the Spring Boot application, integrating Spark, and handling data using Spark DataFrame and Spark SQL. By following the best practices outlined in this article, you can create efficient and scalable data processing applications.


References​

  1. Spring Boot Documentation
  2. Apache Spark Documentation
  3. Apache Kafka Documentation
  4. Elasticsearch Documentation
  5. MinIO Documentation
  6. Confluent Schema Registry Documentation

By following these best practices, you can leverage Spark and Spring Boot to build powerful and scalable data processing applications.

Deep Dive into Spring Boot Multi-Threading

· 阅读需 7 分钟

Introduction to Spring Boot Multi-Threading

Spring Boot provides robust support for multi-threading, enabling developers to build highly concurrent and scalable applications. Leveraging multi-threading in Spring Boot can significantly improve the performance and responsiveness of your application. This article delves into best practices, annotation-based configuration, and real-life production scenarios to help you master multi-threading in Spring Boot.


Best Practices for Spring Boot Multi-Threading

  1. Use @Async for Asynchronous Processing: The @Async annotation allows methods to run in a separate thread, making it easy to implement asynchronous processing.
  2. Configure Thread Pools: Use TaskExecutor to configure thread pools with specific characteristics like core pool size, max pool size, and queue capacity.
  3. Handle Exceptions Gracefully: Implement exception handling mechanisms to manage exceptions that occur in asynchronous tasks.
  4. Monitor Thread Pools: Use Spring Boot Actuator to monitor thread pool metrics and ensure optimal performance.
  5. Avoid Blocking Operations: Ensure that asynchronous methods do not perform blocking operations, which can degrade performance.

Annotation-Based Configuration

Spring Boot provides several annotations to configure and manage multi-threading:

  • @EnableAsync: Enables asynchronous processing in the application.
  • @Async: Marks a method as asynchronous, meaning it will run in a separate thread.
  • @Configuration: Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions.
  • @Bean: Indicates that a method produces a bean to be managed by the Spring container.

Diagram of Multi-Threading in Spring Boot

+-------------------------+
| Spring Boot Application |
| - @EnableAsync |
| - @Configuration |
| - @Bean |
+-------------------------+
|
v
+-------------------+
| TaskExecutor |
| - corePoolSize |
| - maxPoolSize |
| - queueCapacity |
+-------------------+
|
v
+-------------------+
| @Async Methods |
| - Method1 |
| - Method2 |
+-------------------+
|
v
+-------------------+
| Thread Pool |
| - Thread 1 |
| - Thread 2 |
| - Thread 3 |
+-------------------+
|
v
+-------------------+
| Task Execution |
| - Task 1 |
| - Task 2 |
| - Task 3 |
+-------------------+

Sample Code with Detailed Comments

Configuration Class​

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync // Enable asynchronous processing
public class AsyncConfig {

@Bean(name = "taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10); // Core pool size
executor.setMaxPoolSize(20); // Maximum pool size
executor.setQueueCapacity(100); // Queue capacity
executor.setThreadNamePrefix("AsyncThread-"); // Thread name prefix
executor.initialize();
return executor;
}
}

Service Class​

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {

@Async("taskExecutor") // Use the taskExecutor bean for this method
public void performAsyncTask(String taskName) {
System.out.println("Starting task: " + taskName + " on thread: " + Thread.currentThread().getName());
try {
// Simulate task execution time
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Completed task: " + taskName + " on thread: " + Thread.currentThread().getName());
}
}

Controller Class​

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AsyncController {

@Autowired
private AsyncService asyncService;

@GetMapping("/performTask")
public String performTask(@RequestParam String taskName) {
asyncService.performAsyncTask(taskName);
return "Task " + taskName + " has been submitted for asynchronous processing.";
}
}

Application Class​

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync // Enable asynchronous processing
public class AsyncApplication {

public static void main(String[] args) {
SpringApplication.run(AsyncApplication.class, args);
}
}

Complex Scenario 1: Flow Control and Backpressure Handling

In high-throughput systems, it's crucial to manage the flow of tasks to avoid overwhelming the system. Backpressure handling ensures that the system can gracefully handle a large number of incoming tasks.

Configuration Class​

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class BackpressureConfig {

@Bean(name = "backpressureExecutor")
public Executor backpressureExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5); // Core pool size
executor.setMaxPoolSize(10); // Maximum pool size
executor.setQueueCapacity(50); // Queue capacity
executor.setThreadNamePrefix("BackpressureThread-"); // Thread name prefix
executor.initialize();
return executor;
}
}

Service Class​

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class BackpressureService {

@Async("backpressureExecutor")
public void handleTask(String taskName) {
System.out.println("Handling task: " + taskName + " on thread: " + Thread.currentThread().getName());
try {
// Simulate task execution time
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Completed handling task: " + taskName + " on thread: " + Thread.currentThread().getName());
}
}

Controller Class​

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BackpressureController {

@Autowired
private BackpressureService backpressureService;

@GetMapping("/handleTask")
public String handleTask(@RequestParam String taskName) {
backpressureService.handleTask(taskName);
return "Task " + taskName + " has been submitted for backpressure handling.";
}
}

Complex Scenario 2: Task Prioritization

In some scenarios, certain tasks may need to be prioritized over others. Spring Boot allows you to configure task executors with priority queues to handle such cases.

Configuration Class​

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.PriorityBlockingQueue;

@Configuration
@EnableAsync
public class PriorityConfig {

@Bean(name = "priorityExecutor")
public Executor priorityExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5); // Core pool size
executor.setMaxPoolSize(10); // Maximum pool size
executor.setQueueCapacity(50); // Queue capacity
executor.setThreadNamePrefix("PriorityThread-"); // Thread name prefix
executor.setTaskDecorator(new PriorityTaskDecorator()); // Set task decorator for priority
executor.initialize();
return executor;
}
}

Task Decorator​

import org.springframework.core.task.TaskDecorator;

public class PriorityTaskDecorator implements TaskDecorator {

@Override
public Runnable decorate(Runnable runnable) {
return () -> {
// Set priority logic here
System.out.println("Setting priority for task: " + runnable);
runnable.run();
};
}
}

Service Class​

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class PriorityService {

@Async("priorityExecutor")
public void prioritizeTask(String taskName) {
System.out.println("Prioritizing task: " + taskName + " on thread: " + Thread.currentThread().getName());
try {
// Simulate task execution time
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Completed prioritizing task: " + taskName + " on thread: " + Thread.currentThread().getName());
}
}

Controller Class​

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PriorityController {

@Autowired
private PriorityService priorityService;

@GetMapping("/prioritizeTask")
public String prioritizeTask(@RequestParam String taskName) {
priorityService.prioritizeTask(taskName);
return "Task " + taskName + " has been submitted for prioritization.";
}
}

Complex Scenario 3: Handling Long-Running Tasks

Long-running tasks can block threads and degrade system performance. Spring Boot provides mechanisms to handle such tasks efficiently.

Configuration Class​

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class LongRunningConfig {

@Bean(name = "longRunningExecutor")
public Executor longRunningExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5); // Core pool size
executor.setMaxPoolSize(10); // Maximum pool size
executor.setQueueCapacity(50); // Queue capacity
executor.setThreadNamePrefix("LongRunningThread-"); // Thread name prefix
executor.initialize();
return executor;
}
}

Service Class​

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class LongRunningService {

@Async("longRunningExecutor")
public void handleLongRunningTask(String taskName) {
System.out.println("Handling long-running task: " + taskName + " on thread: " + Thread.currentThread().getName());
try {
// Simulate long-running task execution time
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Completed handling long-running task: " + taskName + " on thread: " + Thread.currentThread().getName());
}
}

Controller Class​

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LongRunningController {

@Autowired
private LongRunningService longRunningService;

@GetMapping("/handleLongRunningTask")
public String handleLongRunningTask(@RequestParam String taskName) {
longRunningService.handleLongRunningTask(taskName);
return "Long-running task " + taskName + " has been submitted for processing.";
}
}

Conclusion

Spring Boot's support for multi-threading allows developers to build highly concurrent and scalable applications. By following best practices and leveraging annotation-based configuration, you can efficiently manage asynchronous tasks, handle backpressure, prioritize tasks, and manage long-running tasks. This article provides a comprehensive guide to mastering multi-threading in Spring Boot, complete with detailed sample code and real-life production scenarios.

Deep Dive into Spring Scheduler

· 阅读需 9 分钟

Introduction​

Spring Scheduler provides a simple way to schedule tasks within a Spring application. It offers flexibility in managing time-based events and task execution, allowing developers to configure tasks in a variety of ways, including sequential, parallel, and conditional execution.


Understanding Spring Scheduler Basics​

Spring Scheduler uses annotations to simplify the scheduling of methods within beans. The primary annotation used is @Scheduled, which can define various timing configurations, such as fixed rate, fixed delay, and cron expressions.

Enabling Scheduling​

Before defining tasks, enable scheduling in your Spring application by adding the @EnableScheduling annotation.

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class SchedulingConfig {
// Any other scheduling-related configuration if needed
}

Simple Scheduling with @Scheduled​

The @Scheduled annotation can be applied to any method to enable scheduling. Below is a simple example:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SimpleScheduler {

@Scheduled(fixedRate = 5000)
public void printMessage() {
System.out.println("This message prints every 5 seconds.");
}
}

In this example, printMessage() runs every 5 seconds.


Scheduling Scenarios​

Spring Scheduler supports various scenarios for task scheduling, which can be useful for specific use cases in a production environment.

Sequential Job Execution​

In sequential execution, jobs run in a specific order, with each job waiting for the previous one to complete before starting.

Example: Sequential Scheduling Using Fixed Delay

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SequentialScheduler {

@Scheduled(fixedDelay = 10000)
public void job1() {
System.out.println("Job 1 started.");
// simulate a delay
try { Thread.sleep(2000); } catch (InterruptedException ignored) {}
System.out.println("Job 1 completed.");
}

@Scheduled(fixedDelay = 10000)
public void job2() {
System.out.println("Job 2 started after Job 1.");
// simulate a delay
try { Thread.sleep(3000); } catch (InterruptedException ignored) {}
System.out.println("Job 2 completed.");
}
}

With fixedDelay, Job 2 only begins after Job 1 completes.

Parallel Job Execution​

To execute jobs in parallel, we configure a task executor that runs multiple tasks concurrently.

Example: Parallel Scheduling Using a Custom Task Executor

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class ParallelSchedulingConfig {

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2); // Number of concurrent tasks allowed
executor.setMaxPoolSize(5);
executor.initialize();
return executor;
}
}

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ParallelScheduler {

@Async
@Scheduled(fixedRate = 10000)
public void jobA() {
System.out.println("Parallel Job A started.");
}

@Async
@Scheduled(fixedRate = 10000)
public void jobB() {
System.out.println("Parallel Job B started concurrently with Job A.");
}
}

With @Async, jobA and jobB run concurrently, allowing for parallel execution.

Job Waiting Until Previous Job Completes​

To configure jobs to wait for previous jobs to finish, use fixedDelay with asynchronous methods to ensure that each task waits until its predecessor is completed.

Diagram: Job Waiting until Previous Job Completes

Job 1 ---> Wait ---> Job 2 ---> Wait ---> Job 3
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class WaitingScheduler {

@Scheduled(fixedDelay = 12000)
public void job() {
System.out.println("Job started and waits until the previous instance completes.");
// Simulate processing time
try { Thread.sleep(5000); } catch (InterruptedException ignored) {}
System.out.println("Job completed.");
}
}

In this scenario, each new execution waits until the previous one finishes.


Cron Expressions and Fixed Rates​

Spring Scheduler allows defining precise timing patterns using fixed rate, fixed delay, and cron expressions.

Fixed Rate vs Fixed Delay​

  • Fixed Rate: Executes a task at a specified interval, regardless of task completion time.
  • Fixed Delay: Executes a task with a delay between the end of the previous execution and the start of the next.
@Scheduled(fixedRate = 60000) // Executes every 60 seconds
public void fixedRateTask() {
System.out.println("Fixed Rate Task");
}

@Scheduled(fixedDelay = 60000) // Executes with a 60-second delay after completion
public void fixedDelayTask() {
System.out.println("Fixed Delay Task");
}

Using Cron Expressions​

Cron expressions allow precise scheduling based on time. Format: second minute hour day month weekday.

Example: Scheduling a Task for 5:00 AM Every Day

@Scheduled(cron = "0 0 5 * * ?")
public void dailyTask() {
System.out.println("This task runs at 5:00 AM every day.");
}

Conditional Scheduling with Annotations​

Spring allows scheduling tasks based on conditions. Combining @ConditionalOnProperty with @Scheduled, we can configure tasks to run only when certain conditions are met.

Conditional Scheduling Example​

This example schedules a job only if a specific property is set to true.

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true")
public class ConditionalScheduler {

@Scheduled(fixedRate = 10000)
public void conditionalTask() {
System.out.println("This task runs only if 'scheduler.enabled=true'.");
}
}

To enable or disable this task, add scheduler.enabled=true or false in the configuration.


All-In-One Sample​

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableScheduling
public class ProductionSchedulerConfig {

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(5);
executor.initialize();
return executor;
}
}

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true", matchIfMissing = true)
public class ProductionScheduler {

@Async
@Scheduled(cron = "0 0 12 * * ?") // Executes at noon daily
public void dailySummaryJob() {


System.out.println("Executing daily summary job...");
}

@Async
@Scheduled(fixedDelay = 15000) // Executes every 15 seconds after the previous execution ends
public void periodicCleanupJob() {
System.out.println("Running periodic cleanup job...");
}
}

ConditionalOnProperty with Scheduled Job in Depth​

The @ConditionalOnProperty annotation in Spring is typically used to conditionally enable or disable beans based on specific configuration properties. In this case:

@ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true")

This will activate the annotated bean only if the scheduler.enabled property is set to true in your configuration (like in application.properties or environment variables).

Scenario: Running a Scheduled Job in Only One Pod in a Kubernetes Cluster​

When deploying a Spring application in a Kubernetes cluster across multiple containers (pods), ensuring that a scheduled job only runs in one specific pod can be tricky because Spring's @Scheduled jobs will run in every instance where the job bean is active. Here’s how to solve this problem by using the @ConditionalOnProperty annotation in combination with Kubernetes configurations.

Solution Outline​

  1. Set a Leader Pod: Configure only one of the pods to have scheduler.enabled=true using Kubernetes annotations and environment variables, making it the leader pod responsible for running the scheduled job.
  2. Configure Pods with Unique Environment Variables: Use Kubernetes configMaps or Secrets to assign the scheduler.enabled property as true only in the leader pod’s environment. Other pods should not have this property set to true, so they won’t run the job.

Steps to Implement​

  1. Create a ConfigMap or Secret for the Scheduler Property:

    • Define a ConfigMap that holds the scheduler.enabled setting for the lead pod.
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: scheduler-config
    data:
    scheduler.enabled: "true"
  2. Use Pod Annotations to Select the Leader Pod:

    • When deploying multiple replicas, use a label selector or custom script to ensure only one pod is given the scheduler.enabled=true setting.
  3. Configure the Deployment to Pass the scheduler.enabled Environment Variable:

    • In your Kubernetes Deployment file, you can use envFrom to load the environment variables from the ConfigMap, but with a selector so only one pod uses this ConfigMap.
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: scheduler-job
    spec:
    replicas: 3 # Running multiple instances
    selector:
    matchLabels:
    app: scheduler-app
    template:
    metadata:
    labels:
    app: scheduler-app
    spec:
    containers:
    - name: scheduler-container
    image: your-image
    envFrom:
    - configMapRef:
    name: scheduler-config
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
  4. Conditional Logic in Spring Boot:

    • With @ConditionalOnProperty, the @Scheduled method will only activate if the scheduler.enabled property is true. So, only the pod with the scheduler.enabled=true environment variable will run the job.
    @Component
    @ConditionalOnProperty(name = "scheduler.enabled", havingValue = "true")
    public class ScheduledJob {

    @Scheduled(fixedRate = 5000)
    public void performTask() {
    System.out.println("Scheduled job is running on the leader pod!");
    }
    }

Additional Consideration: Use Leader Election​

For robust setups, you could implement leader election to dynamically select the leader pod. This can be achieved using tools like Spring Cloud Kubernetes Leader Election or implementing a custom leader election mechanism based on a shared lock in an external data store, such as Redis or ZooKeeper.

By setting scheduler.enabled=true conditionally based on which pod is the elected leader, you can ensure the scheduled job runs in only one container at a time, with the ability to switch seamlessly if the leader pod goes down.

Conclusion​

Spring Scheduler provides flexibility and control over task scheduling. By using @Scheduled with different configurations, we can create complex scheduling workflows that address specific business needs.

Deep Dive Redis Integration With Spring Boot Application

· 阅读需 12 分钟

Table of Contents​


Introduction​

In distributed systems, caching is a critical mechanism to improve performance by reducing the load on backend services and databases. This article delves into the design and implementation of cache mechanisms for distributed systems, covering high-level architecture, detailed designs, and sample code for various scenarios.


High-Level Architecture Design​

Diagram​

graph TD
A[Client] --> B[Load Balancer]
B --> C[Service Layer]
C --> D[Cache Layer]
C --> E[Data Storage Layer]
D --> E
F[Monitoring and Metrics] --> C
F --> D
F --> E

Explanation​

  1. Client: The client sends requests to the distributed system.
  2. Load Balancer: Distributes incoming requests across multiple instances of the service layer.
  3. Service Layer: Handles business logic and interacts with the cache and data storage layers.
  4. Cache Layer: Stores frequently accessed data to reduce the load on the data storage layer.
  5. Data Storage Layer: The primary data store (e.g., database) where data is persisted.
  6. Monitoring and Metrics: Collects and monitors performance metrics for the entire system.

Detail Designs​

Cache Layer​

The cache layer is responsible for storing frequently accessed data to reduce the load on the data storage layer. It can be implemented using distributed caching solutions like Redis or Hazelcast.

Redis Setup and Integration​

Redis is an in-memory data structure store that can be used as a distributed cache. It supports various data structures like strings, hashes, lists, sets, and sorted sets.

Step 1: Install Redis​

You can install Redis on your local machine or use a cloud-based Redis service like AWS ElastiCache or Redis Labs.

For local installation on Linux:

sudo apt-get update
sudo apt-get install redis-server

For local installation on macOS:

brew install redis
Step 2: Start Redis Server​

Start the Redis server:

redis-server
Step 3: Verify Redis Installation​

Verify that Redis is running:

redis-cli ping

If Redis is running correctly, you should see PONG as the response.

Redis Configuration​

Redis configuration can be managed through the redis.conf file. Common configurations include setting the maximum memory limit, configuring persistence, and setting up replication.

Example redis.conf snippet:

maxmemory 256mb
maxmemory-policy allkeys-lru

Redis Integration with Spring Boot​

To integrate Redis with a Spring Boot application, you need to add the necessary dependencies and configure the Redis connection.

Step 1: Add Dependencies​

Add the following dependencies to your pom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Step 2: Configure Redis Connection​

Configure the Redis connection in your application.yml:

spring:
redis:
host: localhost
port: 6379
cache:
type: redis
Step 3: Enable Caching​

Enable caching in your Spring Boot application by adding the @EnableCaching annotation to your main application class:

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

@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Step 4: Use Redis Cache​

Use the @Cacheable annotation to cache method results in Redis:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class CacheService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from data storage layer
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}
}

Best Practices for Redis in Kubernetes​

When deploying Redis in Kubernetes, it's important to follow best practices to ensure high availability, scalability, and security.

Step 1: Deploy Redis in Kubernetes​

You can deploy Redis in Kubernetes using a StatefulSet for stateful applications or a Deployment for stateless applications. Here's an example of deploying Redis using a StatefulSet:

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
volumeMounts:
- name: redis-data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
Step 2: Create a Headless Service​

Create a headless service to manage the Redis StatefulSet:

apiVersion: v1
kind: Service
metadata:
name: redis
spec:
clusterIP: None
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
Step 3: Configure Redis Access in Spring Boot​

When deploying your Spring Boot application in Kubernetes, you should use the service name to access Redis. Update your application.yml to use the service name:

spring:
redis:
host: redis
port: 6379
cache:
type: redis
Step 4: Enable Redis Persistence​

To enable Redis persistence, configure the redis.conf file to use either RDB (Redis Database Backup) or AOF (Append-Only File) persistence:

save 900 1
save 300 10
save 60 10000
appendonly yes
Step 5: Secure Redis​

Secure Redis by setting a password and enabling TLS:

requirepass yourpassword
tls-port 6379
tls-cert-file /path/to/redis.crt
tls-key-file /path/to/redis.key
tls-ca-cert-file /path/to/ca.crt
Step 6: Monitor Redis​

Use Prometheus and Grafana to monitor Redis performance and health. Deploy a Redis exporter to expose Redis metrics:

apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-exporter
spec:
replicas: 1
selector:
matchLabels:
app: redis-exporter
template:
metadata:
labels:
app: redis-exporter
spec:
containers:
- name: redis-exporter
image: oliver006/redis_exporter:latest
args:
- --redis.addr=redis:6379
- --redis.password=yourpassword
ports:
- containerPort: 9121

Create a service to expose the Redis exporter:

apiVersion: v1
kind: Service
metadata:
name: redis-exporter
spec:
selector:
app: redis-exporter
ports:
- port: 9121
targetPort: 9121

Data Storage Layer​

The data storage layer is the primary data store where data is persisted. It can be a relational database, NoSQL database, or any other storage solution.

Diagram​

graph TD
A[Service Layer] --> B[Data Storage Layer]
B --> C[Relational Database]
B --> D[NoSQL Database]

Sample Code​

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {

public User findById(Long userId) {
// Database query logic
return new User();
}

public void save(User user) {
// Database save logic
}
}

Service Layer​

The service layer handles business logic and interacts with the cache and data storage layers. It is responsible for managing the flow of data between the client, cache, and data storage.

Diagram​

graph TD
A[Client] --> B[Service Layer]
B --> C[Cache Layer]
B --> D[Data Storage Layer]

Sample Code​

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Autowired
private CacheService cacheService;

@Autowired
private UserRepository userRepository;

public User getUserById(Long userId) {
User user = cacheService.getUserById(userId);
if (user == null) {
user = userRepository.findById(userId);
cacheService.saveUser(user);
}
return user;
}

public void saveUser(User user) {
userRepository.save(user);
cacheService.saveUser(user);
}
}

Load Balancer​

The load balancer distributes incoming requests across multiple instances of the service layer to ensure high availability and scalability.

Diagram​

graph TD
A[Client] --> B[Load Balancer]
B --> C[Service Instance 1]
B --> D[Service Instance 2]
B --> E[Service Instance 3]

Sample Code​

Load balancers are typically implemented using infrastructure components like NGINX, HAProxy, or cloud-provided load balancers (e.g., AWS ELB, Google Cloud Load Balancer).

Monitoring and Metrics​

Monitoring and metrics collection are essential for tracking the performance and health of the distributed system. Tools like Prometheus, Grafana, and ELK stack can be used for this purpose.

Diagram​

graph TD
A[Service Layer] --> B[Monitoring and Metrics]
B --> C[Prometheus]
B --> D[Grafana]
B --> E[ELK Stack]

Sample Code​

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;

public class MetricsCollector {

private static final Counter cacheHits = Counter.build()
.name("cache_hits_total")
.help("Total number of cache hits.")
.register();

private static final Counter cacheMisses = Counter.build()
.name("cache_misses_total")
.help("Total number of cache misses.")
.register();

private static final Gauge cacheSize = Gauge.build()
.name("cache_size")
.help("Current size of the cache.")
.register();

public static void recordCacheHit() {
cacheHits.inc();
}

public static void recordCacheMiss() {
cacheMisses.inc();
}

public static void setCacheSize(long size) {
cacheSize.set(size);
}
}

Complex Scenario 1: Flow Control and Backpressure Handling​

Problem Statement​

In high-traffic scenarios, the cache might become a bottleneck if not properly managed. Flow control and backpressure handling are essential to prevent the cache from being overwhelmed.

Solution Architecture​

  1. Rate Limiting: Implement rate limiting to control the number of requests hitting the cache.
  2. Circuit Breaker: Use a circuit breaker to handle failures gracefully and prevent cascading failures.
  3. Backpressure Handling: Implement backpressure mechanisms to manage the flow of data between services.

Flow Chart​

graph TD
A[Client Request] --> B{Rate Limiter}
B -->|Allowed| C[Cache Lookup]
B -->|Denied| D[Fallback to Database]
C --> E{Cache Hit?}
E -->|Yes| F[Return Cached Data]
E -->|No| G[Fetch from Database]
G --> H[Update Cache]
H --> F
D --> G

Sample Code​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from database
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}
}

Complex Scenario 2: Cache Invalidation and Data Synchronization​

Problem Statement​

Ensuring that the cache remains consistent with the underlying data store is challenging, especially in distributed systems.

Solution Architecture​

  1. Event-Driven Invalidation: Use event-driven mechanisms to invalidate the cache when data changes.
  2. Distributed Locking: Use distributed locking to ensure that only one service updates the cache at a time.
  3. TTL-Based Invalidation: Use Time-to-Live (TTL) to automatically invalidate cache entries after a certain period.

Sequence Diagram​

sequenceDiagram
participant Client
participant Service
participant Cache
participant Database

Client->>Service: Update User
Service->>Database: Update User in Database
Database-->>Service: User Updated
Service->>Cache: Invalidate User Cache
Cache-->>Service: Cache Invalidated
Service-->>Client: User Updated

Sample Code​

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@CacheEvict(value = "userCache", key = "#userId")
public void updateUser(Long userId, User user) {
// Update user in database
updateUserInDatabase(userId, user);
}

private void updateUserInDatabase(Long userId, User user) {
// Database update logic
}
}

Complex Scenario 3: Cache Stampede Prevention​

Problem Statement​

A cache stampede occurs when multiple requests for the same data hit the cache simultaneously, leading to a spike in database load.

Solution Architecture​

  1. Probabilistic Early Expiration: Use probabilistic early expiration to spread out cache expiration times.
  2. Locking Mechanism: Implement a locking mechanism to ensure that only one request populates the cache at a time.
  3. Background Refresh: Use background refresh to keep the cache up-to-date without impacting the user experience.

Flow Chart​

graph TD
A[Client Request] --> B{Cache Hit?}
B -->|Yes| C[Return Cached Data]
B -->|No| D{Lock Acquired?}
D -->|Yes| E[Fetch from Database]
D -->|No| F[Wait for Lock]
E --> G[Update Cache]
G --> C
F --> B

Sample Code​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class UserService {

private final Lock lock = new ReentrantLock();

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
try {
lock.lock();
// Double-check to ensure the cache is still empty
User cachedUser = fetchUserFromCache(userId);
if (cachedUser != null) {
return cachedUser;
}
// Fetch user from database
User user = fetchUserFromDatabase(userId);
// Update cache
updateCache(userId, user);
return user;
} finally {
lock.unlock();
}
}

private User fetchUserFromCache(Long userId) {
// Cache lookup logic
return null;
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}

private void updateCache(Long userId, User user) {
// Cache update logic
}
}

Conclusion​

Caching is a powerful mechanism for improving the performance and scalability of distributed systems. By understanding the high-level architecture and detailed designs, you can effectively implement cache mechanisms that handle various complex scenarios like flow control, cache invalidation, and cache stampede prevention. Use the provided sample code and diagrams to implement efficient caching in your distributed system.

Deep Dive Test Automation of Spring Boot Application

· 阅读需 8 分钟

Testing Spring Boot applications is crucial for validating functionality across the persistence, service, and controller layers, especially when deployed in containerized environments. This guide covers everything from configuring each layer’s tests to handling parallel endpoint testing and leveraging Docker for containerized environments.



1. Overview​

Testing a Spring Boot application ensures each layer functions as expected and that the system performs cohesively. Tests should cover:

  • Persistence Layer: Verifying data access and manipulation.
  • Service Layer: Ensuring business logic functions correctly.
  • Controller Layer: Testing API endpoints and interactions.

This article includes testing strategies for both development and containerized environments, plus configurations for parallel endpoint testing.


2. Testing the Persistence Layer​

The persistence layer involves data storage and access. To test without connecting to production databases, use embedded databases for unit testing and Testcontainers for more complex integration tests.

Embedded Database for Testing​

Embedded databases (like H2) are ideal for simple, fast tests:

  1. Add H2 dependency in pom.xml:

    <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
    </dependency>
  2. Application configuration for testing (src/test/resources/application-test.yml):

    spring:
    datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    username: sa
    password:
    jpa:
    hibernate:
    ddl-auto: create-drop
    show-sql: true
  3. Persistence Layer Test:

    @SpringBootTest
    @Transactional
    public class UserRepositoryTest {
    @Autowired
    private UserRepository userRepository;

    @Test
    public void testSaveUser() {
    User user = new User("John Doe", "john.doe@example.com");
    User savedUser = userRepository.save(user);
    assertNotNull(savedUser.getId());
    }
    }

Integration Testing with Testcontainers​

For more realistic integration tests with databases (e.g., PostgreSQL), use Testcontainers:

  1. Add Testcontainers dependency:

    <dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>postgresql</artifactId>
    <version>1.16.2</version>
    <scope>test</scope>
    </dependency>
  2. Set up Testcontainers for PostgreSQL:

    @SpringBootTest
    public class UserRepositoryTest {

    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13")
    .withDatabaseName("testdb")
    .withUsername("user")
    .withPassword("password");

    @DynamicPropertySource
    static void configureProperties(DynamicPropertyRegistry registry) {
    postgres.start();
    registry.add("spring.datasource.url", postgres::getJdbcUrl);
    registry.add("spring.datasource.username", postgres::getUsername);
    registry.add("spring.datasource.password", postgres::getPassword);
    }

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testFindUserById() {
    // Test logic
    }
    }

3. Testing the Service Layer​

The service layer holds business logic. Use mocks for isolated unit tests and real dependencies for integration tests.

Unit Tests with Mocks​

  1. Define a test with @MockBean:
    @ExtendWith(MockitoExtension.class)
    public class UserServiceTest {

    @Mock
    private UserRepository userRepository;

    @InjectMocks
    private UserService userService;

    @Test
    public void testCreateUser() {
    User user = new User("Alice", "alice@example.com");
    when(userRepository.save(any(User.class))).thenReturn(user);

    User savedUser = userService.createUser(user);
    assertEquals("Alice", savedUser.getName());
    }
    }

Integration Testing with Dependencies​

For full service layer integration:

@SpringBootTest
@Transactional
public class UserServiceIntegrationTest {

@Autowired
private UserService userService;

@Test
public void testFindAllUsers() {
List<User> users = userService.findAllUsers();
assertNotNull(users);
}
}

4. Testing the Controller Layer​

Controller tests validate API endpoints. MockMvc is used for isolated testing, while containerized setups allow testing real endpoints.

MockMvc for Isolated Controller Testing​

MockMvc allows API testing without starting a full web server.

@WebMvcTest(UserController.class)
public class UserControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private UserService userService;

@Test
public void testGetUser() throws Exception {
when(userService.getUserById(anyLong())).thenReturn(new User(1L, "Alice"));

mockMvc.perform(get("/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Alice"));
}
}

Testing Real Endpoints in a Container​

For real endpoint testing, use RestTemplate with a server running on a random port:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerIntegrationTest {

@LocalServerPort
private int port;

@Autowired
private TestRestTemplate restTemplate;

@Test
public void testGetUser() {
ResponseEntity<User> response = restTemplate.getForEntity("http://localhost:" + port + "/users/1", User.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
}

Parallel Testing for API Endpoints​

When testing multiple endpoints simultaneously, use RANDOM_PORT to prevent port conflicts.


5. End-to-End Testing in Containerized Environments​

Using Docker Compose, spin up services to perform full integration tests in a controlled environment.

Using Docker Compose for Integration Testing​

  1. Docker Compose File:

    version: '3'
    services:
    app:
    image: my-spring-app:latest
    ports:
    - "8080:8080"
    environment:
    - SPRING_PROFILES_ACTIVE=test
    postgres:
    image: postgres:13
    environment:
    POSTGRES_USER: user
    POSTGRES_PASSWORD: password
    POSTGRES_DB: testdb
  2. Run and Tear Down Tests:

    docker-compose up -d
    # Run integration tests
    docker-compose down

6. Sample Code and Configuration Summary​

This section provides practical code snippets and configurations to help automate testing in Spring Boot applications. The examples cover the persistence layer, service layer, controller layer, and containerized testing.

Maven Dependencies​

Make sure to include the necessary dependencies in your pom.xml for testing:

<dependencies>
<!-- Spring Boot Starter for Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- H2 Database for In-Memory Testing -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<!-- Testcontainers for Integration Testing -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.16.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.16.2</version>
<scope>test</scope>
</dependency>
</dependencies>

Sample Application Properties​

Define properties for the test environment in src/test/resources/application-test.yml:

spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true

Persistence Layer Test Example​

Example of testing a repository with an embedded H2 database:

@SpringBootTest
@Transactional
public class UserRepositoryTest {

@Autowired
private UserRepository userRepository;

@Test
public void testSaveUser() {
User user = new User("John Doe", "john.doe@example.com");
User savedUser = userRepository.save(user);
assertNotNull(savedUser.getId());
}
}

Service Layer Test Example​

Example of unit testing the service layer with Mockito:

@ExtendWith(MockitoExtension.class)
public class UserServiceTest {

@Mock
private UserRepository userRepository;

@InjectMocks
private UserService userService;

@Test
public void testCreateUser() {
User user = new User("Alice", "alice@example.com");
when(userRepository.save(any(User.class))).thenReturn(user);

User savedUser = userService.createUser(user);
assertEquals("Alice", savedUser.getName());
}
}

Controller Layer Test Example​

Example of testing a controller using MockMvc:

@WebMvcTest(UserController.class)
public class UserControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private UserService userService;

@Test
public void testGetUser() throws Exception {
when(userService.getUserById(anyLong())).thenReturn(new User(1L, "Alice"));

mockMvc.perform(get("/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Alice"));
}
}

Integration Testing with Testcontainers​

Example of a test that uses Testcontainers to spin up a PostgreSQL instance for integration tests:

@SpringBootTest
public class UserRepositoryIntegrationTest {

static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13")
.withDatabaseName("testdb")
.withUsername("user")
.withPassword("password");

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
postgres.start();
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@Autowired
private UserRepository userRepository;

@Test
public void testFindUserById() {
User user = new User("Alice", "alice@example.com");
userRepository.save(user);

Optional<User> foundUser = userRepository.findById(user.getId());
assertTrue(foundUser.isPresent());
assertEquals("Alice", foundUser.get().getName());
}
}

Docker Compose Configuration​

An example of a docker-compose.yml file for running your application and PostgreSQL for testing:

version: '3.8'
services:
app:
image: my-spring-app:latest
ports:
- "8080:8080"
environment:
SPRING_PROFILES_ACTIVE: test
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/testdb
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: password

postgres:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: testdb

Running Tests in Docker​

Run your integration tests with Docker Compose:

docker-compose up -d
# Run tests
mvn test
docker-compose down

This guide offers configurations and code samples to help set up automated testing for Spring Boot applications in both development and containerized environments, suitable for integration into CI/CD pipelines.

Deep Dive into Caffeine Cache and Integration with Spring Boot

· 阅读需 13 分钟

Introduction​

Caffeine is a high-performance, near-optimal caching library for Java, widely used in Spring Boot applications to improve performance by reducing the load on databases and other backend services. This article delves into the details of Caffeine cache, its integration with Spring Boot, and how it works under the hood. We'll also provide real-life production use cases and sample code to illustrate its usage.


Caffeine Cache Overview​

Caffeine is designed to be efficient and easy to use, providing advanced features like eviction policies, time-based expiration, and statistics recording. It uses a combination of data structures and algorithms to manage cache entries efficiently.

Key Features​

  • High Performance: Optimized for fast read and write operations.
  • Eviction Policies: Supports LRU, FIFO, and LFU eviction policies.
  • Time-Based Expiration: Entries can be expired after a specified period of inactivity or since they were last written.
  • Statistics Recording: Tracks cache hits, misses, and evictions for monitoring and tuning.

Configuring Caffeine Cache in Spring Boot​

Adding Dependencies​

First, ensure you have the necessary dependencies in your pom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>

Enabling Caching​

Enable caching in your Spring Boot application by adding the @EnableCaching annotation to your main application class:

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

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

Configuring Cache Manager​

Create a configuration class to set up Caffeine cache:

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("userCache", "productCache");
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}

Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(600, TimeUnit.SECONDS)
.recordStats();
}
}

YAML Configuration​

You can also configure Caffeine cache using YAML:

spring:
cache:
type: caffeine
cache-names:
- userCache
- productCache
caffeine:
spec: maximumSize=500,expireAfterAccess=600s

Explanation of Caffeine Cache Configuration​

  • CaffeineCacheManager: Manages multiple caches, each identified by a name.
  • Cache Names: "userCache" and "productCache" are used to identify different caches.
  • maximumSize(500): Sets the maximum number of entries the cache can hold.
  • expireAfterAccess(600, TimeUnit.SECONDS): Specifies that entries should be expired after 600 seconds of inactivity.
  • recordStats(): Enables statistics recording for monitoring cache performance.

How Caffeine Cache Works​

Cache Population​

When a cache miss occurs, Caffeine fetches the data from the underlying data source (e.g., database) and stores it in the cache.

Cache Eviction​

Caffeine uses various eviction policies to manage the cache size:

  • Least Recently Used (LRU): Evicts the least recently used entries first.
  • First In, First Out (FIFO): Evicts the oldest entries first.
  • Least Frequently Used (LFU): Evicts the least frequently used entries first.

Expiration​

Caffeine supports time-based expiration:

  • Expire After Access: Entries are expired after a specified period of inactivity.
  • Expire After Write: Entries are expired after a specified period since they were last written.

Statistics​

Caffeine can record statistics such as cache hits, misses, and evictions, which can be useful for monitoring and tuning the cache.

Statistics Recording​

Caffeine provides a recordStats() method that enables the collection of cache statistics. This method returns a Caffeine instance that will record statistics for the cache.

Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(600, TimeUnit.SECONDS)
.recordStats();
}

Tracking Cache Hits, Misses, and Evictions​

Once statistics recording is enabled, you can access the statistics using the stats() method on the cache. This method returns a CacheStats object that contains various metrics:

  • hitCount(): The number of successful cache hits.
  • missCount(): The number of cache misses.
  • evictionCount(): The number of cache evictions.
  • loadSuccessCount(): The number of times a cache miss was successfully loaded.
  • loadFailureCount(): The number of times a cache miss failed to load.

Here's an example of how to access and log cache statistics:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;

public class CacheStatisticsExample {

public static void main(String[] args) {
Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(100)
.recordStats()
.build();

// Perform cache operations
cache.put("key1", "value1");
cache.getIfPresent("key1");
cache.getIfPresent("key2");

// Access cache statistics
CacheStats stats = cache.stats();
System.out.println("Cache Hits: " + stats.hitCount());
System.out.println("Cache Misses: " + stats.missCount());
System.out.println("Cache Evictions: " + stats.evictionCount());
}
}

Caffeine Cache Internals​

Cache Interface​

The Cache interface defines the core operations for interacting with the cache:

public interface Cache<K, V> {
V getIfPresent(K key);
V get(K key, Function<? super K, ? extends V> mappingFunction);
void put(K key, V value);
void invalidate(K key);
void invalidateAll();
long estimatedSize();
CacheStats stats();
}

Caffeine Class​

The Caffeine class is the builder for creating cache instances:

public final class Caffeine<K, V> {
public static <K, V> Caffeine<K, V>newBuilder() {
return new Caffeine<>();
}

public Caffeine<K, V> maximumSize(long maximumSize) {
// Implementation
return this;
}

public Caffeine<K, V> expireAfterAccess(long duration, TimeUnit unit) {
// Implementation
return this;
}

public Cache<K, V> build() {
// Implementation
return new BoundedLocalCache<>(this);
}
}

BoundedLocalCache Class​

The BoundedLocalCache class is the core implementation of the cache, handling operations like get, put, and eviction:

final class BoundedLocalCache<K, V> extends LocalCache<K, V> {
BoundedLocalCache(Caffeine<K, V> builder) {
// Initialization
}

@Override
public V getIfPresent(Object key) {
// Implementation
}

@Override
public void put(K key, V value) {
// Implementation
}

@Override
public void invalidate(Object key) {
// Implementation
}

@Override
public long estimatedSize() {
// Implementation
}

@Override
public CacheStats stats() {
// Implementation
}
}

Detailed Logic in Caffeine Cache​

Cache Lookup​

Caffeine uses a ConcurrentHashMap to store cache entries. When a lookup is performed, Caffeine checks if the key exists in the map:

public V getIfPresent(Object key) {
Node<K, V> node = data.get(key);
if (node == null) {
return null;
}
// Additional logic for expiration and eviction checks
return node.getValue();
}

Cache Miss and Method Execution​

If the key is not found in the cache, the method is executed, and the result is stored in the cache:

public V get(K key, Function<? super K, ? extends V> mappingFunction) {
V value = getIfPresent(key);
if (value != null) {
return value;
}
value = mappingFunction.apply(key);
put(key, value);
return value;
}

Cache Update​

When the result is fetched from the database, it is stored in the cache:

public void put(K key, V value) {
Node<K, V> node = new Node<>(key, value);
data.put(key, node);
// Additional logic for eviction and expiration
}

Real-Life Production Use Cases​

1. User Profile Caching​

In a social media application, user profiles are frequently accessed. Caching user profiles can significantly reduce the load on the database and improve response times.

2. Product Catalog Caching​

In an e-commerce application, product catalogs are often large and frequently accessed. Caching product data can improve performance and reduce database load.

3. API Response Caching​

In microservices, caching API responses can reduce the number of calls to downstream services, improving overall system performance.

4. Session Data Caching​

In applications with user sessions, caching session data can reduce the load on session storage systems and improve scalability.


Sample Code​

Caffeine Cache Configuration​

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("userCache", "productCache");
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}

Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(600, TimeUnit.SECONDS)
.recordStats();
}
}

User Service with Caching​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from database
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User(userId, "John Doe");
}
}

Flow Diagram​

Here's a textual diagram to illustrate the flow:

graph TD
A[Client Request] --> B{Cache Lookup}
B -->|Cache Hit| C[Return Cached Data]
B -->|Cache Miss| D[Execute Method]
D --> E[Fetch from Database]
E --> F[Update Cache]
F --> C

Explanation of the Flow Diagram​

  1. Client Request: The client makes a request to fetch a user by ID.
  2. Cache Lookup: The cache manager looks up the userId in the "userCache".
  3. Cache Hit: If the user is found in the cache, the cached data is returned immediately.
  4. Cache Miss: If the user is not found in the cache, the method getUserById(Long userId) is executed.
  5. Execute Method: The method fetches the user from the database.
  6. Fetch from Database: The user is retrieved from the database.
  7. Update Cache: The fetched user is stored in the cache.
  8. Return Cached Data: The user data is returned to the client.

Statistics Logging, Tracking and Mornitoring​

Caffeine's statistics can be tracked using logging tools and even integrated with GUI tools for better visualization and monitoring.

1. Logging Cache Statistics​

Log cache statistics using a logging framework like SLF4J or Log4j. This allows you to periodically log the cache metrics to a log file or console.

Example: Logging Cache Statistics with SLF4J​

First, add the necessary dependencies in pom.xml:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>

Next, configure cache to log statistics periodically:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class CacheStatisticsLogger {

private static final Logger logger = LoggerFactory.getLogger(CacheStatisticsLogger.class);

public static void main(String[] args) {
Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(100)
.recordStats()
.build();

// Perform cache operations
cache.put("key1", "value1");
cache.getIfPresent("key1");
cache.getIfPresent("key2");

// Log cache statistics
logCacheStats(cache);
}

private static void logCacheStats(Cache<?, ?> cache) {
CacheStats stats = cache.stats();
logger.info("Cache Hits: {}", stats.hitCount());
logger.info("Cache Misses: {}", stats.missCount());
logger.info("Cache Evictions: {}", stats.evictionCount());
logger.info("Load Success Count: {}", stats.loadSuccessCount());
logger.info("Load Failure Count: {}", stats.loadFailureCount());
}
}

2. Integrating with GUI Tools​

Integrate Caffeine's statistics with GUI tools like Grafana or Prometheus for better visualization and monitoring. Here's an example of how to integrate with Prometheus and Grafana.

Step 1: Add Prometheus and Grafana Dependencies​

Add the necessary dependencies to pom.xml:

<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.15.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.15.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.15.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.15.0</version>
</dependency>

Step 2: Configure Prometheus Metrics Collector​

Create a configuration class to collect cache statistics using Prometheus:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Configuration
public class PrometheusConfig {

private final Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(100)
.recordStats()
.build();

private final Counter cacheHits = Counter.build()
.name("cache_hits_total")
.help("Total number of cache hits.")
.register();

private final Counter cacheMisses = Counter.build()
.name("cache_misses_total")
.help("Total number of cache misses.")
.register();

private final Counter cacheEvictions = Counter.build()
.name("cache_evictions_total")
.help("Total number of cache evictions.")
.register();

private final Gauge cacheSize = Gauge.build()
.name("cache_size")
.help("Current size of the cache.")
.register();

@PostConstruct
public void init() {
DefaultExports.initialize();
}

@Bean
public ServletRegistrationBean<MetricsServlet> metricsServlet() {
CollectorRegistry collectorRegistry = new CollectorRegistry();
collectorRegistry.register(cacheHits);
collectorRegistry.register(cacheMisses);
collectorRegistry.register(cacheEvictions);
collectorRegistry.register(cacheSize);
return new ServletRegistrationBean<>(new MetricsServlet(collectorRegistry), "/metrics");
}

@Bean
public Cache<String, String> cache() {
return cache;
}

@Bean
public Runnable cacheStatsCollector() {
return () -> {
CacheStats stats = cache.stats();
cacheHits.inc(stats.hitCount());
cacheMisses.inc(stats.missCount());
cacheEvictions.inc(stats.evictionCount());
cacheSize.set(cache.estimatedSize());
};
}
}

Step 3: Configure Prometheus and Grafana​

  1. Prometheus: Configure Prometheus to scrape the metrics endpoint (/metrics) of the Spring Boot application.

  2. Grafana: Set up Grafana to visualize the metrics collected by Prometheus. Dashboards can be created to display cache hits, misses, evictions, and other relevant metrics.

Example: Prometheus Configuration​

Create a prometheus.yml file:

global:
scrape_interval: 15s

scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:8080']

Example: Grafana Dashboard​

  1. Add Prometheus as a Data Source in Grafana.
  2. Create a Dashboard and add panels to visualize the cache metrics:
    • Cache Hits: cache_hits_total
    • Cache Misses: cache_misses_total
    • Cache Evictions: cache_evictions_total
    • Cache Size: cache_size

Summary​

By logging cache statistics using a logging framework like SLF4J or integrating with monitoring tools like Prometheus and Grafana, we can effectively track and visualize the performance of the Caffeine cache. This allows us to monitor cache hits, misses, evictions, and other metrics, helping us to tune and optimize the cache for better performance.

Conclusion​

Caffeine cache is a powerful tool for improving the performance and scalability of Spring Boot applications. By understanding how Caffeine works, including its eviction policies and expiration

Deep Dive into Memory Cache in Spring Boot Application

· 阅读需 13 分钟

Table of Contents​


Introduction​

In modern microservice architectures, performance and scalability are critical. Memory caching is a powerful technique to improve application performance by reducing the load on databases and other backend services. This article delves into the design and implementation of memory caches in Spring Boot applications, focusing on real-life project scenarios and the latest technology stack.


Understanding Memory Cache​

A memory cache is a high-speed data storage layer that stores a subset of data, typically transient in nature, so that future requests for that data are served faster. Caching can significantly improve the performance and scalability of applications by reducing the need to access slower underlying storage layers.

Key Concepts​

  • Cache Hit: When the requested data is found in the cache.
  • Cache Miss: When the requested data is not found in the cache.
  • Cache Eviction: The process of removing data from the cache to make room for new data.

Cache Design Principles​

1. Cache Hit and Miss​

  • Cache Hit: When the requested data is found in the cache.
  • Cache Miss: When the requested data is not found in the cache.

2. Cache Eviction Policies​

  • Least Recently Used (LRU): Removes the least recently used items first.
  • First In, First Out (FIFO): Removes the oldest items first.
  • Least Frequently Used (LFU): Removes the least frequently used items first.

3. Cache Invalidation​

  • Time-to-Live (TTL): Data is automatically invalidated after a certain period.
  • Event-Driven Invalidation: Data is invalidated based on specific events (e.g., data updates).

Cache Implementation in Spring Boot​

Spring Boot Cache Abstraction​

Spring Boot provides a powerful cache abstraction that allows you to apply caching to your methods with minimal configuration. The core annotation is @Cacheable, which caches the result of a method invocation.

Caffeine Cache Integration​

Caffeine is a high-performance, near-optimal caching library for Java. It is widely used in Spring Boot applications due to its efficiency and ease of integration.

Cache Configuration​

To configure Caffeine cache in a Spring Boot application, you need to add the following dependencies:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>

Next, enable caching in your Spring Boot application by adding the @EnableCaching annotation to your main application class:

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

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

YAML Configuration​

You can also configure Caffeine cache using YAML:

spring:
cache:
type: caffeine
cache-names:
- userCache
- productCache
caffeine:
spec: maximumSize=500,expireAfterAccess=600s

Detailed Logic in Caffeine Cache​

When you annotate a method with @Cacheable in Spring Boot, the cache manager will first check if the requested data is already present in the specified cache. If the data is found, it will be returned immediately, bypassing the method's execution. If the data is not found, the method will be executed, and the result will be stored in the cache for future requests.

Cache Lookup​

Caffeine uses a ConcurrentHashMap to store cache entries. When a lookup is performed, Caffeine checks if the key exists in the map:

public V getIfPresent(Object key) {
Node<K, V> node = data.get(key);
if (node == null) {
return null;
}
// Additional logic for expiration and eviction checks
return node.getValue();
}

Cache Miss and Method Execution​

If the key is not found in the cache, the method is executed, and the result is stored in the cache:

public V get(K key, Function<? super K, ? extends V> mappingFunction) {
V value = getIfPresent(key);
if (value != null) {
return value;
}
value = mappingFunction.apply(key);
put(key, value);
return value;
}

Cache Update​

When the result is fetched from the database, it is stored in the cache:

public void put(K key, V value) {
Node<K, V> node = new Node<>(key, value);
data.put(key, node);
// Additional logic for eviction and expiration
}

Microservice Architecture and Cache​

Cache Consistency​

Ensuring cache consistency is crucial in distributed systems. Techniques like cache invalidation, event-driven updates, and distributed locking can be used to maintain consistency.

Cache Eviction Strategies​

Choosing the right eviction strategy is important. LRU is commonly used, but in scenarios with frequent updates, LFU or custom eviction policies might be more appropriate.


Complex Scenario 1: Flow Control and Backpressure Handling​

Problem Statement​

In high-traffic scenarios, the cache might become a bottleneck if not properly managed. Flow control and backpressure handling are essential to prevent the cache from being overwhelmed.

Solution Architecture​

  1. Rate Limiting: Implement rate limiting to control the number of requests hitting the cache.
  2. Circuit Breaker: Use a circuit breaker to handle failures gracefully and prevent cascading failures.
  3. Backpressure Handling: Implement backpressure mechanisms to manage the flow of data between services.

Flow Chart​

graph TD
A[Client Request] --> B{Rate Limiter}
B -->|Allowed| C[Cache Lookup]
B -->|Denied| D[Fallback to Database]
C --> E{Cache Hit?}
E -->|Yes| F[Return Cached Data]
E -->|No| G[Fetch from Database]
G --> H[Update Cache]
H --> F
D --> G

Sample Code​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from database
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}
}

Complex Scenario 2: Cache Invalidation and Data Synchronization​

Problem Statement​

Ensuring that the cache remains consistent with the underlying data store is challenging, especially in distributed systems.

Solution Architecture​

  1. Event-Driven Invalidation: Use event-driven mechanisms to invalidate the cache when data changes.
  2. Distributed Locking: Use distributed locking to ensure that only one service updates the cache at a time.
  3. TTL-Based Invalidation: Use Time-to-Live (TTL) to automatically invalidate cache entries after a certain period.

Sequence Diagram​

sequenceDiagram
participant Client
participant Service
participant Cache
participant Database

Client->>Service: Update User
Service->>Database: Update User in Database
Database-->>Service: User Updated
Service->>Cache: Invalidate User Cache
Cache-->>Service: Cache Invalidated
Service-->>Client: User Updated

Sample Code​

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@CacheEvict(value = "userCache", key = "#userId")
public void updateUser(Long userId, User user) {
// Update user in database
updateUserInDatabase(userId, user);
}

private void updateUserInDatabase(Long userId, User user) {
// Database update logic
}
}

Complex Scenario 3: Cache Stampede Prevention​

Problem Statement​

A cache stampede occurs when multiple requests for the same data hit the cache simultaneously, leading to a spike in database load.

Solution Architecture​

  1. Probabilistic Early Expiration: Use probabilistic early expiration to spread out cache expiration times.
  2. Locking Mechanism: Implement a locking mechanism to ensure that only one request populates the cache at a time.
  3. Background Refresh: Use background refresh to keep the cache up-to-date without impacting the user experience.

Flow Chart​

graph TD
A[Client Request] --> B{Cache Hit?}
B -->|Yes| C[Return Cached Data]
B -->|No| D{Lock Acquired?}
D -->|Yes| E[Fetch from Database]
D -->|No| F[Wait for Lock]
E --> G[Update Cache]
G --> C
F --> B

Sample Code​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class UserService {

private final Lock lock = new ReentrantLock();

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
try {
lock.lock();
// Double-check to ensure the cache is still empty
User cachedUser = fetchUserFromCache(userId);
if (cachedUser != null) {
return cachedUser;
}
// Fetch user from database
User user = fetchUserFromDatabase(userId);
// Update cache
updateCache(userId, user);
return user;
} finally {
lock.unlock();
}
}

private User fetchUserFromCache(Long userId) {
// Cache lookup logic
return null;
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}

private void updateCache(Long userId, User user) {
// Cache update logic
}
}

Real-Life Production Use Cases​

1. User Profile Caching​

In a social media application, user profiles are frequently accessed. Caching user profiles can significantly reduce the load on the database and improve response times.

2. Product Catalog Caching​

In an e-commerce application, product catalogs are often large and frequently accessed. Caching product data can improve performance and reduce database load.

3. API Response Caching​

In microservices, caching API responses can reduce the number of calls to downstream services, improving overall system performance.

4. Session Data Caching​

In applications with user sessions, caching session data can reduce the load on session storage systems and improve scalability.


Sample Code​

Caffeine Cache Configuration​

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("userCache", "productCache");
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}

Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(600, TimeUnit.SECONDS);
}
}

User Service with Caching​

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
// Fetch user from database
return fetchUserFromDatabase(userId);
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}
}

Rate Limiting for Cache Access​

Why Rate Limiting is Needed​

Reading from a memory cache is generally very fast and should not be a bottleneck in terms of performance. However, there are several reasons why you might still want to implement rate limiting for cache access, even if the cache is in memory:

  1. Resource Management: Even though reading from memory is fast, excessive concurrent requests can still consume a significant amount of CPU and memory resources. Rate limiting helps manage these resources more effectively, ensuring that the system remains responsive and does not become overwhelmed.

  2. Preventing Cache Stampede: While reading from the cache is fast, if the cache is not populated (cache miss), all concurrent requests will end up fetching the data from the database or another slow data source. This can lead to a cache stampede, where the database or other backend services are overwhelmed with requests. Rate limiting can help mitigate this by controlling the number of concurrent requests that can hit the cache.

  3. Graceful Degradation: In high-traffic scenarios, rate limiting ensures that the system can gracefully degrade its performance. By limiting the number of requests that can access the cache, you can prevent the system from becoming unresponsive or crashing under heavy load.

  4. Security and Denial of Service (DoS) Prevention: Rate limiting can also be a security measure to prevent Denial of Service (DoS) attacks. By limiting the number of requests from a single client or IP address, you can protect the system from being overwhelmed by malicious requests.

  5. Ensuring Fairness: Rate limiting ensures that all clients have fair access to the cache. Without rate limiting, a few clients with high request rates could monopolize the cache, leading to poor performance for other clients.

  6. Monitoring and Analytics: Rate limiting provides valuable data on the usage patterns of the cache. This data can be used for monitoring, analytics, and capacity planning.

Example: Rate Limiting with Spring Boot​

Here's an example of how you might implement rate limiting for cache access in a Spring Boot application using the Bucket4j library:

Add Dependencies​

<dependency>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-core</artifactId>
<version>7.5.0</version>
</dependency>

Rate Limiting Configuration​

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.Bucket4j;
import io.github.bucket4j.Refill;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class RateLimitingConfig {

@Bean
public Bucket cacheRateLimiter() {
Bandwidth limit = Bandwidth.classic(100, Refill.greedy(100, Duration.ofMinutes(1)));
return Bucket4j.builder().addLimit(limit).build();
}
}

Service Implementation with Rate Limiting​

import io.github.bucket4j.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Autowired
private Bucket cacheRateLimiter;

@Cacheable(value = "userCache", key = "#userId")
public User getUserById(Long userId) {
if (cacheRateLimiter.tryConsume(1)) {
// Fetch user from database
return fetchUserFromDatabase(userId);
} else {
throw new RuntimeException("Rate limit exceeded");
}
}

private User fetchUserFromDatabase(Long userId) {
// Database query logic
return new User();
}
}

Explanation​

  1. Bucket4j Configuration: The Bucket4j library is used to configure a rate limiter with a limit of 100 requests per minute.
  2. Service Implementation: The getUserById method checks if the rate limit has been exceeded before fetching the user from the database. If the limit is exceeded, it throws an exception.

Conclusion​

While reading from a memory cache is fast and should not be a bottleneck, implementing rate limiting can provide several benefits, including resource management, preventing cache stampedes, ensuring fairness, and enhancing security. By using libraries like Bucket4j, you can easily implement rate limiting in your Spring Boot application to manage cache access effectively.


Conclusion​

Memory caching is a powerful tool for improving the performance and scalability of Spring Boot applications in a microservice architecture. By understanding the principles of cache design and implementing best practices, you can effectively manage cache performance, consistency, and reliability in real-life projects.

This article provides a comprehensive guide to memory cache design and implementation in Spring Boot, covering various scenarios and best practices. Use the provided sample code and diagrams to implement efficient caching in your microservice architecture.

Deep Dive into Spring Boot and Quarkus

· 阅读需 6 分钟

Introduction​

Spring Boot and Quarkus are two of the most popular frameworks for building Java applications. Both frameworks aim to simplify the development process and provide robust solutions for modern application development. However, they have different philosophies and approaches. This article will provide a detailed comparison of Spring Boot and Quarkus, exploring their architecture, performance, ecosystem, and community support.

Architecture​

2.1 Overview​

Spring Boot​

Spring Boot is built on top of the Spring framework and provides a streamlined way to create stand-alone, production-grade Spring-based applications. It leverages the Spring ecosystem to offer a wide range of features out of the box.

Quarkus​

Quarkus is a Kubernetes-native Java framework designed for OpenJDK HotSpot and GraalVM. It is optimized for low memory usage and fast startup times, making it ideal for cloud-native applications.

2.2 Microservices​

Spring Boot​

Spring Boot is well-suited for building microservices. It provides built-in support for service discovery, configuration management, and circuit breakers through Spring Cloud Kubernetes, which integrates with Kubernetes' native service discovery mechanisms.

+-------------------+ +-------------------+ +----------------------------------+
| Spring Boot | | Spring Cloud | | Kubernetes |
| Microservice | | Kubernetes | | (Service Discovery and Config |
+-------------------+ +-------------------+ +----------------------------------+
| | |
| (1) Service Discovery | |
|-------------------------->| |
| | (2) Configuration Fetch |
| |-------------------------->|
| | |
| (3) Circuit Breaker | |
|<--------------------------| |
| | |
+-------------------+ +-------------------+ +--------------------------------+
| Spring Boot | | Spring Cloud | | Kubernetes |
| Microservice | | Kubernetes | | (Service Discovery and Config |
+-------------------+ +-------------------+ +--------------------------------+

Quarkus​

Quarkus also supports microservices with extensions for service discovery, configuration management, and fault tolerance. It integrates well with Kubernetes and provides native support for GraalVM.

+-------------------+ +-------------------+ +-------------------+
| Quarkus | | Kubernetes | | GraalVM |
| Microservice | | (Service Discovery| | (Native Image) |
+-------------------+ +-------------------+ +-------------------+
| | |
| (1) Service Discovery | |
|-------------------------->| |
| | (2) Configuration Fetch |
| |-------------------------->|
| | |
| (3) Native Image Build | |
|<--------------------------| |
| | |
+-------------------+ +-------------------+ +-------------------+
| Quarkus | | Kubernetes | | GraalVM |
| Microservice | | (Service Discovery| | (Native Image) |
+-------------------+ +-------------------+ +-------------------+

2.3 Dependency Injection​

Spring Boot​

Spring Boot uses the Spring Framework's dependency injection mechanism, which is based on the Inversion of Control (IoC) principle.

@Service
public class MyService {
public String getMessage() {
return "Hello, Spring Boot!";
}
}

@RestController
public class MyController {
@Autowired
private MyService myService;

@GetMapping("/message")
public String getMessage() {
return myService.getMessage();
}
}

Quarkus​

Quarkus uses the Contexts and Dependency Injection (CDI) standard for dependency injection, which is part of the Java EE specification.

@ApplicationScoped
public class MyService {
public String getMessage() {
return "Hello, Quarkus!";
}
}

@Path("/message")
public class MyResource {
@Inject
MyService myService;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getMessage() {
return myService.getMessage();
}
}

2.4 Reactive Programming​

Spring Boot​

Spring Boot supports reactive programming through Spring WebFlux, which is built on top of Project Reactor.

@RestController
public class MyReactiveController {
@GetMapping("/reactive-message")
public Mono<String> getReactiveMessage() {
return Mono.just("Hello, Reactive Spring Boot!");
}
}

Quarkus​

Quarkus supports reactive programming through its Reactive Streams Operators and integrates well with frameworks like Vert.x.

@Path("/reactive-message")
public class MyReactiveResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> getReactiveMessage() {
return Uni.createFrom().item("Hello, Reactive Quarkus!");
}
}

Performance​

3.1 Startup Time​

Spring Boot​

Spring Boot applications typically have a longer startup time due to the extensive initialization of the Spring framework.

Quarkus​

Quarkus applications have a much faster startup time, especially when using GraalVM to build native images.

3.2 Memory Usage​

Spring Boot​

Spring Boot applications generally consume more memory due to the heavyweight nature of the Spring framework.

Quarkus​

Quarkus applications consume less memory, making them more efficient for containerized environments.

3.3 Runtime Efficiency​

Spring Boot​

Spring Boot applications are optimized for traditional Java runtime environments.

Quarkus​

Quarkus applications are optimized for modern, cloud-native environments and can take advantage of GraalVM for native execution.

Ecosystem and Community​

4.1 Spring Ecosystem​

Spring Boot benefits from the extensive Spring ecosystem, which includes Spring Data, Spring Security, Spring Cloud Kubernetes, and many other modules.

4.2 Quarkus Ecosystem​

Quarkus has a growing ecosystem with extensions for various technologies, including databases, messaging systems, and cloud services.

4.3 Community Support​

Spring Boot​

Spring Boot has a large and active community, with extensive documentation, tutorials, and third-party libraries.

Quarkus​

Quarkus has a rapidly growing community, with increasing support from Red Hat and other contributors.

Sample Code​

5.1 Spring Boot Example​

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}

5.2 Quarkus Example​

import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@QuarkusMain
public class QuarkusExample {
public static void main(String[] args) {
Quarkus.run(args);
}
}

@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello, Quarkus!";
}
}

Conclusion​

Both Spring Boot and Quarkus are powerful frameworks for building Java applications, each with its own strengths and weaknesses. Spring Boot offers a mature and feature-rich ecosystem, making it a great choice for a wide range of applications. Quarkus, on the other hand, is optimized for modern, cloud-native environments and provides excellent performance and efficiency.

Choosing between Spring Boot and Quarkus depends on the specific requirements of your project, including performance, ecosystem, and community support. By understanding the differences and capabilities of both frameworks, you can make an informed decision that best suits your needs.

References​


This article provides a comprehensive comparison of Spring Boot and Quarkus, covering their architecture, performance, ecosystem, and community support. By understanding these frameworks, developers can choose the best tool for their projects and build efficient and scalable applications.