参考: https://www.cnblogs.com/hello-shf/p/10864977.html

使用场景

springboot的诸多starter简化了配置, 方便了使用, 在日常项目中, 若对某个技术栈有比较完善的二开和封装, 可以将相关配置提取封装成自定义的starter, 并抽取模板方法供其他模块或项目调用.

springboot-starter原理

先回顾下springboot-starter的原理:

  1. SpringBoot的@SpringBootApplication注解, 其中包含了@EnableAutoConfiguration自动配置注解, @EnableAutoConfiguration导入了AutoConfigurationImportSelector.class配置文件导入的选择器
  2. AutoConfigurationImportSelector会根据spring.factories中指定的配置类的位置去加载默认配置, 这就达到了自动配置的目的
  3. 加载配置时会有@ConditionOnClass注解做条件控制, 若引入了某个starter使@ConditionOnClass条件成立, 则会加载该starter对应的配置, 以达到starter的自动装配

示例: elasticsearch-spring-boot-starter

这里以es的封装为例
实现步骤:

elasticsearch-spring-boot-starter模块

依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>demo-search</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>elasticsearch-spring-boot-starter</artifactId>
  12. <dependencies>
  13. <!--es-->
  14. <dependency>
  15. <groupId>org.elasticsearch.client</groupId>
  16. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  17. <version>7.2.0</version>
  18. </dependency>
  19. <!--使用xmlproperty配置-->
  20. <!-- <dependency>-->
  21. <!-- <groupId>org.springframework.boot</groupId>-->
  22. <!-- <artifactId>spring-boot-configuration-processor</artifactId>-->
  23. <!-- <optional>true</optional>-->
  24. <!-- <version>2.1.3.RELEASE</version>-->
  25. <!-- </dependency>-->
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter</artifactId>
  29. <version>2.1.3.RELEASE</version>
  30. </dependency>
  31. </dependencies>
  32. </project>

引入es配置

ElasticsearchConfigProperty

  1. @ConfigurationProperties(prefix = "elasticsearch")
  2. public class ElasticsearchConfigProperty {
  3. private String httpHost;
  4. private Integer connectionTimeOut;
  5. private Integer socketTimeOut;
  6. private Integer connectionRequestTimeOut;
  7. public String getHttpHost() {
  8. return httpHost;
  9. }
  10. public void setHttpHost(String httpHost) {
  11. this.httpHost = httpHost;
  12. }
  13. public Integer getConnectionTimeOut() {
  14. return connectionTimeOut;
  15. }
  16. public void setConnectionTimeOut(Integer connectionTimeOut) {
  17. this.connectionTimeOut = connectionTimeOut;
  18. }
  19. public Integer getSocketTimeOut() {
  20. return socketTimeOut;
  21. }
  22. public void setSocketTimeOut(Integer socketTimeOut) {
  23. this.socketTimeOut = socketTimeOut;
  24. }
  25. public Integer getConnectionRequestTimeOut() {
  26. return connectionRequestTimeOut;
  27. }
  28. public void setConnectionRequestTimeOut(Integer connectionRequestTimeOut) {
  29. this.connectionRequestTimeOut = connectionRequestTimeOut;
  30. }
  31. }

ElasticsearchConfig

  1. @Configuration
  2. @EnableConfigurationProperties(ElasticsearchConfigProperty.class)
  3. @ConditionalOnProperty(prefix = "elasticsearch", name = "enable", havingValue = "true")
  4. public class ElasticsearchConfig {
  5. @Autowired
  6. private ElasticsearchConfigProperty elasticsearchConfigProperty;
  7. @Bean
  8. public RestHighLevelClient restHighLevelClient() {
  9. RestClientBuilder builder = RestClient.builder(HttpHost.create(elasticsearchConfigProperty.getHttpHost()));
  10. builder.setRequestConfigCallback(requestConfigBuilder -> {
  11. if(elasticsearchConfigProperty.getConnectionTimeOut()!=null){
  12. requestConfigBuilder.setConnectTimeout(elasticsearchConfigProperty.getConnectionTimeOut());
  13. }
  14. if(elasticsearchConfigProperty.getSocketTimeOut()!=null){
  15. requestConfigBuilder.setSocketTimeout(elasticsearchConfigProperty.getSocketTimeOut());
  16. }
  17. if(elasticsearchConfigProperty.getConnectionRequestTimeOut()!=null){
  18. requestConfigBuilder.setConnectionRequestTimeout(elasticsearchConfigProperty.getConnectionRequestTimeOut());
  19. }
  20. return requestConfigBuilder;
  21. });
  22. return new RestHighLevelClient(builder);
  23. }
  24. }

提供ElasticsearchTemplate模板接口

  1. public interface ElasticsearchTemplate {
  2. SearchResponse search(String index, int page, int pageSize, SearchSourceBuilder queryBuilder)
  3. throws IOException;
  4. }
  1. @Service
  2. public class ElasticsearchTemplateImpl implements ElasticsearchTemplate {
  3. @Autowired
  4. private RestHighLevelClient restHighLevelClient;
  5. @Override
  6. public SearchResponse search(String index, int page, int pageSize, SearchSourceBuilder sourceBuilder)
  7. throws IOException {
  8. sourceBuilder.from((page - 1) * pageSize).size(pageSize);
  9. SearchRequest request = new SearchRequest(index);
  10. request.source(sourceBuilder);
  11. return restHighLevelClient.search(request, RequestOptions.DEFAULT);
  12. }
  13. }

注册配置

在resources/META-INF下建文件spring.factories

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. org.example.elasticsearch.spring.boot.starter.config.ElasticsearchConfig

调用示例: demo-search-service

依赖及配置

  1. <dependency>
  2. <groupId>org.example</groupId>
  3. <artifactId>elasticsearch-spring-boot-starter</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>
  1. elasticsearch:
  2. enable: true
  3. httpHost: http://39.106.55.179:40009

使用

image.png