com.gitee.summer9102.develop.alibaba.AliAutoConfiguration Maven / Gradle / Ivy
package com.gitee.summer9102.develop.alibaba;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.gitee.summer9102.develop.alibaba.elasticsearch.AliElasticsearch;
import com.gitee.summer9102.develop.alibaba.elasticsearch.AliElasticsearchProperties;
import com.gitee.summer9102.develop.alibaba.oss.AliOss;
import com.gitee.summer9102.develop.alibaba.oss.AliOssProperties;
import com.gitee.summer9102.develop.alibaba.umeng.Umeng;
import com.gitee.summer9102.develop.alibaba.umeng.UmengProperties;
import com.gitee.summer9102.develop.alibaba.vod.AliVod;
import com.gitee.summer9102.develop.alibaba.vod.AliVodProperties;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({AliOssProperties.class, AliVodProperties.class, AliElasticsearchProperties.class, UmengProperties.class})
public class AliAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "ali.oss.enabled", havingValue = "true")
public AliOss aliOss(AliOssProperties aliOssProperties) {
AliOss aliOss = new AliOss();
aliOss.setAliOssProperties(aliOssProperties);
CredentialsProvider provider = new DefaultCredentialProvider(aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());
OSSClient ossClient = new OSSClient(aliOssProperties.getEndpoint(), provider, null);
aliOss.setOssClient(ossClient);
IClientProfile profile = DefaultProfile.getProfile(aliOssProperties.getRegionId(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());
IAcsClient acsClient = new DefaultAcsClient(profile);
aliOss.setAcsClient(acsClient);
return aliOss;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "ali.vod.enabled", havingValue = "true")
public AliVod aliVod(AliVodProperties aliVodProperties) {
AliVod aliVod = new AliVod();
aliVod.setAliVodProperties(aliVodProperties);
DefaultProfile profile = DefaultProfile.getProfile(aliVodProperties.getRegionId(), aliVodProperties.getAccessKeyId(), aliVodProperties.getAccessKeySecret());
DefaultAcsClient acsClient = new DefaultAcsClient(profile);
aliVod.setAcsClient(acsClient);
return aliVod;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "ali.elasticsearch.enabled", havingValue = "true")
public AliElasticsearch aliElasticsearch(AliElasticsearchProperties aliElasticsearchProperties) {
AliElasticsearch aliElasticsearch = new AliElasticsearch();
aliElasticsearch.setAliElasticsearchProperties(aliElasticsearchProperties);
// 阿里云ES集群需要basic auth验证。
final org.apache.http.client.CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//访问用户名和密码为您创建阿里云Elasticsearch实例时设置的用户名和密码,也是Kibana控制台的登录用户名和密码。
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(aliElasticsearchProperties.getUsername(), aliElasticsearchProperties.getPassword()));
// 通过builder创建rest client,配置http client的HttpClientConfigCallback。
// 单击所创建的Elasticsearch实例ID,在基本信息页面获取公网地址,即为ES集群地址。
RestClientBuilder builder = RestClient.builder(new HttpHost(aliElasticsearchProperties.getHostname(), aliElasticsearchProperties.getPort()))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
// RestHighLevelClient实例通过REST low-level client builder进行构造。
RestHighLevelClient highClient = new RestHighLevelClient(builder);
aliElasticsearch.setHighClient(highClient);
return aliElasticsearch;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "umeng.api.enabled", havingValue = "true")
public Umeng umeng(UmengProperties umengProperties) {
Umeng umeng = new Umeng();
umeng.setUmengProperties(umengProperties);
ApiExecutor apiExecutor = new ApiExecutor(umengProperties.getKey(), umengProperties.getSecurity());
apiExecutor.setServerHost("gateway.open.umeng.com");
umeng.setApiExecutor(apiExecutor);
return umeng;
}
}