10分钟了解全家桶概念: https://blog.csdn.net/CSDN_000000000000001/article/details/90255707
一、服务注册系列:
Zookeeper:
- 专业的独立产品。常和Dubbo进行搭配建立微服务框架。
- 具体实现方法,详见地址: https://www.cnblogs.com/kyleinjava/p/9431491.html
Eureka:
- 原生,2.0遇到性能瓶颈,停止维护
- 具体实现方法,详见地址: https://www.cnblogs.com/sam-uncle/p/8954401.html
Consul:
- 原生,GO语言开发
- 无关紧要,就不具体实现了
Nacos(阿里巴巴最新开发):
- 阿里巴巴新开发的注册中心插件,相当于: Nacos = Spring Cloud Eureka + Spring Cloud Config
- 具体实现方法,详见地址:
- https://yq.aliyun.com/articles/717995 或者 https://blog.csdn.net/weixin_34280237/article/details/92929936
- 手册地址: https://nacos.io/zh-cn/docs/what-is-nacos.html
- 【提供者】引入依赖:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${latest.version}</version> </dependency>
注意:版本 2.1.x.RELEASE 对应的是 Spring Boot 2.1.x 版本。版本 2.0.x.RELEASE 对应的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 对应的是 Spring Boot 1.5.x 版本。
- 【提供者】配置信息:
server.port=8070 spring.application.name=service-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- 【提供者】通过 Spring Cloud 原生注解
@EnableDiscoveryClient
开启服务注册发现功能:@SpringBootApplication @EnableDiscoveryClient //这个注解可以省略 public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } }
- 【消费者】配置信息:
server.port=8080 spring.application.name=service-consumer spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- 【消费者】通过 Spring Cloud 原生注解
@EnableDiscoveryClient
开启服务注册发现功能:@SpringBootApplication @EnableDiscoveryClient //这个注解可以省略 public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } }
二、服务调用系列。
- 可通过Ribbon+RestTemplate进行服务调用和负载均衡:
Ribbon知识讲解:
概念:
和nginx类似,都是用来做负载均衡处理的,不同的是,Ribbon是用来做本地服务的负载均衡,Nginx是用来做客户端请求的负载均衡。
(Nginx相当于医院的大门,当病患进来(客户端请求)之后,找对应的科室,进入科室之后再分配到不忙碌的医生(通过Ribbon本地服务负载均衡进行分配请求 ))
Ribbon核心组件IRule:
根据特定算法中从服务列表选取一个要访问的服务。
在SpringBoot项目中变更Ribbon负载均衡规则:
(官网强调,不能讲规则配置类被@ComponentScan注解扫描到,所以需要在主启动类以外新建一个包放入Ribbon规则配置类。)
规则配置类:
最后在启动类上加上 @RibbonClient 注解,并指定要调用的服务以及使用的Ribbon规则。
负载均衡的算法:
- 通过使用openFeign(自带Ribbon)进行服务接口调用:
在【消费者】的pom中引入 openFeign 依赖,去调用【服务者】中提供的接口:
加入依赖(版本号由 Spring Cloud 进行管理)
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <-- Spring Cloud 版本管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
启动类添加 @EnableFeignClients 注解 、
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NacosConsumerFeignApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerFeignApplication.class, args); } }
创建Feign接口、
@FeignClient(value = "nacos-provider") //value值为注册中心中服务者的ID名 public interface EchoService { @GetMapping(value = "/echo/{message}") String echo(@PathVariable("message") String message); }
Controller中调用服务者提供的服务接口、
@RestController public class NacosConsumerFeignController { @Autowired private EchoService echoService; @GetMapping(value = "/echo/hi") public String echo() { return echoService.echo("Hi Feign"); } }
openFeign请求时长设置:
三、服务降级系列。
- 具体解决的情况:
- Hystrix服务:
- 服务降级:https://blog.csdn.net/chengqiuming/article/details/81412510
- 服务熔断:https://www.cnblogs.com/yufeng218/p/11042555.html
- 仪表盘监控:https://blog.csdn.net/jasnet_u/article/details/82177130
仪表盘图解:
四、服务网关系列。
Spring Cloud Gateway:
分布式配置中心系列。
Spring Cloud Config:
一篇就够: https://www.cnblogs.com/fengzheng/p/11242128.html
Bus消息总线系列。
当我们手动更新了 Spring Cloud Config的配置文件后,可通过Spring Cloud Bus 进行广播通知让服务更新自己的配置文件以达到动态刷新效果。
Spring Cloud Stream 消息驱动。
Spring Cloud Sleuth分布式请求链路追踪系列。
前言:
CAP知识讲解: