springcloud框架怎么搭建

springcloud是时下非常流行的微服务框架,今天为大家介绍springcloud微服务框架中核心组件的搭建过程,同时介绍各个组件的使用方法和运行原理,下面就跟着小编一起学习吧。

东西/原料

  • IntelliJ IDEA

方式/步调

  1. 1

    SpringCloud旨在构建一套尺度化的微办事解决方案,供给了很是多的组件供用户选择。SpringCloud是在SpringBoot的根本上实现的开辟东西,供给设置装备摆设办事治理的微办事注册中间、网关、断路器等功能。在springcloud框架搭建过程中我们需要在父pom中引入spring-boot-starter-parent和spring-cloud-dependencies用来节制引入的springboot和spring cloud的版本号。

    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.5.4.RELEASE</version>
    </parent>


    <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Edgware.SR2</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
    </dependencyManagement>

  2. 2

    SpringCloud的办事治理组件Eureka,包含办事注册中间、办事注册与发现机制。Eureka组件既有办事端也有客户端,办事端需要零丁另起一个办事,需要添加的依靠是:

    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

    Eureka客户端的依靠是:

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

  3. 3

    Hystrix是容错办理组件,实现断路器功能,为办事中存在的延迟和故障供给更壮大的容错能力,需要添加的依靠如下:

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId
    </dependency>

    经由过程利用注解@HystrixCommand对接口进行熔断和降级处置。

  4. 4

    Ribbon是客户端负载平衡的办事挪用组件,Feign是基于Ribbon和Hystrix的声明式办事挪用组件。

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    Ribbon实现负载平衡需要如下的Bean的设置装备摆设:

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
      return new RestTemplate();

  5. 5

    Zuul网关组件,供给智能路由、拜候过滤等功能。

    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-zuul</artifactId>
    </dependency>

    zuul也需要零丁起一个办事。而且zuul需要注册到Eureka,zuul的设置装备摆设如下:

  6. 6

    别的,利用SpringCloud的这些组件,需要在启动类中添加如下的设置装备摆设:

    @EnableDiscoveryClient
    @EnableScheduling
    @EnableCircuitBreaker
    @SpringBootApplication

    END

注重事项

  • SpringCloud的每个组件整合都需要零丁的篇幅去介绍,后续再为大师讲解。
  • 发表于 2020-02-25 17:00
  • 阅读 ( 815 )
  • 分类:其他类型

0 条评论

请先 登录 后评论
联系我们:uytrv@hotmail.com 问答工具