Skip to content

Commit 59438be

Browse files
committed
init oceanengine-spring-boot-starter
1 parent c66c89c commit 59438be

File tree

7 files changed

+312
-0
lines changed

7 files changed

+312
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>io.github.hyq0719</groupId>
7+
<artifactId>marketing-api-spring-boot-starters</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>oceanengine-spring-boot-starter</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
<name>Marketing API - Spring Boot Starter for Ocean Engine</name>
15+
<description>巨量引擎 Spring Boot Starter</description>
16+
17+
<properties>
18+
<maven.compiler.source>${java.version}</maven.compiler.source>
19+
<maven.compiler.target>${java.version}</maven.compiler.target>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>io.github.hyq0719</groupId>
25+
<artifactId>marketing-api-oceanengine</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hyq0719.spring.starter.mktapi.oceanengine.config;
2+
3+
import cn.hutool.cron.CronUtil;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.DisposableBean;
6+
import org.springframework.beans.factory.InitializingBean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
@Slf4j
11+
public class CronJobAutoConfiguration implements InitializingBean, DisposableBean {
12+
13+
14+
@Override
15+
public void destroy() throws Exception {
16+
try {
17+
CronUtil.stop();
18+
} catch (Exception e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
23+
@Override
24+
public void afterPropertiesSet() throws Exception {
25+
log.info("[CRONJOB] TOKEN REFRESH JOB START");
26+
try {
27+
CronUtil.start();
28+
} catch (Exception e) {
29+
e.printStackTrace();
30+
}
31+
log.info("[CRONJOB] TOKEN REFRESH JOB START FINISH");
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.hyq0719.spring.starter.mktapi.oceanengine.config;
2+
3+
import com.hyq0719.mktapi.common.executor.http.ApacheHttpHandler;
4+
import com.hyq0719.mktapi.common.executor.http.OkhttpHttpHandler;
5+
import com.hyq0719.spring.starter.mktapi.oceanengine.properties.SdkProperties;
6+
import lombok.Data;
7+
import lombok.extern.slf4j.Slf4j;
8+
import okhttp3.ConnectionPool;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.OkHttpClient.Builder;
11+
import org.apache.http.client.config.RequestConfig;
12+
import org.apache.http.impl.client.CloseableHttpClient;
13+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
14+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
15+
import org.springframework.context.annotation.Bean;
16+
17+
import java.util.concurrent.TimeUnit;
18+
19+
@EnableConfigurationProperties(SdkProperties.class)
20+
@Data
21+
@Slf4j
22+
public class HttpAutoConfiguration {
23+
24+
@Bean
25+
@ConditionalOnMissingBean
26+
public OkhttpHttpHandler okhttpRequestHandler(OkHttpClient httpClients) {
27+
return new OkhttpHttpHandler(httpClients);
28+
}
29+
30+
@Bean
31+
@ConditionalOnMissingBean
32+
public ApacheHttpHandler httpClientRequestHandler(CloseableHttpClient httpClient,
33+
RequestConfig requestConfig) {
34+
return new ApacheHttpHandler(httpClient, requestConfig);
35+
}
36+
37+
@Bean
38+
@ConditionalOnMissingBean
39+
public OkHttpClient okHttpClient(SdkProperties sdkProperties) {
40+
validateProperties(sdkProperties);
41+
log.info("开始构建httpClients");
42+
Builder builder = new Builder();
43+
builder.connectTimeout(sdkProperties.getNet().getConnectTimeout(), TimeUnit.SECONDS);
44+
builder.readTimeout(sdkProperties.getNet().getReadTimeout(), TimeUnit.SECONDS);
45+
builder.writeTimeout(sdkProperties.getNet().getWriteTimeout(), TimeUnit.SECONDS);
46+
builder.connectionPool(new ConnectionPool(sdkProperties.getNet().getConnectionPool().getMaxIdleConnections(),
47+
sdkProperties.getNet().getConnectionPool().getKeepAliveDuration(), TimeUnit.MINUTES));
48+
log.info("结束构建httpClients");
49+
return builder.build();
50+
}
51+
52+
private void validateProperties(SdkProperties sdkProperties) {
53+
if (sdkProperties == null) {
54+
throw new RuntimeException("sdkProperties is null");
55+
}
56+
SdkProperties.NetConfig net = sdkProperties.getNet();
57+
if (net == null) {
58+
throw new RuntimeException("sdkProperties -> NetConfig is null");
59+
}
60+
if (net.getConnectionPool() == null) {
61+
throw new RuntimeException("sdkProperties -> NetConfig -> ConnectionPool is null");
62+
}
63+
if (net.getConnectionPool() == null) {
64+
throw new RuntimeException("sdkProperties -> NetConfig -> ConnectionPool is null");
65+
}
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.hyq0719.spring.starter.mktapi.oceanengine.config;
2+
3+
import com.hyq0719.mktapi.common.executor.http.OkhttpHttpHandler;
4+
import com.hyq0719.mktapi.common.token.ITokenCronService;
5+
import com.hyq0719.mktapi.common.token.cache.ITokenLocalCache;
6+
import com.hyq0719.mktapi.oceanengine.OceanApiClient;
7+
import com.hyq0719.mktapi.oceanengine.OceanRetryStrategy;
8+
import com.hyq0719.mktapi.oceanengine.service.OceanSdkService;
9+
import com.hyq0719.mktapi.oceanengine.token.OceanExternalTokenService;
10+
import com.hyq0719.spring.starter.mktapi.oceanengine.properties.SdkProperties;
11+
import lombok.Data;
12+
import lombok.extern.slf4j.Slf4j;
13+
import org.apache.commons.lang3.StringUtils;
14+
import org.springframework.boot.CommandLineRunner;
15+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
16+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
17+
import org.springframework.context.annotation.Bean;
18+
import org.springframework.context.annotation.Configuration;
19+
import org.springframework.context.annotation.Import;
20+
21+
@Configuration
22+
@EnableConfigurationProperties(SdkProperties.class)
23+
@Import(HttpAutoConfiguration.class)
24+
@Data
25+
@Slf4j
26+
public class OceanAutoConfiguration implements CommandLineRunner {
27+
28+
private ITokenCronService oceanTrigger;
29+
30+
@Bean
31+
@ConditionalOnMissingBean
32+
public OceanSdkService oceanSdkService(OceanApiClient oceanApiClient, OceanRetryStrategy oceanRetryStrategy) {
33+
log.info("SDK bulid oceanSdkService, oceanApiClient:{},oceanRetryStrategy:{}", oceanApiClient, oceanRetryStrategy);
34+
return new OceanSdkService(oceanApiClient, oceanRetryStrategy);
35+
}
36+
37+
@Bean
38+
public OceanRetryStrategy oceanRetryStrategy(SdkProperties sdkProperties) {
39+
OceanRetryStrategy oceanRetryStrategy = new OceanRetryStrategy();
40+
SdkProperties.ChannelConfig oceanengine = sdkProperties.getOceanengine();
41+
if (oceanengine == null) {
42+
throw new RuntimeException("oceanengine properties is null");
43+
}
44+
oceanRetryStrategy.setRetryCount(oceanengine.getRetryCount());
45+
oceanRetryStrategy.setEnable(oceanengine.getEnableRetry());
46+
return oceanRetryStrategy;
47+
}
48+
49+
@Bean
50+
public ITokenLocalCache oceanCache() {
51+
return new ITokenLocalCache();
52+
}
53+
54+
@Bean
55+
@ConditionalOnMissingBean
56+
public OceanApiClient oceanApiClient(OkhttpHttpHandler httpsClient, OceanExternalTokenService oceanExternalTokenService) {
57+
if (httpsClient == null) {
58+
throw new RuntimeException("oceanengine RequestHandler is null");
59+
}
60+
if (oceanExternalTokenService == null) {
61+
throw new RuntimeException("OceanExternalTokenService is null");
62+
}
63+
return new OceanApiClient(oceanCache(), httpsClient, oceanExternalTokenService);
64+
}
65+
66+
@Bean
67+
public ITokenCronService oceanCronService(OceanExternalTokenService oceanExternalTokenService, SdkProperties sdkProperties) {
68+
String cron = sdkProperties.getOceanengine().getCron();
69+
if (StringUtils.isEmpty(cron)) {
70+
throw new RuntimeException("oceanengine cron is null");
71+
}
72+
ITokenCronService simpleCronService = new ITokenCronService(oceanExternalTokenService, oceanCache(),
73+
sdkProperties.getOceanengine().getCron());
74+
simpleCronService.run(); // 注册定时器
75+
oceanTrigger = simpleCronService;
76+
return simpleCronService;
77+
}
78+
79+
@Override
80+
public void run(String... args) {
81+
log.info("oceanengine Trigger obj:{}", oceanTrigger);
82+
try {
83+
oceanTrigger.trigger();
84+
} catch (Exception e) {
85+
e.printStackTrace();
86+
log.info("oceanengine sdk start run error:{}", e.getMessage());
87+
}
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.hyq0719.spring.starter.mktapi.oceanengine.properties;
2+
3+
4+
import lombok.Data;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
7+
@Data
8+
@ConfigurationProperties("mktapi.sdk")
9+
public class SdkProperties {
10+
private NetConfig net; // 网络配置
11+
private ChannelConfig oceanengine; // 巨量引擎
12+
13+
@Data
14+
public static class NetConfig {
15+
private Integer connectTimeout = 20;
16+
private Integer readTimeout = 20;
17+
private Integer writeTimeout = 20;
18+
private ConnectionPool connectionPool;
19+
}
20+
21+
@Data
22+
public static class ConnectionPool {
23+
private Integer maxIdleConnections = 12;
24+
private Integer keepAliveDuration = 8;
25+
}
26+
27+
@Data
28+
public static class ChannelConfig {
29+
private Boolean enableRetry = true;
30+
private Integer retryCount = 3;
31+
private String cron;
32+
}
33+
34+
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Auto Configure
2+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
3+
com.hyq0719.spring.starter.mktapi.oceanengine.config.HttpAutoConfiguration,\
4+
com.hyq0719.spring.starter.mktapi.oceanengine.config.OceanAutoConfiguration,\
5+
com.hyq0719.spring.starter.mktapi.oceanengine.config.CronJobAutoConfiguration

spring-boot-starters/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.github.hyq0719</groupId>
9+
<artifactId>marketing-api-sdk</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
<packaging>pom</packaging>
13+
14+
<artifactId>marketing-api-spring-boot-starters</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
16+
<name>Marketing API - Spring Boot Starters</name>
17+
<description>Marketing API 各个模块的 Spring Boot Starter</description>
18+
19+
<modules>
20+
<module>kuaishou-spring-boot-starter</module>
21+
<module>oceanengine-spring-boot-starter</module>
22+
<module>tencent-spring-boot-starter</module>
23+
<module>vivo-spring-boot-starter</module>
24+
<module>production-spring-boot-starter</module>
25+
</modules>
26+
27+
<properties>
28+
<maven.compiler.source>${java.version}</maven.compiler.source>
29+
<maven.compiler.target>${java.version}</maven.compiler.target>
30+
<spring.boot.version>2.0.1.RELEASE</spring.boot.version>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter</artifactId>
37+
<version>${spring.boot.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-autoconfigure</artifactId>
42+
<version>${spring.boot.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-configuration-processor</artifactId>
47+
<version>${spring.boot.version}</version>
48+
<optional>true</optional>
49+
</dependency>
50+
</dependencies>
51+
52+
</project>

0 commit comments

Comments
 (0)