Spring Cloud 是一系列框架的集合,它利用了 Spring Boot 的開發便利性,提供了微服務開發中所需的各種工具,包括配置管理、服務發現、斷路器、智能路由、微代理、控制總線、一次性令牌、全局鎖、領導選舉、分佈式會話、集羣狀態等。以下是 Spring Cloud 中一些主要的組件:
1. Spring Cloud Config
用於集中化管理所有微服務環境下的配置,可以將配置放到遠程服務器,集中化管理配置,當配置發生變更時,可以實時刷新到各個微服務中。
用途:集中管理配置文件,支持動態刷新配置。
# config-server.yml (Config Server 配置)
spring:
cloud:
config:
server:
git:
uri: https://github.com/your-repo/config-repo.git # 配置倉庫地址
searchPaths: config # 指定配置文件路徑
# application.yml (客户端配置)
spring:
cloud:
config:
uri: http://config-server:8888 # 配置中心地址
fail-fast: true
enabled: true
2. Spring Cloud Netflix Eureka
基於 Netflix Eureka 的服務發現與註冊中心實現,允許各個微服務實例向 Eureka Server 註冊自身,並且可以相互發現彼此的位置,從而實現服務之間的調用。
用途:服務發現與註冊
# eureka-server.yml (Eureka Server 配置)
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# service.yml (服務實例配置)
spring:
application:
name: my-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. Spring Cloud Netflix Hystrix
提供了斷路器功能,隔離了遠程系統、服務和第三方庫的訪問點,當出現故障時能夠快速失敗,避免級聯效應。
用途:斷路器模式,防止雪崩效應。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class MyController {
@GetMapping("/service")
@HystrixCommand(fallbackMethod = "fallback")
public String callService() {
// 調用遠程服務
return restTemplate.getForObject("http://remote-service", String.class);
}
public String fallback() {
return "Fallback method invoked!";
}
}
4. Spring Cloud Netflix Ribbon
基於 Netflix Ribbon 實現的客户端負載均衡庫,可以在消費端實現對服務端請求的負載均衡策略。
用途:微服務架構中的路由轉發和過濾。
#application.yml 配置文
spring:
application:
name: zuul-gateway
server:
port: 8765
zuul:
routes:
provider-service:
path: /provider-service/**
url: http://localhost:8080
consumer-service:
path: /consumer-service/**
url: http://localhost:8081
#provider-service 的application.yml 配置文件
spring:
application:
name: provider-service
server:
port: 8080
#consumer-service 的application.yml 配置文件
spring:
application:
name: consumer-service
server:
port: 8081
5. Spring Cloud Netflix Zuul
邊緣服務應用,為微服務架構集羣提供代理、過濾、路由等功能,常被用來做API網關。
用途:API網關,路由請求到不同微服務。
# zuul-gateway.yml (Zuul 網關配置)
zuul:
routes:
my-service:
path: /my-service/**
url: http://localhost:8080
java代碼實現:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
}
6. Spring Cloud Bus
用於簡化應用程序之間或應用程序與消息代理之間的集成工作,可以用於事件發佈、服務間通信,也可以用於在集羣(例如配置變化事件)中廣播消息。
用途:簡化應用程序之間的事件發佈和訂閲機制
application.yml 配置文件
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
enabled: true
trace: true
provider-service 創建一個簡單的服務提供者,暴露一個 REST API,並監聽配置更新事件。
spring:
application:
name: provider-service
cloud:
config:
uri: http://localhost:8888
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
enabled: true
trace: true
consumer-service 創建一個服務消費者,暴露一個 REST API,並監聽配置更新事件。
spring:
application:
name: consumer-service
cloud:
config:
uri: http://localhost:8888
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
enabled: true
trace: true
7. Spring Cloud Sleuth
為微服務架構下的服務提供了追蹤支持,通常與 Zipkin 配合使用,可以收集服務間的調用鏈路數據,便於問題排查。
用途:跟蹤微服務間的調用鏈路。
java代碼實現:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TracingController {
@GetMapping("/trace")
public String trace() {
// 業務邏輯
return "Traced";
}
}