From bbf4affc160903d04b5fc60cd61ae359ecbc6c3a Mon Sep 17 00:00:00 2001 From: "woo-jin.shin" Date: Thu, 18 Nov 2021 00:38:16 +0900 Subject: [PATCH] [PPN-211113] Implement ConfigurationProperties --- .../processor/ProcessorApplication.java | 4 ++ .../constant/WebClientPropertiesUnitName.java | 19 ++++++++ .../client/SenderAPIClient.java | 36 +++++++++++++++ .../main/resources/development/webclient.yml | 4 +- .../main/resources/production/webclient.yml | 5 ++- receiver/build.gradle | 1 + .../ppomppu/receiver/ReceiverApplication.java | 5 ++- .../TelegramBotConfiguration.java | 7 ++- .../properties/TelegramBotProperties.java | 33 +++++++++++--- .../constant/WebClientPropertiesUnitName.java | 19 ++++++++ .../client/ProcessorAPIWebClient.java | 17 ++++--- .../resources/application-development.yml | 1 + .../main/resources/application-production.yml | 3 +- .../main/resources/development/webclient.yml | 4 +- .../main/resources/production/webclient.yml | 4 +- .../ppomppu/sender/SenderApplication.java | 4 ++ .../client/SlackMessageSender.java | 2 - support/build.gradle | 5 +-- ...PpomppuNotifierWebClientConfiguration.java | 19 ++++++++ .../properties/WebClientProperties.java | 44 +++++++++++++++++++ 20 files changed, 206 insertions(+), 30 deletions(-) create mode 100644 processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/constant/WebClientPropertiesUnitName.java create mode 100644 processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/infrastructure/client/SenderAPIClient.java create mode 100644 receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/dto/constant/WebClientPropertiesUnitName.java create mode 100644 support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/PpomppuNotifierWebClientConfiguration.java create mode 100644 support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/properties/WebClientProperties.java diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/ProcessorApplication.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/ProcessorApplication.java index b310e75..eb9f40c 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/ProcessorApplication.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/ProcessorApplication.java @@ -2,6 +2,9 @@ package com.myoa.engineering.crawl.ppomppu.processor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; + +import com.myoa.engineering.crawl.ppomppu.support.webclient.PpomppuNotifierWebClientConfiguration; /** * ProcessorApplication @@ -9,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @since 2021-08-20 * */ +@Import({ PpomppuNotifierWebClientConfiguration.class }) @SpringBootApplication public class ProcessorApplication { diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/constant/WebClientPropertiesUnitName.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/constant/WebClientPropertiesUnitName.java new file mode 100644 index 0000000..3383138 --- /dev/null +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/constant/WebClientPropertiesUnitName.java @@ -0,0 +1,19 @@ +package com.myoa.engineering.crawl.ppomppu.processor.dto.constant; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * WebClientPropertiesUnitName + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-11-18 + * + */ +@Getter +@AllArgsConstructor +public enum WebClientPropertiesUnitName { + PPOMPPU_NOTIFIER_SENDER_API("ppn-sender-api"), + ; + + private String unitName; +} diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/infrastructure/client/SenderAPIClient.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/infrastructure/client/SenderAPIClient.java new file mode 100644 index 0000000..e9de2a8 --- /dev/null +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/infrastructure/client/SenderAPIClient.java @@ -0,0 +1,36 @@ +package com.myoa.engineering.crawl.ppomppu.processor.infrastructure.client; + +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +import com.myoa.engineering.crawl.ppomppu.processor.dto.constant.WebClientPropertiesUnitName; +import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebClientFilterFactory; +import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties; +import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties.WebClientPropertiesUnit; + +import lombok.extern.slf4j.Slf4j; + +/** + * PpomppuNotifierSenderAPIClient + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-11-17 + * + */ +@Slf4j +@Component +public class SenderAPIClient { + + private final WebClient webClient; + + public SenderAPIClient(WebClientProperties webClientProperties) { + WebClientPropertiesUnit webClientPropertiesUnit = + webClientProperties.find(WebClientPropertiesUnitName.PPOMPPU_NOTIFIER_SENDER_API.getUnitName()); + this.webClient = WebClient.builder() + .baseUrl(webClientPropertiesUnit.getBaseUrl()) + .filter(WebClientFilterFactory.logRequest()) + .filter(WebClientFilterFactory.logResponse()) + .build(); + } +} + +// TODO webclient properties \ No newline at end of file diff --git a/processor/src/main/resources/development/webclient.yml b/processor/src/main/resources/development/webclient.yml index dc5e59f..d600658 100644 --- a/processor/src/main/resources/development/webclient.yml +++ b/processor/src/main/resources/development/webclient.yml @@ -1,5 +1,5 @@ webclient: - some: test + init: true units: - - unit-name: processor-api + - unit-name: ppn-sender-api base-url: http://localhost:20081 \ No newline at end of file diff --git a/processor/src/main/resources/production/webclient.yml b/processor/src/main/resources/production/webclient.yml index 4bb1d1d..862c2b9 100644 --- a/processor/src/main/resources/production/webclient.yml +++ b/processor/src/main/resources/production/webclient.yml @@ -1,4 +1,5 @@ webclient: + init: true units: - - unit-name: processor-api - base-url: http://soundhoundfound-processor:20080 \ No newline at end of file + - unit-name: ppn-sender-api + base-url: http://ppn_sender:20081 \ No newline at end of file diff --git a/receiver/build.gradle b/receiver/build.gradle index f56745d..f7771b1 100644 --- a/receiver/build.gradle +++ b/receiver/build.gradle @@ -8,6 +8,7 @@ dependencies { // https://projectreactor.io/docs/core/release/reference/#debug-activate implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-configuration-processor' + implementation 'org.springframework.cloud:spring-cloud-starter-config' implementation 'org.telegram:telegrambots:5.3.0' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/ReceiverApplication.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/ReceiverApplication.java index a50c70b..339a01d 100644 --- a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/ReceiverApplication.java +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/ReceiverApplication.java @@ -3,8 +3,10 @@ package com.myoa.engineering.crawl.ppomppu.receiver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Import; import com.myoa.engineering.crawl.ppomppu.receiver.configuration.properties.TelegramBotProperties; +import com.myoa.engineering.crawl.ppomppu.support.webclient.PpomppuNotifierWebClientConfiguration; /** * ReceiverApplication @@ -12,8 +14,9 @@ import com.myoa.engineering.crawl.ppomppu.receiver.configuration.properties.Tele * @since 2021-08-20 * */ +@Import({ PpomppuNotifierWebClientConfiguration.class}) @SpringBootApplication -@EnableConfigurationProperties({ TelegramBotProperties.class }) +// @EnableConfigurationProperties({ TelegramBotProperties.class }) public class ReceiverApplication { public static void main(String[] args) { diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java index 31ab08b..dd120dc 100644 --- a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java @@ -9,6 +9,7 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import com.myoa.engineering.crawl.ppomppu.receiver.configuration.properties.TelegramBotProperties; +import com.myoa.engineering.crawl.ppomppu.receiver.configuration.properties.TelegramBotProperties.TelegramBotPropertiesUnit; import com.myoa.engineering.crawl.ppomppu.receiver.dispatch.MessageDispatcher; import com.myoa.engineering.crawl.ppomppu.receiver.handler.message.MessageHandler; @@ -21,6 +22,7 @@ import com.myoa.engineering.crawl.ppomppu.receiver.handler.message.MessageHandle @Configuration public class TelegramBotConfiguration { + private static final String BOT_PROPERTIES_UNIT_NAME = "ppomppu_notify_bot"; @Bean public TelegramBotsApi telegramBotsApi(MessageDispatcher messageDispatcher) throws TelegramApiException { TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class); @@ -30,7 +32,8 @@ public class TelegramBotConfiguration { @Bean public MessageDispatcher messageDispatcher(List messageHandlers, - TelegramBotProperties botProperties) { - return new MessageDispatcher(messageHandlers, botProperties.getName(), botProperties.getToken()); + TelegramBotProperties telegramBotProperties) { + TelegramBotPropertiesUnit propertiesUnit = telegramBotProperties.find(BOT_PROPERTIES_UNIT_NAME); + return new MessageDispatcher(messageHandlers, propertiesUnit.getName(), propertiesUnit.getToken()); } } diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/properties/TelegramBotProperties.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/properties/TelegramBotProperties.java index f524798..e438889 100644 --- a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/properties/TelegramBotProperties.java +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/properties/TelegramBotProperties.java @@ -1,9 +1,17 @@ package com.myoa.engineering.crawl.ppomppu.receiver.configuration.properties; +import java.util.ArrayList; +import java.util.List; + import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConstructorBinding; +import org.springframework.stereotype.Component; +import lombok.AllArgsConstructor; +import lombok.Data; import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; /** * TelegramBotProperties @@ -11,15 +19,26 @@ import lombok.Getter; * @since 2021-09-05 * */ +@Component +@Setter @Getter -@ConstructorBinding -@ConfigurationProperties(prefix = "telegram.bot") +@ConfigurationProperties(prefix = "infra.telegram.bot") public class TelegramBotProperties { - private final String name; - private final String token; - public TelegramBotProperties(final String name, final String token) { - this.name = name; - this.token = token; + private List units = new ArrayList<>(); + + @Data + public static class TelegramBotPropertiesUnit { + private String unitName; + private String name; + private String token; + } + + public TelegramBotPropertiesUnit find(String unitName) { + return units.stream() + .filter(e -> e.getUnitName().equals(unitName)) + .findFirst() + .orElseThrow( + () -> new IllegalArgumentException(this.getClass().getName() + ": unitName Not found. " + unitName)); } } diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/dto/constant/WebClientPropertiesUnitName.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/dto/constant/WebClientPropertiesUnitName.java new file mode 100644 index 0000000..bdab14d --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/dto/constant/WebClientPropertiesUnitName.java @@ -0,0 +1,19 @@ +package com.myoa.engineering.crawl.ppomppu.receiver.dto.constant; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * WebClientPropertiesUnitName + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-11-18 + * + */ +@Getter +@AllArgsConstructor +public enum WebClientPropertiesUnitName { + PPOMPPU_NOTIFIER_PROCESSOR_API("ppn-processor-api"), + ; + + private String unitName; +} diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/infrastructure/client/ProcessorAPIWebClient.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/infrastructure/client/ProcessorAPIWebClient.java index 87b763e..2dbca2a 100644 --- a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/infrastructure/client/ProcessorAPIWebClient.java +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/infrastructure/client/ProcessorAPIWebClient.java @@ -1,12 +1,15 @@ package com.myoa.engineering.crawl.ppomppu.receiver.infrastructure.client; -import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClientRequestException; +import com.myoa.engineering.crawl.ppomppu.receiver.dto.constant.WebClientPropertiesUnitName; import com.myoa.engineering.crawl.ppomppu.support.dto.code.PpomppuBoardName; +import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebClientFilterFactory; +import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties; +import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties.WebClientPropertiesUnit; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; @@ -18,10 +21,14 @@ public class ProcessorAPIWebClient { private final WebClient webClient; - public ProcessorAPIWebClient(WebClient.Builder webClientBuilder, - @Value("${webclient.base-url}") String baseUrl) { - this.webClient = webClientBuilder.baseUrl(baseUrl) - .build(); + public ProcessorAPIWebClient(WebClientProperties webClientProperties) { + WebClientPropertiesUnit webClientPropertiesUnit = + webClientProperties.find(WebClientPropertiesUnitName.PPOMPPU_NOTIFIER_PROCESSOR_API.getUnitName()); + this.webClient = WebClient.builder() + .baseUrl(webClientPropertiesUnit.getBaseUrl()) + .filter(WebClientFilterFactory.logRequest()) + .filter(WebClientFilterFactory.logResponse()) + .build(); } public Mono emitParseEvent(PpomppuBoardName boardName) { diff --git a/receiver/src/main/resources/application-development.yml b/receiver/src/main/resources/application-development.yml index 70ac133..ad67d50 100644 --- a/receiver/src/main/resources/application-development.yml +++ b/receiver/src/main/resources/application-development.yml @@ -4,3 +4,4 @@ spring: on-profile: development import: - classpath:/development/webclient.yml + - "configserver:http://192.168.0.100:11080" \ No newline at end of file diff --git a/receiver/src/main/resources/application-production.yml b/receiver/src/main/resources/application-production.yml index 7c1069c..831bcb9 100644 --- a/receiver/src/main/resources/application-production.yml +++ b/receiver/src/main/resources/application-production.yml @@ -3,4 +3,5 @@ spring: activate: on-profile: production import: - - classpath:/production/webclient.yml \ No newline at end of file + - classpath:/production/webclient.yml + - "configserver:http://192.168.0.100:11080" \ No newline at end of file diff --git a/receiver/src/main/resources/development/webclient.yml b/receiver/src/main/resources/development/webclient.yml index abe8b3b..9fbfe3a 100644 --- a/receiver/src/main/resources/development/webclient.yml +++ b/receiver/src/main/resources/development/webclient.yml @@ -1,5 +1,5 @@ webclient: - base-url: http://localhost:20081 + init: true units: - - unit-name: processor-api + - unit-name: ppn-processor-api base-url: http://localhost:20081 \ No newline at end of file diff --git a/receiver/src/main/resources/production/webclient.yml b/receiver/src/main/resources/production/webclient.yml index 18322d9..aa3fb1b 100644 --- a/receiver/src/main/resources/production/webclient.yml +++ b/receiver/src/main/resources/production/webclient.yml @@ -1,5 +1,5 @@ webclient: - base-url: http://ppn_processor:20080 + init: true units: - - unit-name: processor-api + - unit-name: ppn-processor-api base-url: http://ppn_processor:20080 diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/SenderApplication.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/SenderApplication.java index e091164..20f8126 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/SenderApplication.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/SenderApplication.java @@ -2,6 +2,9 @@ package com.myoa.engineering.crawl.ppomppu.sender; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; + +import com.myoa.engineering.crawl.ppomppu.support.webclient.PpomppuNotifierWebClientConfiguration; /** * SenderApplication @@ -9,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @since 2021-08-20 * */ +@Import({ PpomppuNotifierWebClientConfiguration.class }) @SpringBootApplication public class SenderApplication { diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/infrastructure/client/SlackMessageSender.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/infrastructure/client/SlackMessageSender.java index ea0732b..1eef297 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/infrastructure/client/SlackMessageSender.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/infrastructure/client/SlackMessageSender.java @@ -25,7 +25,6 @@ public class SlackMessageSender implements MessageSender { private static final String SLACK_API_URL = "https://slack.com/api"; private final WebClient webClient; - private final String apiSecret; public SlackMessageSender(String apiSecret) { this.webClient = WebClient.builder() @@ -37,7 +36,6 @@ public class SlackMessageSender implements MessageSender { .filter(WebClientFilterFactory.logRequest()) .filter(WebClientFilterFactory.logResponse()) .build(); - this.apiSecret = apiSecret; } @Override diff --git a/support/build.gradle b/support/build.gradle index ae063c9..42415c5 100644 --- a/support/build.gradle +++ b/support/build.gradle @@ -1,12 +1,9 @@ dependencies { - runtimeOnly 'mysql:mysql-connector-java' compileOnly 'org.projectlombok:lombok' // https://projectreactor.io/docs/core/release/reference/#debug-activate - implementation 'org.springframework.boot:spring-boot-starter-webflux' - - annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.projectlombok:lombok' + implementation 'org.springframework.boot:spring-boot-starter-webflux' } diff --git a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/PpomppuNotifierWebClientConfiguration.java b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/PpomppuNotifierWebClientConfiguration.java new file mode 100644 index 0000000..b907331 --- /dev/null +++ b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/PpomppuNotifierWebClientConfiguration.java @@ -0,0 +1,19 @@ +package com.myoa.engineering.crawl.ppomppu.support.webclient; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * PpomppuNotifierWebClientConfiguration + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-11-18 + * + */ +@Configuration +@ConditionalOnProperty(value = "webclient.init", havingValue = "true") +@ComponentScan(basePackageClasses = WebClientBaseScan.class) +public class PpomppuNotifierWebClientConfiguration { + + +} diff --git a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/properties/WebClientProperties.java b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/properties/WebClientProperties.java new file mode 100644 index 0000000..64b89f8 --- /dev/null +++ b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/webclient/properties/WebClientProperties.java @@ -0,0 +1,44 @@ +package com.myoa.engineering.crawl.ppomppu.support.webclient.properties; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; + +import lombok.Data; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +/** + * WebClientPropertiesUnit + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-11-18 + * + */ +@NoArgsConstructor +@Setter +@Getter +@Component +@ConfigurationProperties(prefix = "webclient") +public class WebClientProperties { + + private List units = new ArrayList<>(); + + @Data + public static class WebClientPropertiesUnit { + private String unitName; + private String baseUrl; + // TODO headers + } + + public WebClientPropertiesUnit find(@NonNull String unitName) { + return units.stream() + .filter(x -> x.getUnitName().equals(unitName)) + .findFirst() + .orElseThrow( + () -> new IllegalArgumentException("Not found properties unit. unitName : " + unitName)); + } +}