第一章 Spring Cloud简介
Spring Cloud是Spring提供的微服务框架,由下面几个部分组成。
- 注册中心
- 配置中心
- 服务链路追踪
- 负载均衡
- 服务容错
- 服务网关
- 服务发布与回滚
- 服务动态伸缩、容器化
下面我们来搭建一个简单的Spring Cloud项目。
第二章 Eureka
Eureka是一个用于服务注册和发现的组件,也就是上面说的注册中心的部分。
Eureka基本机构主要包括以下3个角色。
- Eureka Server:服务注册中心,提供服务注册和发现功能。
- Provider Service:服务提供者。
- Consumer Service:服务消费者。
首先推荐使用Spring Initializr创建注册中心,Spring Boot的版本我们用2.4.9

EurekaApplication中添加注解作为Eureka服务器
@SpringBootApplication
@EnableEurekaServer //启动Eureka服务器
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}application.yml配置相关信息
server:
port: 9004
spring:
application:
name: eureka-server
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
defaultZone: http://127.0.0.1:9004/eureka
# 不注册自己
register-with-eureka: false
# 不拉取服务
fetch-registry: false接下来创建同样两个订单和支付项目,注册到Eureka中,订单调用支付服务
- 支付服务
pom添加依赖
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>PaymentApplication添加注解
@SpringBootApplication
@EnableDiscoveryClient //Eureka Client,用这个以后换成nacos也不用变了
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class, args);
}
}application.yml配置相关信息
server:
port: 9001
spring:
application:
name: cloud-payment-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9004/eureka
register-with-eureka: true
fetch-registry: truePaymentController
@RestController
@RequestMapping("/payment")
public class PaymentController {
@GetMapping("/{id}")
public ResponseEntity<Payment> payment(@PathVariable("id") Integer id) {
Payment payment = new Payment(id,"支付成功");
return ResponseEntity.ok(payment);
}
}- 订单服务
pom添加依赖
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>OrderApplication添加注解
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}application.yml配置相关信息
server:
port: 9002
spring:
application:
name: cloud-order-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9004/eurekaOrderController
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/payment/{id}")
public ResponseEntity<Payment> getPaymentById(@PathVariable("id") Integer id) {
// String url = "http://localhost:9001/payment/" + id;
//1.通过discoveryClient输入服务名获得注册列表
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
//2.获得第一条
ServiceInstance serviceInstance = serviceInstances.get(0);
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/payment/" + id;
Payment payment = restTemplate.getForObject(url, Payment.class);
return ResponseEntity.ok(payment);
}
}第三章 Spring Cloud OpenFeign
Feign是一个声明式的HTTP客户端组件,它旨在是编写Http客户端变得更加容易。OpenFeign添加了对于Spring MVC注解的支持,同时集成了Spring Cloud LoadBalancer和Spring Cloud CircuitBreaker,在使用Feign时,提供负载均衡和熔断降级的功能。
在订单工程工程的pom.xml中添加如下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>OrderApplication中开启OpenFeign
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启OpenFeign
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}创建接口
@FeignClient(value = "cloud-payment-service") //调用的服务
public interface PaymentClient {
@GetMapping("/payment/{id}")
public Payment payment(@PathVariable("id") Integer id);
}OrderController
@Autowired
private PaymentClient paymentClient;
@GetMapping("/feign/payment/{id}")
public ResponseEntity<Payment> getPaymentByFeign(@PathVariable("id") Integer id) {
Payment payment = paymentClient.payment(id);
return ResponseEntity.ok(payment);
}第四章 微服务网关Spring Cloud Gateway
SpringCloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。
同样创建网关工程

GatewayApplication中开启网关功能
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}application.yml中完成配置
server:
port: 9005
spring:
application:
name: api-gateway
#配置路由
cloud:
gateway:
routes:
- id: service1
uri: lb://cloud-payment-service #使用LoadBalancer负载均衡路由
predicates:
- Path=/payment/{sagment} #或者/payment/**
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9004/eureka