
目录
服务调用_OpenFeign实现服务降级

引入降级依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
开启openfeign对sentinel支持
feign:
sentinel:
enabled: true
client:
config:
default:
# ⽹络连接阶段1秒超时 7
connectTimeout: 1000
# 服务请求响应阶段2秒超时
readTimeout: 2000
编写降级类
@Component
public class PaymentServiceFallback implements PaymentService {
@Override
public String index() {
return "系统繁忙请稍后再试一试~~~~";
}
}
设置降级
/**
* 支付远程调用类
*/
@Service
@FeignClient(value = "provider-payment",fallback = PaymentServiceFallback.class)
public interface PaymentService {
@GetMapping("/payment/index")
String index();
}
测试
关闭服务生产者服务请求http://localhost:80/order/index

服务调用_Dubbo实现服务生产者

创建接口服务工程cloud-service-api

创建公共支付接口
public interface IPaymentService {
String index();
}
创建支付生产者工程cloud-dubbo-provider-payment8001

POM引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.tong</groupId>
<artifactId>cloud-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
</dependencies>
编写主启动类
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class ProviderDubboMain8001 {
public static void main(String[] args) {
SpringApplication.run(ProviderDubboMain8001.class,args);
log.info("**************ProviderDubboMain8001 *********");
}
}
编写配置文件YML
server:
port: 8001
spring:
main:
allow-bean-definition-overriding: true
application:
name: provider-dubbo-payment
cloud:
nacos:
discovery:
server-addr: 192.168.66.101:8848
#dubbo配置
dubbo:
scan:
base-packages: com.tong.service
registry:
address: nacos://192.168.66.101:8848
timeout: 10000
protocol:
name: dubbo
port: -1

支付接口实现
@DubboService(timeout = 5000,version = "1.0",methods = {@Method(name = "index",retries = 2)})
public class PaymentServiceImpl implements IPaymentService {
@Override
public String index() {
return "hello dubbo payment";
}
}
测试
启动支付生产者服务

服务调用_Dubbo消费者调用接口

创建订单服务消费者工程cloud-dubbo-consumer-order80

POM添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.tong</groupId>
<artifactId>cloud-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
编写主启动类
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class OrderConsumerDubboMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsumerDubboMain80.class, args);
log.info("**************OrderConsumerDubboMain80 *********");
}
}
编写配置文件YML
server:
port: 80
spring:
main:
allow-circular-references: true
#在 Spring Boot 2.1 以及更高的版本增加该设定,因为 Spring Boot 默认调整了 Bean 定义覆盖行为。
allow-bean-definition-overriding: true
application:
#应用名字
name: consumer-dubbo-order
cloud:
nacos:
discovery:
#Nacos地址
server-addr: 192.168.66.101:8848
#dubbo配置
dubbo:
cloud:
#dubbo.cloud.subscribed-services:表示要订阅服务的服务名,可以配置'*',代表订阅所有服务,不推荐使用。若需订阅多应用,使用 "," 分割。
subscribed-services: "*"
registry:
# 注册至Nacos
address: nacos://192.168.66.101:8848
# 超时时间
timeout: 10000
protocol:
# 协议Dubbo
name: dubbo
# 端口号
port: -1
consumer:
check: false
实现接口调用
@Service
public class PaymentServiceImpl {
@DubboReference(version = "1.0")
IPaymentService iPaymentService;
public String index() {
return iPaymentService.index();
}
}
注意: @Reference 的包路径,不要导错包。
编写订单控制层
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private PaymentServiceImpl paymentService;
@GetMapping("index")
public String index(){
String index = paymentService.index();
System.out.println(index);
return index;
}
}

Spring Cloud Alibaba简介

什么是Spring Cloud Alibaba
Spring Cloud Alibaba致力于提供微服务开发的一站式解决方案 。 此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。

为什么要推出SpringCloud Alibaba呢?

问题: 但近几年来呢,许多的微服务组件已经闭源或者已经停止维护了 。
SpringCloudAlibaba 微服务组件内容与功能对比
Spring Cloud Alibaba是阿里巴巴结合自身的微服务实践开源的微服务全家桶。

SpringCloud 和 Spring Cloud Alibaba的功能对比


更换组件后微服务架构图

1.Spring Cloud Alibaba提供_____解决方案。微服务开发的一站式
- 为什么要推出SpringCloud Alibaba____。已经闭源或者已经停止维护
Spring Cloud Alibaba版本与兼容性

https://github.com/alibaba/spring-cloud-alibaba/wiki/ 版本说明
Spring Cloud维护主线版本

Spring Cloud Alibaba维护主线版本


组件版本关系
下表为按时间顺序发布的 Spring Cloud Alibaba 以及对应的适配 Spring Cloud 和 Spring Boot 版本关系(由于 Spring Cloud 版本 命名有调整,所以对应的 Spring Cloud Alibaba 版本号也做了对应变化)

SpringCloud与SpringBoot版本选型
https://github.com/alibaba/spring-cloud-alibaba/wiki/
下表为按时间顺序发布的 Spring Cloud Alibaba 以及对应的适配 Spring Cloud 和 Spring Boot 版本关系(由于 Spring Cloud 版本 命名有调整,所以对应的 Spring Cloud Alibaba 版本号也做了对应 变化)

1.通过查看____信息可以发现某个版本是Spring Cloud维护主线版本。GA
2.Spring Cloud Hoxton.SR9版本对应SpringCloud Alibaba的版 本是__。2.2.6.RELEASE
分布式服务治理_什么是Nacos

Nacos是阿里巴巴开源的服务注册中心以及配置中心,致力于给开 发者提供一款便捷、简单上手的开源框架。


为什么Nacos这么受欢迎
Nacos官方文档的介绍中有这么一句话,如下:
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以"服务"为中心的现代应用架构 (例如微服务范 式、云原生范式) 的服务基础设施。

Eureka 、 Config 这两个组件相信大家都用过,有什么感受?


Nacos主要提供以下四大功能
1、服务发现和服务健康测
Nacos 使服务更容易注册,并通过DNS或HTTP接口发现其他服务, Nacos还提供服务的实时健康检查,以防止向不健康的主机或服务实例发送请求。
2、动态配置服务
动态配置服务允许您在所有环境中以集中和动态的方式管理所有服 务的配置。Nacos消除了在更新配置时重 新部署应用程序, 这使配置的更改更加高效和灵活。
3、动态 DNS 服务
Nacos提供基于DNS协议的服务发现能力,旨在支持异构语言的服 务发现,支持将注册在Nacos上的服务以域名的方式暴露端点,让三方应用方便查阅及发现。
4、服务及其元数据管理
Nacos能让您从微服务平台建设的视觉管理数据中心的所有服务及 元数据,包括管理服务的描述、生命周期、服务的静态依赖分析、 服务的健康状态、服务的流量管理、路由及安全策略。
Nacos名字由来


1.Spring Cloud Alibaba中Nacos组件是___。服务注册中心以及配置中心
- Spring Cloud Alibaba中Nacos组件干掉了Spring Cloud哪三 个组件,分别是____。
Eureka、Bus、Config