Spring Cloud是Spring提供的微服务框架,由下面几个部分组成。
下面我们来搭建一个简单的Spring Cloud项目。
Eureka是一个用于服务注册和发现的组件,也就是上面说的注册中心的部分。
Eureka基本机构主要包括以下3个角色。
首先推荐使用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添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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: true
PaymentController
@RestController
@RequestMapping("/payment")
public class PaymentController {
@GetMapping("/{id}")
public ResponseEntity payment(@PathVariable("id") Integer id) {
Payment payment = new Payment(id,"支付成功");
return ResponseEntity.ok(payment);
}
}
pom添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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/eureka
OrderController
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/payment/{id}")
public ResponseEntity getPaymentById(@PathVariable("id") Integer id) {
// String url = "http://localhost:9001/payment/" + id;
//1.通过discoveryClient输入服务名获得注册列表
List 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);
}
}
Feign是一个声明式的HTTP客户端组件,它旨在是编写Http客户端变得更加容易。OpenFeign添加了对于Spring MVC注解的支持,同时集成了Spring Cloud LoadBalancer和Spring Cloud CircuitBreaker,在使用Feign时,提供负载均衡和熔断降级的功能。
在订单工程工程的pom.xml中添加如下依赖
org.springframework.cloud
spring-cloud-starter-openfeign
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 getPaymentByFeign(@PathVariable("id") Integer id) {
Payment payment = paymentClient.payment(id);
return ResponseEntity.ok(payment);
}
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