1. 介绍springcloud
spring cloud是基于spring boot框架的微服务。可以迅速搭建分布式系统中的公共组件(例如:断路器,集群,配置管理,负载均衡等)。spring cloud也可以在开发人员的电脑上跑起来。
2. springcloud的基本组件
- Eureka,服务注册中心,特性有失效剔除、服务保护。
代码例子:
a. 创建model工程
b.pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
c.application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
d.启动类DemoApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
e.启动DemoApplication,浏览器上输入地址:localhost:8761
- Ribbon,客户端负载均衡,特性有区域亲和、重试机制。
- Feign,声明式服务调用,本质上就是Ribbon+Hystrix。
- Hystrix,客户端容错保护,特性有服务降级、服务熔断、请求缓存、请求合并、依赖隔离。
- Zuul,API服务网关,功能有路由分发和过滤。
- Config,分布式配置中心,支持本地仓库、SVN、Git、Jar包内配置等模式。
- Bus,消息总线,配合Config仓库修改的一种Stream实现。
- Sleuth,分布式服务追踪,需要搞清楚TraceID和SpanID以及抽样,如何与ELK整合。
- Dashboard,Hystrix仪表盘,监控集群模式和单点模式,其中集群模式需要收集器Turbine配合。