目录
1.认识微服务:
单体架构:
2.服务注册和发现
1.注册中心:
2.服务注册:
3.服务发现:
发现并调用服务:
方法1:
方法2:
方法3:OpenFeign
OpenFeign:
1.引入依赖:
2.启动类加入:@EnableFeignClients
3.编写OpenFeign 客户端:
4.使用FeignClient:
连接池:
1.认识微服务:
单体架构:
单体架构(monolithic structure):顾名思义,整个项目中所有功能模块都在一个工程中开发;项目部署时需要对所有模块一起编译、打包;项目的架构设计、开发模式都非常简单。
问题:
-
团队协作成本高:试想一下,你们团队数十个人同时协作开发同一个项目,由于所有模块都在一个项目中,不同模块的代码之间物理边界越来越模糊。最终要把功能合并到一个分支,你绝对会陷入到解决冲突的泥潭之中。
-
系统发布效率低:任何模块变更都需要发布整个系统,而系统发布过程中需要多个模块之间制约较多,需要对比各种文件,任何一处出现问题都会导致发布失败,往往一次发布需要数十分钟甚至数小时。
-
系统可用性差:单体架构各个功能模块是作为一个服务部署,相互之间会互相影响,一些热点功能会耗尽系统资源,导致其它服务低可用。
微服务架构:
特点:
-
单一职责:一个微服务负责一部分业务功能,并且其核心数据不依赖于其它模块。
-
团队自治:每个微服务都有自己独立的开发、测试、发布、运维人员,团队人员规模不超过10人(2张披萨能喂饱)
-
服务自治:每个微服务都独立打包部署,访问自己独立的数据库。并且要做好服务隔离,避免对其它服务产生影响
Spring Cloud Alibaba与Spring Cloud 的联系:
-
基于 Spring Boot:两者都是基于 Spring Boot 的微服务框架。
-
Spring Cloud 核心理念:Spring Cloud Alibaba 遵循 Spring Cloud 的设计理念和模式。
-
组件兼容:Spring Cloud Alibaba 的组件可以与 Spring Cloud 的其他组件配合使用。
Spring Cloud Alibaba与Spring Cloud 的区别:
-
服务发现:Spring Cloud 使用的是 Netflix Eureka,而 Spring Cloud Alibaba 使用的是 Nacos。
-
断路器:Spring Cloud 原生支持 Hystrix,而 Spring Cloud Alibaba 推荐使用 Sentinel。
-
配置管理:Spring Cloud 使用 Spring Cloud Config Server,而 Spring Cloud Alibaba 使用 Nacos 作为配置中心。
-
负载均衡:Spring Cloud LoadBalancer结合 Nacos实现负载均衡。
-
远程调用支持:Spring Cloud Alibaba 支持 Dubbo 作为 RPC 调用框架,而 Spring Cloud 默认不包含 RPC 支持
2.服务注册和发现
如果不使用注册中心 服务调用者 直接发送http请求调服务提供者 会产生许多问题 把url路径写死会导致 提供者挂吊时不能及时停止调用 另外不能实现多个 服务器提供者的负载均衡实现
1.注册中心:
注册中心为Nacos
-
服务启动时就会注册自己的服务信息(服务名、IP、端口)到注册中心
-
调用者可以从注册中心订阅想要的服务,获取服务对应的实例列表(1个服务可能多实例部署)
-
调用者自己对实例列表负载均衡,挑选一个实例
-
调用者向该实例发起远程调用
当服务提供者的实例宕机或者启动新实例时,调用者如何得知呢?
-
服务提供者会定期向注册中心发送请求,报告自己的健康状态(心跳请求)
-
当注册中心长时间收不到提供者的心跳时,会认为该实例宕机,将其从服务的实例列表中剔除
-
当服务有新实例启动时,会发送注册服务请求,其信息会被记录在注册中心的服务实例列表
-
当注册中心服务列表变更时,会主动通知微服务,更新本地服务列表
2.服务注册:
为服务提供者将 自身注册到 nacos中 只需要两步:
-
引入依赖
-
配置Nacos地址
1.
在父工程需要添加Spring Cloud及Spring Cloud Alibaba的版本约束,如果已添加不用重复添加
<!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<!-- type=pom ,scope=import 表示继承 和 parent 作用一样 -->
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
因为我们安装的是2.4.0版本的nacos,Spring Cloud Alibaba默认使用的Nacos比该版本低,所以需要在父pom.xml中约定nacos-client版本,如下:
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.4.0</version>
</dependency>
在item-service
的pom.xml
中添加依赖:
<!--nacos 服务注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.
在item-service
的application.yml
中添加nacos地址配置:
spring:
application:
name: item-service # 服务名称
cloud:
nacos:
server-addr: 192.168.100.168:8848 # nacos地址
3.服务发现:
为服务的消费者要去 nacos 去订阅服务 从而实现 请求的发送 实现 服务和服务之间 的信息传输
分为三步:
-
引入依赖
-
配置Nacos地址
-
发现并调用服务
服务发现除了要引入nacos依赖以外,由于还需要负载均衡,因此要引入SpringCloud提供的LoadBalancer依赖。
我们在cart-service
中的pom.xml
中添加下面的依赖:
<!--nacos 服务注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--负载均衡器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
在cart-service
的application.yml
中添加nacos地址配置:
spring:
cloud:
nacos:
server-addr: 192.168.100.168:8848
发现并调用服务:
方法1:
使用DiscoveryClient工具实现服务发现,SpringCloud已经帮我们自动装配,我们可以直接注入使用:
@RequireArgConstructor 代替 @Autowired 注解 可以 用构造器的方法为用 final 修饰的成员 变量自动 注入实例
之后 用discoveryClient 中的getInstance 方法 即可活得 所有 服务提供者的列表
随机选择一个实例 得到Uri调用即可
方法2:
使用@LoadBalanced注解标识RestTemplate,如下:
修改RemoteCallConfig类
@Configuration
public class RemoteCallConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
之后在url地址中之间写入访问的 服务 即可自动 实现 负载均衡
方法3:OpenFeign
OpenFeign:
使用OpenFeign的作用:为了更简单的实现服务之间的远程调用
其实远程调用的关键点就在于四个:
-
请求方式
-
请求路径
-
请求参数
-
返回值类型
1.引入依赖:
<!--openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--负载均衡器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
2.启动类加入:@EnableFeignClients
3.编写OpenFeign 客户端:
在cart-service
中,定义一个新的接口,编写Feign客户端:
package com.hmall.cart.client;
import com.hmall.cart.domain.dto.ItemDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient("item-service")
public interface ItemClient {
@GetMapping("/items")
List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids);
}
这里只需要声明接口,无需实现方法。接口中的几个关键信息:
-
@FeignClient("item-service")
:声明服务名称 -
@GetMapping
:声明请求方式 -
@GetMapping("/items")
:声明请求路径 -
@RequestParam("ids") Collection<Long> ids
:声明请求参数 -
List<ItemDTO>
:返回值类型
4.使用FeignClient:
直接注入使用方法即可
连接池:
Feign底层发起http请求,依赖于其它的框架。其底层支持的http客户端实现包括:
-
HttpURLConnection:默认实现,不支持连接池
-
Apache HttpClient :支持连接池
-
OKHttp:支持连接池
在cart-service
的pom.xml
中引入依赖:
<!--OK http 的依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
在cart-service
的application.yml
配置文件中开启Feign的连接池功能:
feign:
okhttp:
enabled: true # 开启OKHttp功能