[PPN-211113] Implement ConfigurationProperties

This commit is contained in:
woo-jin.shin 2021-11-18 00:38:16 +09:00
parent 24a848dc9f
commit bbf4affc16
20 changed files with 206 additions and 30 deletions

View File

@ -2,6 +2,9 @@ package com.myoa.engineering.crawl.ppomppu.processor;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import com.myoa.engineering.crawl.ppomppu.support.webclient.PpomppuNotifierWebClientConfiguration;
/** /**
* ProcessorApplication * ProcessorApplication
@ -9,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @since 2021-08-20 * @since 2021-08-20
* *
*/ */
@Import({ PpomppuNotifierWebClientConfiguration.class })
@SpringBootApplication @SpringBootApplication
public class ProcessorApplication { public class ProcessorApplication {

View File

@ -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;
}

View File

@ -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

View File

@ -1,5 +1,5 @@
webclient: webclient:
some: test init: true
units: units:
- unit-name: processor-api - unit-name: ppn-sender-api
base-url: http://localhost:20081 base-url: http://localhost:20081

View File

@ -1,4 +1,5 @@
webclient: webclient:
init: true
units: units:
- unit-name: processor-api - unit-name: ppn-sender-api
base-url: http://soundhoundfound-processor:20080 base-url: http://ppn_sender:20081

View File

@ -8,6 +8,7 @@ dependencies {
// https://projectreactor.io/docs/core/release/reference/#debug-activate // https://projectreactor.io/docs/core/release/reference/#debug-activate
implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-configuration-processor' implementation 'org.springframework.boot:spring-boot-configuration-processor'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.telegram:telegrambots:5.3.0' implementation 'org.telegram:telegrambots:5.3.0'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

View File

@ -3,8 +3,10 @@ package com.myoa.engineering.crawl.ppomppu.receiver;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.receiver.configuration.properties.TelegramBotProperties;
import com.myoa.engineering.crawl.ppomppu.support.webclient.PpomppuNotifierWebClientConfiguration;
/** /**
* ReceiverApplication * ReceiverApplication
@ -12,8 +14,9 @@ import com.myoa.engineering.crawl.ppomppu.receiver.configuration.properties.Tele
* @since 2021-08-20 * @since 2021-08-20
* *
*/ */
@Import({ PpomppuNotifierWebClientConfiguration.class})
@SpringBootApplication @SpringBootApplication
@EnableConfigurationProperties({ TelegramBotProperties.class }) // @EnableConfigurationProperties({ TelegramBotProperties.class })
public class ReceiverApplication { public class ReceiverApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -9,6 +9,7 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; 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;
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.dispatch.MessageDispatcher;
import com.myoa.engineering.crawl.ppomppu.receiver.handler.message.MessageHandler; 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 @Configuration
public class TelegramBotConfiguration { public class TelegramBotConfiguration {
private static final String BOT_PROPERTIES_UNIT_NAME = "ppomppu_notify_bot";
@Bean @Bean
public TelegramBotsApi telegramBotsApi(MessageDispatcher messageDispatcher) throws TelegramApiException { public TelegramBotsApi telegramBotsApi(MessageDispatcher messageDispatcher) throws TelegramApiException {
TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class); TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class);
@ -30,7 +32,8 @@ public class TelegramBotConfiguration {
@Bean @Bean
public MessageDispatcher messageDispatcher(List<MessageHandler> messageHandlers, public MessageDispatcher messageDispatcher(List<MessageHandler> messageHandlers,
TelegramBotProperties botProperties) { TelegramBotProperties telegramBotProperties) {
return new MessageDispatcher(messageHandlers, botProperties.getName(), botProperties.getToken()); TelegramBotPropertiesUnit propertiesUnit = telegramBotProperties.find(BOT_PROPERTIES_UNIT_NAME);
return new MessageDispatcher(messageHandlers, propertiesUnit.getName(), propertiesUnit.getToken());
} }
} }

View File

@ -1,9 +1,17 @@
package com.myoa.engineering.crawl.ppomppu.receiver.configuration.properties; 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.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding; import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/** /**
* TelegramBotProperties * TelegramBotProperties
@ -11,15 +19,26 @@ import lombok.Getter;
* @since 2021-09-05 * @since 2021-09-05
* *
*/ */
@Component
@Setter
@Getter @Getter
@ConstructorBinding @ConfigurationProperties(prefix = "infra.telegram.bot")
@ConfigurationProperties(prefix = "telegram.bot")
public class TelegramBotProperties { public class TelegramBotProperties {
private final String name;
private final String token;
public TelegramBotProperties(final String name, final String token) { private List<TelegramBotPropertiesUnit> units = new ArrayList<>();
this.name = name;
this.token = token; @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));
} }
} }

View File

@ -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;
}

View File

@ -1,12 +1,15 @@
package com.myoa.engineering.crawl.ppomppu.receiver.infrastructure.client; package com.myoa.engineering.crawl.ppomppu.receiver.infrastructure.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException; 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.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 lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -18,10 +21,14 @@ public class ProcessorAPIWebClient {
private final WebClient webClient; private final WebClient webClient;
public ProcessorAPIWebClient(WebClient.Builder webClientBuilder, public ProcessorAPIWebClient(WebClientProperties webClientProperties) {
@Value("${webclient.base-url}") String baseUrl) { WebClientPropertiesUnit webClientPropertiesUnit =
this.webClient = webClientBuilder.baseUrl(baseUrl) webClientProperties.find(WebClientPropertiesUnitName.PPOMPPU_NOTIFIER_PROCESSOR_API.getUnitName());
.build(); this.webClient = WebClient.builder()
.baseUrl(webClientPropertiesUnit.getBaseUrl())
.filter(WebClientFilterFactory.logRequest())
.filter(WebClientFilterFactory.logResponse())
.build();
} }
public Mono<String> emitParseEvent(PpomppuBoardName boardName) { public Mono<String> emitParseEvent(PpomppuBoardName boardName) {

View File

@ -4,3 +4,4 @@ spring:
on-profile: development on-profile: development
import: import:
- classpath:/development/webclient.yml - classpath:/development/webclient.yml
- "configserver:http://192.168.0.100:11080"

View File

@ -4,3 +4,4 @@ spring:
on-profile: production on-profile: production
import: import:
- classpath:/production/webclient.yml - classpath:/production/webclient.yml
- "configserver:http://192.168.0.100:11080"

View File

@ -1,5 +1,5 @@
webclient: webclient:
base-url: http://localhost:20081 init: true
units: units:
- unit-name: processor-api - unit-name: ppn-processor-api
base-url: http://localhost:20081 base-url: http://localhost:20081

View File

@ -1,5 +1,5 @@
webclient: webclient:
base-url: http://ppn_processor:20080 init: true
units: units:
- unit-name: processor-api - unit-name: ppn-processor-api
base-url: http://ppn_processor:20080 base-url: http://ppn_processor:20080

View File

@ -2,6 +2,9 @@ package com.myoa.engineering.crawl.ppomppu.sender;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import com.myoa.engineering.crawl.ppomppu.support.webclient.PpomppuNotifierWebClientConfiguration;
/** /**
* SenderApplication * SenderApplication
@ -9,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @since 2021-08-20 * @since 2021-08-20
* *
*/ */
@Import({ PpomppuNotifierWebClientConfiguration.class })
@SpringBootApplication @SpringBootApplication
public class SenderApplication { public class SenderApplication {

View File

@ -25,7 +25,6 @@ public class SlackMessageSender implements MessageSender<SlackMessageDTO> {
private static final String SLACK_API_URL = "https://slack.com/api"; private static final String SLACK_API_URL = "https://slack.com/api";
private final WebClient webClient; private final WebClient webClient;
private final String apiSecret;
public SlackMessageSender(String apiSecret) { public SlackMessageSender(String apiSecret) {
this.webClient = WebClient.builder() this.webClient = WebClient.builder()
@ -37,7 +36,6 @@ public class SlackMessageSender implements MessageSender<SlackMessageDTO> {
.filter(WebClientFilterFactory.logRequest()) .filter(WebClientFilterFactory.logRequest())
.filter(WebClientFilterFactory.logResponse()) .filter(WebClientFilterFactory.logResponse())
.build(); .build();
this.apiSecret = apiSecret;
} }
@Override @Override

View File

@ -1,12 +1,9 @@
dependencies { dependencies {
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
// https://projectreactor.io/docs/core/release/reference/#debug-activate // 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' annotationProcessor 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
} }

View File

@ -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 {
}

View File

@ -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<WebClientPropertiesUnit> 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));
}
}