上篇文章:
在上篇文章中,代码有一些缺点:
1.使用RestTemplate时候URL写死了,如果服务提供方修改ip,就需要改动代码。
2.如果服务提供方多机部署,ip如何写。
3.返回结果如何共用,不重复写代码,并且URL也容易写错,如何复用。
4.服务提供方的接口实际上是开放的,存在一定风险,应该只对服务调用方开放。
上述缺点可以使用服务注册/发现来解决:
1 服务注册/发现
服务注册/发现包括三个角色:服务注册中心、服务消费者、服务提供者:
服务注册中心:保存服务的注册信息(比如ip),服务节点变更会同步更新。使用相关通信机制保持和服务提供者之间的通信,如果服务提供者无法通信,就会注销服务实例。
服务消费者:调用其它微服务接口的一方。
服务提供者:被其它微服务调用接口的一方。
服务发现是指服务消费者要调用服务提供方接口时,先去服务注册中心查询服务相关信息,比如地址等。
服务注册是指服务提供方启动时主动向服务注册中心注册服务,并定期向注册中心发送心跳包,汇报自身存活状态。
2 CAP理论
C:一致性(Consistency),指同一时间无论访问主节点还是从节点获得的数据是一致的(强一致性,弱一致性是主节点涉及修改后由于同步需要时间,因此存在一定时间的不一致)。
A:可用性(Availability),指任何时候访问系统都有响应,无论响应的数据是否过时或错误。
P:分区容错性(Partition Tolerance),指出现网络分区后(某节点故障),系统仍能提供服务。
CAP理论是分布式系统最基础最关键的理论。但是一致性和可用性无法同时满足,因为要满足一致性,就需要数据同步时不能访问系统,这违背了可用性;如果要满足可用性,有可能访问过时数据(还未同步的从节点数据),这违背了一致性。因此C和A无法同时满足。
P必须要满足,因此就出现了CP和CA两种组合,而常见的注册中心组件就是选择了两种组合中的一种:
Zookeeper |
Eureka |
Nacos |
满足CP |
满足CA |
CP或CA(默认CA) |
实际上,为用户提供访问时,提供过时的数据胜过不提供响应。
3 Eureka
Eureka分为Eureka Server和Eureka Client,Eureka Server供注册中心使用,Eureka Client供服务提供者使用。
3.1 搭建注册中心
由于注册中心也相当于一个微服务,因此也需要创建一个项目并启动,这里还是采用父子项目的形式创建,仍然用前面创建的父项目:
配置pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
由于这不需要进行SpringBoot Web开发,因此不需要SpringBoot Web相关依赖。配置SpringBoot配置文件:
server:
port: 10010
spring:
application:
name: eureka_server
eureka:
instance:
hostname: localhost
client:
fetch-registry: false # 是否从其它Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
register-with-eureka: false # 是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
service-url:
# 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
这里必须要加@EnableEurekaServer才能成功开启注册中心服务。运行代码结果如下(访问127.0.0.1:10010):
3.2 服务注册
引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
SpringBoot配置文件:
spring:
application:
name: product-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka
启动项目,就可以发现该服务已经注册到Eureka中:
3.3 服务发现
引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
SpringBoot配置文件:
spring:
application:
name: order-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka
修改远程调用代码:
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
public OrderInfo selectOrderById(Integer orderId) {
OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
//旧方式
//String url = "http://127.0.0.1:8081/product/" + orderInfo.getProductId();
//ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//新方式:通过注册中心进行服务发现
List<ServiceInstance> productService = discoveryClient.getInstances("product-service");
EurekaServiceInstance serviceInstance = (EurekaServiceInstance) productService.get(0);
String url = serviceInstance.getUri() + "/product/" + orderInfo.getProductId();
ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
orderInfo.setProductInfo(productInfo);
return orderInfo;
}
}
运行代码,结果如下:
可以发现,配置了注册中心后,关于服务之间的远程调用更加灵活方便。
4 Eureka与Zookeeper区别
二者都是用于服务注册和服务发现的,但是也存在很大不同:
1.Eureka是Netflix开源项目,Zookeeper是Apache开源项目。
2.Eureka基于AP原则,保证高可用性;Zookeeper基于CP原则,保证数据一致性。
3.集群环境下,Eureka每个节点地位均等;Zookeeper节点存在地位划分,主要是Leader、Follower或Observer,这就意味Leader节点故障需要进行重新选举Leader,存在一定时间的服务不可用。
下篇文章: