SpringCloud
2023/1/1大约 6 分钟
Spring Cloud基本概念和功能
Spring Cloud是一系列框架的有序集合,它利用Spring Boot的开发便利性简化了分布式系统基础设施的开发,比如服务发现、服务网关、配置管理、消息总线、负载均衡、断路器等,都可以用Spring Boot的开发风格做到一键启动和部署。下面是Spring Cloud的基本概念和功能:
- 服务注册与发现:Spring Cloud提供了服务注册与发现功能,可以让服务提供者自动向注册中心注册服务,并让服务消费者从注册中心中发现服务。
- 负载均衡:Spring Cloud提供了负载均衡功能,可以让服务消费者通过负载均衡策略选择服务提供者,实现服务的高可用。
- 服务网关:Spring Cloud提供了服务网关功能,可以统一管理服务的入口,实现路由、过滤、限流等功能。
- 配置管理:Spring Cloud提供了配置管理功能,可以将配置文件存储在配置中心,实现配置的集中管理和动态更新。
- 断路器:Spring Cloud提供了断路器功能,可以在服务出现故障时进行熔断,防止故障蔓延,提高系统的容错能力。
为什么要用Spring Cloud?
使用Spring Cloud有以下几个优点:
- 微服务架构支持:Spring Cloud提供了构建微服务架构所需的所有组件,可以帮助开发者快速构建微服务应用。
- 开箱即用:Spring Cloud基于Spring Boot,提供了开箱即用的功能,开发者只需添加依赖和简单配置即可使用。
- 生态丰富:Spring Cloud与Spring生态系统无缝集成,可以使用Spring的各种功能。
- 社区活跃:Spring Cloud有着庞大的社区支持和活跃的开发者社区,可以保证Spring Cloud的更新和维护。
- 易于扩展:Spring Cloud提供了丰富的扩展点,可以根据业务需求进行扩展和定制。
总之,使用Spring Cloud可以帮助开发者快速构建微服务架构,提高开发效率,降低运维成本。
Spring Cloud应用场景
Spring Cloud主要应用于微服务架构、分布式系统、云原生应用等场景,适合构建大规模、高可用、可扩展的应用系统。
Spring Cloud核心组件
Spring Cloud的核心组件包括:
- Spring Cloud Netflix:Netflix OSS组件的集成,包括Eureka(服务注册与发现)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(服务网关)等。
- Spring Cloud Gateway:新一代API网关,基于Spring 5、Spring Boot 2和Project Reactor等技术。
- Spring Cloud OpenFeign:声明式HTTP客户端,集成了Ribbon和Hystrix。
- Spring Cloud Config:配置中心,支持Git、SVN等版本控制系统。
- Spring Cloud Bus:消息总线,用于传播集群中的状态变化。
- Spring Cloud Sleuth:分布式链路追踪,集成了Zipkin、HTrace等。
- Spring Cloud Stream:消息驱动微服务,支持RabbitMQ、Kafka等消息中间件。
Spring Cloud核心架构图
+------------------+ +------------------+ +------------------+
| Service | | Service | | Service |
| Provider A | | Provider B | | Provider C |
+------------------+ +------------------+ +------------------+
| | |
| Register | Register | Register
| | |
v v v
+------------------+ +------------------+ +------------------+
| Eureka | | Eureka | | Eureka |
| Server | | Server | | Server |
| (注册中心) | | (注册中心) | | (注册中心) |
+------------------+ +------------------+ +------------------+
^ ^ ^
| | |
| Discover | Discover | Discover
| | |
+------------------+ +------------------+ +------------------+
| Service | | Service | | Service |
| Consumer A | | Consumer B | | Consumer C |
+------------------+ +------------------+ +------------------+
| | |
| | |
| | |
+------------------+ +------------------+ +------------------+
| Gateway | | Gateway | | Gateway |
| (服务网关) | | (服务网关) | | (服务网关) |
+------------------+ +------------------+ +------------------+
| | |
| | |
| | |
+------------------+ +------------------+ +------------------+
| Config | | Config | | Config |
| Server | | Server | | Server |
| (配置中心) | | (配置中心) | | (配置中心) |
+------------------+ +------------------+ +------------------+在上面的架构图中,展示了Spring Cloud的核心组件之间的关系:服务提供者向Eureka注册服务,服务消费者从Eureka发现服务,通过Gateway访问服务,从Config Server获取配置。
Spring Cloud快速入门
1、创建父工程
创建一个Maven父工程,添加Spring Cloud依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>2、创建Eureka Server
创建Eureka Server模块:
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>配置文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}3、创建服务提供者
创建服务提供者模块:
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>配置文件
server:
port: 8081
spring:
application:
name: service-provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/启动类
@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}Controller
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello from Service Provider!";
}
}4、创建服务消费者
创建服务消费者模块:
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>配置文件
server:
port: 8082
spring:
application:
name: service-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/启动类
@SpringBootApplication
@EnableEurekaClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}Controller
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consume")
public String consume() {
return restTemplate.getForObject("http://service-provider/hello", String.class);
}
}Spring Cloud Gateway
Spring Cloud Gateway是新一代API网关,基于Spring 5、Spring Boot 2和Project Reactor等技术。
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>2、配置路由
server:
port: 8080
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: service-provider
uri: lb://service-provider
predicates:
- Path=/provider/**
filters:
- StripPrefix=1
- id: service-consumer
uri: lb://service-consumer
predicates:
- Path=/consumer/**
filters:
- StripPrefix=1
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/3、过滤器
Spring Cloud Gateway提供了多种过滤器:
- AddRequestHeader:添加请求头
- AddRequestParameter:添加请求参数
- AddResponseHeader:添加响应头
- StripPrefix:去除路径前缀
- PrefixPath:添加路径前缀
- RequestRateLimiter:请求限流
- Hystrix:熔断器
Spring Cloud OpenFeign
Spring Cloud OpenFeign是声明式HTTP客户端,集成了Ribbon和Hystrix。
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>2、启用Feign
@SpringBootApplication
@EnableFeignClients
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}3、创建Feign客户端
@FeignClient(name = "service-provider")
public interface ProviderClient {
@GetMapping("/hello")
String hello();
}4、使用Feign客户端
@RestController
public class ConsumerController {
@Autowired
private ProviderClient providerClient;
@GetMapping("/consume")
public String consume() {
return providerClient.hello();
}
}Spring Cloud Config
Spring Cloud Config是配置中心,支持Git、SVN等版本控制系统。
1、创建Config Server
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>配置文件
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/your-repo/config-repo
search-paths: config
username: your-username
password: your-password启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}2、创建Config Client
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>配置文件
创建bootstrap.yml:
spring:
application:
name: service-provider
cloud:
config:
uri: http://localhost:8888
profile: dev
label: masterSpring Cloud Sleuth
Spring Cloud Sleuth是分布式链路追踪,集成了Zipkin、HTrace等。
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>2、配置Zipkin
spring:
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1.0参考链接
贡献者
cmyshare
