一、Spring Cloud微服务简介
(一)概念
Spring Cloud是一系列框架的有序集合,它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Cloud的子项目一键启动和部署。
(二)主要功能
- 服务发现与注册(Eureka)
- 服务发现是微服务架构中的关键组件。在一个微服务系统中,众多的微服务实例需要相互协作。Eureka作为服务发现组件,允许微服务实例将自己注册到服务注册表中,同时也能够从注册表中获取其他服务的信息。这样,服务之间的调用就不需要硬编码服务的地址,提高了系统的灵活性和可扩展性。
- 配置管理(Config)
- 在微服务架构下,配置文件分散在各个微服务中,管理起来非常困难。Spring Cloud Config提供了一个集中式的配置管理解决方案,它允许将所有微服务的配置文件存储在一个中心位置(如Git仓库),微服务在启动时可以从这个中心位置获取自己所需的配置信息。这使得配置的修改和管理更加便捷,并且能够确保配置的一致性。
- 断路器(Hystrix)
- 在微服务架构中,一个服务常常会调用其他服务。如果被调用的服务出现故障或者响应时间过长,可能会导致调用方的服务也出现问题,甚至可能导致整个系统的雪崩效应。Hystrix通过对服务调用进行隔离、熔断和降级等操作来防止这种情况的发生。例如,当某个服务的故障率达到一定阈值时,Hystrix会自动切断对该服务的调用,从而保护调用方服务的正常运行。
- 路由与负载均衡(Zuul)
- Zuul是Spring Cloud中的网关组件,它可以作为微服务系统的入口,负责接收外部的请求,并将请求路由到相应的微服务实例上。同时,Zuul还具备负载均衡的功能,可以根据一定的算法(如轮询、随机等)将请求均匀地分配到多个可用的微服务实例上,提高系统的可用性和性能。
二、代码样例
以下是一个简单的使用Spring Cloud构建微服务的示例,包括服务注册与发现(Eureka)和一个简单的服务调用示例。
(一)创建Eureka Server(服务注册中心)
- 创建Maven项目
- 使用Spring Initializr创建一个新的Maven项目,添加
spring - cloud - starter - netflix - eureka - server
依赖。
- 使用Spring Initializr创建一个新的Maven项目,添加
- 配置文件(application.yml)
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 这里将Eureka Server的端口设置为8761,并且配置它不向自己注册(
registerWithEureka: false
),也不从自己获取注册表信息(fetchRegistry: false
)。
- 这里将Eureka Server的端口设置为8761,并且配置它不向自己注册(
- 启动类添加注解
- 在启动类上添加
@EnableEurekaServer
注解,示例代码如下:
import org.springframework.boot.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringBootApplication.run(EurekaServerApplication.class, args); } }
- 在启动类上添加
(二)创建微服务提供者
- 创建Maven项目
- 同样使用Spring Initializr创建项目,添加
spring - cloud - starter - netflix - eureka - client
依赖。
- 同样使用Spring Initializr创建项目,添加
- 配置文件(application.yml)
spring: application: name: service - provider server: port: 8081 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
- 这里设置了微服务的名称为
service - provider
,端口为8081,并指定了Eureka Server的地址。
- 这里设置了微服务的名称为
- 创建一个简单的REST服务
- 创建一个简单的Controller,示例代码如下:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProviderController { @GetMapping("/hello") public String sayHello() { return "Hello from service - provider"; } }
(三)创建微服务消费者
- 创建Maven项目
- 使用Spring Initializr创建项目,添加
spring - cloud - starter - netflix - eureka - client
依赖。
- 使用Spring Initializr创建项目,添加
- 配置文件(application.yml)
spring: application: name: service - consumer server: port: 8082 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
- 这里设置了微服务的名称为
service - consumer
,端口为8082,并指定了Eureka Server的地址。
- 这里设置了微服务的名称为
- 使用RestTemplate进行服务调用
- 在启动类中创建
RestTemplate
的实例,并使用@LoadBalanced
注解进行负载均衡配置,示例代码如下:
import org.springframework.boot.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class ConsumerApplication { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringBootApplication.run(ConsumerApplication.class, args); } }
- 然后创建一个Controller来调用服务提供者的服务,示例代码如下:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @GetMapping("/callHello") public String callHello() { return restTemplate.getForObject("http://service - provider/hello", String.class); } }
- 在启动类中创建
这个简单的示例展示了Spring Cloud中服务注册与发现以及服务调用的基本流程。在实际应用中,还可以根据需求进一步集成其他Spring Cloud组件,如配置管理、断路器等。