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>
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>
Hystrix是容错办理组件,实现断路器功能,为办事中存在的延迟和故障供给更壮大的容错能力,需要添加的依靠如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId
</dependency>
经由过程利用注解@HystrixCommand对接口进行熔断和降级处置。
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();
Zuul网关组件,供给智能路由、拜候过滤等功能。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
zuul也需要零丁起一个办事。而且zuul需要注册到Eureka,zuul的设置装备摆设如下:
别的,利用SpringCloud的这些组件,需要在启动类中添加如下的设置装备摆设:
@EnableDiscoveryClient
@EnableScheduling
@EnableCircuitBreaker
@SpringBootApplication
0 篇文章
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!