【Spring Boot && Spring Cloud系列】Spring Boot的启动器Starter

Spring Boot的内置Servlet Container:

Name  Servlet VersionJava Version
Tomcat83.1Java 7+
Tomcat73.0Java 6+
Jetty 9.33.1Java 8+
Jetty9.23.1Java7+
Jetty83.0Java 6+
Undertow1.33.1Java 7+

 Depency Management:

每个版本的Spring Boot都提供了一个依赖列表,这个列表包含了你可能通过Spring Boot用到的所有的Spring模块以及第三方库,该列表可以以BOM的形式支持Maven

Bom和Starter的区别

1、集成spring-boot-starter-parent

spring-boot-starter-parent project默认是Java 1.6.Resource filtering.exec plugin、surefire、Git Commit ID、shade.

Resource filtering for application.properties 和 application.yml,以及,profile-specific file,如application-foo.properties 或 application-foo.yml。

如果不想使用Spring Boot中的默认版本,可以在<properties>覆盖相应的版本,如,想使用不同版本的Spring Data,具体如下:

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
<!-- 使用 java 1.8 -->
<java.version>1.8</java.version>
</properties>

2、不继承spring-boot-starter-parent,这个时候可以使用depency management但是不能使用plugin management

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.4.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注意:scope是import,而且这种情况下,不允许在<properties>覆盖相应的版本。如果要使用其他版本,需要在上面的前面添加一个完整的depency:

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.4.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

除此之外,Spring Boot还提供了一个Maven plugin:spring-boot-maven-plugin用于将项目打包成fat jar:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

自动配置:

@Import 和 @ComponentScan 类似;
@EnableAutoConfiguration 和 @SpringBootApplication 类似;---注意,只能使用一次,建议用在primary @Configuration class上。
 
注意,自动配置永远是第二位的,一旦你配置自己的东西,那自动配置的就会被覆盖。
查看自动配置都配置了什么,以及为什么,启动应用的时候加上 --debug即可。
禁用特定的自动配置:
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

如果class不在classpath中,可以使用excludeName,然后使用全路径即可。

@SpringBootApplication 等同于默认的属性的
   -- @Configuration,
   --@EnableAutoConfiguration and 
 --@ComponentScan。 
-- 注意,@ComponentScan 不能凭空使用。

使用Spring Boot在pom.xml中的配置解释:

1、spring-boot-starter

     Spring Boot的核心启动器,包含了自动配置、日志和YAML

2、spring-boot-starter-actuator

     帮助监控和管理应用

3、spring-boot-starter-amqp

    通过spring-rabbit来支持AMQP协议(Advanced Message Queuing Protocol)

4、spring-boot-starter-aop

   支持面向方面的编程即AOP。包括spring-aop和AspectJ

5、spring-boot-starter-artemis

   通过Apache Artemis支持JMS的API(Java Message Service API)

6、spring-boot-starter-batch

   支持Spring Batch,包括HSQLDB数据库

7、spring-boot-starter-cache

   支持Spring的Cache抽象

8、spring-boot-starter-cloud-connectors

   支持Spring Cloud Connectors,简化了在像Cloud Foundry或Heroku这样的云平台上的连接服务

9、spring-boot-starter-data-elasticsearch

   支持ElasticSearch搜索和分析引擎,包括spring-data-elasticsearch

10、spring-boot-starter-data-genfire

  支持GemFire分布式数据存储,包括spring-data-gemfire

11、spring-boot-starter-data-jpa

  支持JPA(Java Persistence API),包括spring-data-jpa  spring-orm Hibernate

12、spring-boot-starter-data-mongodb

  支持MongoDB数据库,包括spring-data-mpngodb

13、spring-boot-starter-data-rest

  通过spring-data-rest-webmvc,支持通过RESR暴露Spring Data数据仓库

14、spring-boot-starter-data-solr 
   支持Apache Solr搜索平台,包括spring-data-solr。

15、spring-boot-starter-freemarker 
   支持FreeMarker模板引擎。

16、spring-boot-starter-groovy-templates 
   支持Groovy模板引擎。

17、spring-boot-starter-hateoas 
   通过spring-hateoas支持基于HATEOAS的RESTful Web服务。

18、spring-boot-starter-hornetq 
   通过HornetQ支持JMS。

19、spring-boot-starter-integration 
   支持通用的spring-integration模块。

20、spring-boot-starter-jdbc 
   支持JDBC数据库。

21、spring-boot-starter-jersey 
   支持Jersey RESTful Web服务框架。

22、spring-boot-starter-jta-atomikos 
   通过Atomikos支持JTA分布式事务处理。

23、spring-boot-starter-jta-bitronix 
   通过Bitronix支持JTA分布式事务处理。

24、spring-boot-starter-mail 
   支持javax.mail模块。

25、spring-boot-starter-mobile 
    支持spring-mobile。

26、spring-boot-starter-mustache 
   支持Mustache模板引擎。

27、spring-boot-starter-redis 
    支持Redis键值存储数据库,包括spring-redis。

28、spring-boot-starter-security 
    支持spring-security。

29、spring-boot-starter-social-facebook 
   支持spring-social-facebook

30、spring-boot-starter-social-linkedin 
   支持pring-social-linkedin

31、spring-boot-starter-social-twitter 
   支持pring-social-twitter

32、spring-boot-starter-test 
   支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。

33、spring-boot-starter-thymeleaf 
   支持Thymeleaf模板引擎,包括与Spring的集成。

34、spring-boot-starter-velocity 
   支持Velocity模板引擎。

35、spring-boot-starter-web 
    S支持全栈式Web开发,包括Tomcat和spring-webmvc。

36、spring-boot-starter-websocket 
   支持WebSocket开发。

37、spring-boot-starter-ws 
    支持Spring Web Services。

38、Spring Boot应用启动器面向生产环境的还有2种,具体如下:

1)spring-boot-starter-actuator 
   增加了面向产品上线相关的功能,比如测量和监控。

2)spring-boot-starter-remote-shell 
   增加了远程ssh shell的支持。

39、Spring Boot应用启动器还有一些替换技术的启动器,具体如下:

1)spring-boot-starter-jetty 
    引入了Jetty HTTP引擎(用于替换Tomcat)。

2)spring-boot-starter-log4j 
    支持Log4J日志框架。

3)spring-boot-starter-logging 
   引入了Spring Boot默认的日志框架Logback。

4)spring-boot-starter-tomcat 
   引入了Spring Boot默认的HTTP引擎Tomcat。

5)spring-boot-starter-undertow 
   引入了Undertow HTTP引擎(用于替换Tomcat)。

转载于:https://www.cnblogs.com/dream-to-pku/p/7508312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`DynamicServerListLoadBalancer` 是 Netflix Ribbon 中的一个负载均衡器,用于从服务注册中心获取服务列表并进行负载均衡。 在 Spring Boot 中,可以使用 Netflix Ribbon 和 Spring Cloud Netflix 来集成该负载均衡器。 具体实现方法如下: 1. 添加 Ribbon 和 Spring Cloud Netflix 依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ``` 2. 创建一个配置类,并指定负载均衡器的实现类 ```java @Configuration public class RibbonConfiguration { @Bean public IRule ribbonRule() { // 指定负载均衡器的实现类为 ZoneAvoidanceRule return new ZoneAvoidanceRule(); } @Bean public IPing ribbonPing() { // 指定负载均衡器的心跳检测实现类为 NoOpPing return new NoOpPing(); } } ``` 这里使用 `ZoneAvoidanceRule` 作为负载均衡器的实现类,默认情况下是使用 `RoundRobinRule`。 3. 在启动类中添加注解 `@RibbonClients` ```java @SpringBootApplication @RibbonClients(defaultConfiguration = RibbonConfiguration.class) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 这里使用 `@RibbonClients` 注解来指定 Ribbon 的配置类,即 `RibbonConfiguration`。 这样,在 Spring Boot 启动时,就会初始化 `DynamicServerListLoadBalancer`,从注册中心获取服务列表,并进行负载均衡。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值