[PPN-211113] Implement spring config client

This commit is contained in:
woo-jin.shin
2021-11-17 09:39:49 +09:00
parent 0afa52aa53
commit 5b4b44f093
22 changed files with 332 additions and 36 deletions

23
sender/build.gradle Normal file
View File

@@ -0,0 +1,23 @@
dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
implementation project(':support')
// https://projectreactor.io/docs/core/release/reference/#debug-activate
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

View File

@@ -1,10 +1,18 @@
package com.myoa.engineering.crawl.ppomppu.sender;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SenderApplication
* @author Shin Woo-jin (woo-jin.shin@linecorp.com)
* @since 2021-08-20
*
*/
@SpringBootApplication
public class SenderApplication {
public static void main(String[] args) {
SpringApplication.run(SenderApplication.class, args);
}
}

View File

@@ -0,0 +1,37 @@
package com.myoa.engineering.crawl.ppomppu.sender.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackMessageDTO;
import com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client.SlackMessageSender;
import reactor.core.publisher.Mono;
/**
* TestAPIController
* @author Shin Woo-jin (woo-jin.shin@linecorp.com)
* @since 2021-11-15
*
*/
@RestController
@RequestMapping("/api/v1")
public class TestAPIController {
private final SlackMessageSender sender;
public TestAPIController() {
this.sender = new SlackMessageSender("xoxb-2688454277126-2695026012277-K2Ib13lKokmTiBSnSMrc0Bp2");
}
@GetMapping("/test")
public Mono<String> test() {
return sender.sendMessage(SlackMessageDTO.builder()
.text("test!")
.iconEmoji("monge")
.channel("notify_shopping")
.username("shopping notifier")
.build());
}
}

View File

@@ -0,0 +1,12 @@
package com.myoa.engineering.crawl.ppomppu.sender.dto;
import java.io.Serializable;
/**
* MessageDTO
* @author Shin Woo-jin (woo-jin.shin@linecorp.com)
* @since 2021-11-14
*
*/
public interface MessageDTO extends Serializable {
}

View File

@@ -0,0 +1,36 @@
package com.myoa.engineering.crawl.ppomppu.sender.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* SlackMessageDTO
* @author Shin Woo-jin (woo-jin.shin@linecorp.com)
* @since 2021-11-14
*
*/
@Getter
@NoArgsConstructor
public class SlackMessageDTO implements MessageDTO {
private final static long serialVersionUID = 4737608709660494713L;
private String text;
private String channel;
private String username;
@JsonProperty("icon_emoji")
private String iconEmoji;
@Builder
public SlackMessageDTO(String text, String channel, String username, String iconEmoji) {
this.text = text;
this.channel = channel;
this.username = username;
this.iconEmoji = iconEmoji;
}
}

View File

@@ -0,0 +1,17 @@
package com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client;
import com.myoa.engineering.crawl.ppomppu.sender.dto.MessageDTO;
import reactor.core.publisher.Mono;
/**
* MessageSender
* @author Shin Woo-jin (woo-jin.shin@linecorp.com)
* @since 2021-11-14
*
*/
public interface MessageSender<T extends MessageDTO> {
Mono<String> sendMessage(T message);
}

View File

@@ -0,0 +1,88 @@
package com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackMessageDTO;
import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebClientFilterFactory;
import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebFluxExchangeStragiesFactory;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
/**
* SlackMessageSender
*
* @author Shin Woo-jin (woozu.shin@kakaoent.com)
* @since 2021-09-08
*/
@Slf4j
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()
.baseUrl(SLACK_API_URL)
.defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiSecret)
.exchangeStrategies(WebFluxExchangeStragiesFactory.ofDefault())
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.defaultHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8")
.filter(WebClientFilterFactory.logRequest())
.filter(WebClientFilterFactory.logResponse())
.build();
this.apiSecret = apiSecret;
}
@Override
public Mono<String> sendMessage(SlackMessageDTO message) {
return webClient.post()
.uri("/chat.postMessage")
.bodyValue(message)
.exchangeToMono(e -> e.bodyToMono(String.class))
.publishOn(Schedulers.boundedElastic())
.onErrorResume(WebClientRequestException.class, t -> {
log.info("Exception occured, ignoring. : {}", t.getClass().getSimpleName());
return Mono.empty();
})
.doOnNext(e -> log.info("response: {}", e));
}
}
/*
import requests
import json
# woozuyeni_watchtower
SLACK_TOKEN="xoxb-2688454277126-2695026012277-K2Ib13lKokmTiBSnSMrc0Bp2"
CHANNEL = "notify_watchtower"
CHANNEL = "common"
POST_API = "https://slack.com/api/chat.postMessage"
HEADER = "Authorization: Bearer"
headers = {
"Content-Type" : "application/json",
"Authorization" : f"Bearer {SLACK_TOKEN}"
}
data = {}
data["text"] = "몽몽! :monge_big:"
data["channel"] = CHANNEL
data["username"] = "몽이봇"
data["icon_emoji"] = ":monge_big:"
# requests.post(POST_API, data=json.dumps(data), headers=headers)
attachments = [{"title": "Cat", "image_url": image_url}]
image_url = "https://myoa-universe.com/assets/love1.png"
*/

View File

@@ -0,0 +1,11 @@
spring:
config:
activate:
on-profile: development
import:
- classpath:/development/webclient.yml
- "configserver:http://192.168.0.100:11080"
# import: optional:configserver:http://localhost:11080 # can be start up even config server was not found.
profiles:
group:
development: development, slackapi-development

View File

@@ -0,0 +1,6 @@
spring:
config:
activate:
on-profile: production
# import:
# - classpath:/production/webclient.yml

View File

@@ -0,0 +1,19 @@
spring:
application:
# name: ppn-sender
name: app1
main:
allow-bean-definition-overriding: true
profiles:
active: development
freemarker:
enabled: false
server:
port: 20080
management:
endpoints:
web:
exposure:
include: refresh

View File

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