[PPN-211113] Implement ConfigurationProperties
This commit is contained in:
parent
24a848dc9f
commit
bbf4affc16
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -1,5 +1,5 @@
|
|||
webclient:
|
||||
some: test
|
||||
init: true
|
||||
units:
|
||||
- unit-name: processor-api
|
||||
- unit-name: ppn-sender-api
|
||||
base-url: http://localhost:20081
|
|
@ -1,4 +1,5 @@
|
|||
webclient:
|
||||
init: true
|
||||
units:
|
||||
- unit-name: processor-api
|
||||
base-url: http://soundhoundfound-processor:20080
|
||||
- unit-name: ppn-sender-api
|
||||
base-url: http://ppn_sender:20081
|
|
@ -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'
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<MessageHandler> 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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<TelegramBotPropertiesUnit> 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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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,9 +21,13 @@ public class ProcessorAPIWebClient {
|
|||
|
||||
private final WebClient webClient;
|
||||
|
||||
public ProcessorAPIWebClient(WebClient.Builder webClientBuilder,
|
||||
@Value("${webclient.base-url}") String baseUrl) {
|
||||
this.webClient = webClientBuilder.baseUrl(baseUrl)
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -4,3 +4,4 @@ spring:
|
|||
on-profile: development
|
||||
import:
|
||||
- classpath:/development/webclient.yml
|
||||
- "configserver:http://192.168.0.100:11080"
|
|
@ -4,3 +4,4 @@ spring:
|
|||
on-profile: production
|
||||
import:
|
||||
- classpath:/production/webclient.yml
|
||||
- "configserver:http://192.168.0.100:11080"
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
|
@ -25,7 +25,6 @@ public class SlackMessageSender implements MessageSender<SlackMessageDTO> {
|
|||
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<SlackMessageDTO> {
|
|||
.filter(WebClientFilterFactory.logRequest())
|
||||
.filter(WebClientFilterFactory.logResponse())
|
||||
.build();
|
||||
this.apiSecret = apiSecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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'
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue