Skip to content

Commit

Permalink
♻️ refactor: 서비스 간 통신 api 수정 및 카프카 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn authored Jun 23, 2024
2 parents 0b59a86 + dbe0643 commit 30fe5ca
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 64 deletions.
3 changes: 3 additions & 0 deletions weather-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ dependencies {

// File
implementation 'org.apache.poi:poi-ooxml:5.2.0'

// Spring batch
implementation 'org.springframework.boot:spring-boot-starter-batch'
}

openApi {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.waither.weatherservice.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import com.waither.weatherservice.service.WeatherService;

import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class BatchConfig {

private final JobRepository jobRepository;
private final PlatformTransactionManager transactionManager;
private final WeatherService weatherService;

@Bean
public Job dailyWeatherJob() {
return new JobBuilder("dailyWeatherJob", jobRepository)
.start(dailyWeatherStep())
.build();
}

@Bean
public Step dailyWeatherStep() {
return new StepBuilder("dailyWeatherStep", jobRepository)
.tasklet(dailyWeatherTasklet(), transactionManager)
.build();
}

@Bean
public Tasklet dailyWeatherTasklet() {
return new DailyWeatherTasklet(weatherService);
}

@Bean
public Job expectedWeatherJob() {
return new JobBuilder("expectedWeatherJob", jobRepository)
.start(expectedWeatherStep())
.build();
}

@Bean
public Step expectedWeatherStep() {
return new StepBuilder("expectedWeatherStep", jobRepository)
.tasklet(expectedWeatherTasklet(), transactionManager)
.build();
}

@Bean
public Tasklet expectedWeatherTasklet() {
return new ExpectedWeatherTasklet(weatherService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.waither.weatherservice.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Component
@EnableScheduling
@RequiredArgsConstructor
@Slf4j
public class BatchScheduler {

private final JobLauncher jobLauncher;
private final Job dailyWeatherJob;
private final Job expectedWeatherJob;

@Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간마다
public void runDailyWeatherJob() {
try {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("executedTime", System.currentTimeMillis())
.toJobParameters();

jobLauncher.run(dailyWeatherJob, jobParameters);
} catch (JobExecutionException e) {
log.error("Error executing dailyWeatherJob: ", e);
}
}

@Scheduled(cron = "0 0 * * * *") // 1시간마다
public void runExpectedWeatherJob() {
try {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("executedTime", System.currentTimeMillis())
.toJobParameters();

jobLauncher.run(expectedWeatherJob, jobParameters);
} catch (JobExecutionException e) {
log.error("Error executing expectedWeatherJob: ", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.waither.weatherservice.batch;

import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import com.waither.weatherservice.entity.Region;
import com.waither.weatherservice.exception.WeatherExceptionHandler;
import com.waither.weatherservice.response.WeatherErrorCode;
import com.waither.weatherservice.service.WeatherService;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class DailyWeatherTasklet implements Tasklet {

private final WeatherService weatherService;

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
LocalDateTime now = LocalDateTime.now();
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_");
List<Region> regionList = weatherService.getRegionList();
regionList.stream()
.forEach(region -> {
try {
weatherService.createDailyWeather(region.getStartX(), region.getStartY(), dateTime[0],
dateTime[1]);
} catch (URISyntaxException e) {
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
}
});
return RepeatStatus.FINISHED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.waither.weatherservice.batch;

import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import com.waither.weatherservice.entity.Region;
import com.waither.weatherservice.exception.WeatherExceptionHandler;
import com.waither.weatherservice.response.WeatherErrorCode;
import com.waither.weatherservice.service.WeatherService;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ExpectedWeatherTasklet implements Tasklet {

private final WeatherService weatherService;

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
LocalDateTime now = LocalDateTime.now();
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_");
List<Region> regionList = weatherService.getRegionList();
regionList.stream()
.forEach(region -> {
try {
weatherService.createExpectedWeather(region.getStartX(), region.getStartY(), dateTime[0],
dateTime[1]);
} catch (URISyntaxException e) {
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
}
});
return RepeatStatus.FINISHED;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.waither.weatherservice.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -21,13 +21,13 @@ public class WeatherController {
private final WeatherService weatherService;

@GetMapping("/main")
public ApiResponse<MainWeatherResponse> getMainWeather(@RequestBody @Valid GetWeatherRequest getWeatherRequest) {
public ApiResponse<MainWeatherResponse> getMainWeather(@ModelAttribute @Valid GetWeatherRequest getWeatherRequest) {
return ApiResponse.onSuccess(
weatherService.getMainWeather(getWeatherRequest.latitude(), getWeatherRequest.longitude()));
}

@GetMapping("/region")
public ApiResponse<String> convertGpsToRegionName(@RequestBody @Valid GetWeatherRequest getWeatherRequest) {
public ApiResponse<String> convertGpsToRegionName(@ModelAttribute @Valid GetWeatherRequest getWeatherRequest) {
return ApiResponse.onSuccess(
weatherService.convertGpsToRegionName(getWeatherRequest.latitude(), getWeatherRequest.longitude()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public record MainWeatherResponse(

String pop,
String temp,
String tempMin,
String tempMax,
String humidity,
Expand All @@ -28,6 +29,7 @@ public record MainWeatherResponse(

public static MainWeatherResponse from(
String pop,
String temp,
String tempMin,
String tempMax,
String humidity,
Expand All @@ -47,6 +49,7 @@ public static MainWeatherResponse from(
) {
return MainWeatherResponse.builder()
.pop(pop)
.temp(temp)
.tempMin(tempMin)
.tempMax(tempMax)
.humidity(humidity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class DailyWeather {

// 강수확률 (%)
private String pop;
private String tmp;
private String tempMin;
private String tempMax;
private String humidity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
package com.waither.weatherservice.scheduler;

import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;

import com.waither.weatherservice.entity.Region;
import com.waither.weatherservice.exception.WeatherExceptionHandler;
import com.waither.weatherservice.response.WeatherErrorCode;
import com.waither.weatherservice.service.WeatherService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@ConfigurationProperties
public class SchedulerConfig {

private final WeatherService weatherService;

@Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간 마다
public void createDailyWeather() {

LocalDateTime now = LocalDateTime.now();
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_");
List<Region> regionList = weatherService.getRegionList();
regionList.stream()
.forEach(region -> {
try {
weatherService.createDailyWeather(region.getStartX(), region.getStartY(), dateTime[0],
dateTime[1]);
} catch (URISyntaxException e) {
new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
}
});
}

@Scheduled(cron = "0 0 * * * *") // 1시간 마다
public void createExpectedWeather() {

LocalDateTime now = LocalDateTime.now();
String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_");
List<Region> regionList = weatherService.getRegionList();
regionList.stream()
.forEach(region -> {
try {
weatherService.createExpectedWeather(region.getStartX(), region.getStartY(), dateTime[0],
dateTime[1]);
} catch (URISyntaxException e) {
new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
}
});
}
}
// package com.waither.weatherservice.scheduler;
//
// import java.net.URISyntaxException;
// import java.time.LocalDateTime;
// import java.util.List;
//
// import org.springframework.boot.context.properties.ConfigurationProperties;
// import org.springframework.scheduling.annotation.Scheduled;
//
// import com.waither.weatherservice.entity.Region;
// import com.waither.weatherservice.exception.WeatherExceptionHandler;
// import com.waither.weatherservice.response.WeatherErrorCode;
// import com.waither.weatherservice.service.WeatherService;
//
// import lombok.RequiredArgsConstructor;
// import lombok.extern.slf4j.Slf4j;
//
// @Slf4j
// @RequiredArgsConstructor
// @ConfigurationProperties
// public class SchedulerConfig {
//
// private final WeatherService weatherService;
//
// @Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간 마다
// public void createDailyWeather() {
//
// LocalDateTime now = LocalDateTime.now();
// String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_");
// List<Region> regionList = weatherService.getRegionList();
// regionList.stream()
// .forEach(region -> {
// try {
// weatherService.createDailyWeather(region.getStartX(), region.getStartY(), dateTime[0],
// dateTime[1]);
// } catch (URISyntaxException e) {
// throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
// }
// });
// }
//
// @Scheduled(cron = "0 0 * * * *") // 1시간 마다
// public void createExpectedWeather() {
//
// LocalDateTime now = LocalDateTime.now();
// String[] dateTime = weatherService.convertLocalDateTimeToString(now).split("_");
// List<Region> regionList = weatherService.getRegionList();
// regionList.stream()
// .forEach(region -> {
// try {
// weatherService.createExpectedWeather(region.getStartX(), region.getStartY(), dateTime[0],
// dateTime[1]);
// } catch (URISyntaxException e) {
// throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
// }
// });
// }
// }
Loading

0 comments on commit 30fe5ca

Please sign in to comment.