[Spring Cloud][6] Eureka Server 搭建详解,与 Zookeeper 的区别
本文介绍了如何搭建Eureka Server服务注册中心,并将其应用于微服务架构中。主要内容包括:创建Eureka Server子模块,引入相关依赖,完善启动类与配置文件;将product-service注册到Eureka;在order-service中实现服务发现,通过DiscoveryClient获取服务实例进行远程调用。文章还对比了Eureka与Zookeeper的区别,展示了Eureka作
·
文章目录
搭建 Eureka Server
Eureka Server
是一个独立的微服务
创建 eureka-server 子模块
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
项目构建插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
完善启动类
package org.example.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
编写配置文件
server:
port: 10010
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
fetch-registry
:表示是否从Eureka Server
获取注册信息,默认为true
。因为这是一个单点的Eureka Server
,不需要同步其他的Eureka Server
节点的数据,这里设置为false
register-with-eureka
:表示是否将自己注册到Eureka Server
,默认为true
。由于当前应用就是Eureka Server
,故而设置为false
启动服务
启动服务,访问注册中心: http://127.0.0.1:10010/
- 可以看到,
eureka-server
已经启动成功了
服务注册
接下来我们把 product_service
注册到 eureka—server
中
引入 eureka-client 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完善配置文件
spring:
application:
name: product-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka
启动服务
刷新注册中心: http://127.0.0.1:10010/
- 可以看到
product-service
已经注册到eureka
上了
服务发现
接下来我们修改 order-service
,在远程调用时,从 eureka-server
拉取 product-service
的服务信息,实现服务发现
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完善配置文件
服务发现也需要知道 eureka
地址,因此配置内容依然与服务注册一致,都是配置 eureka
信息
spring:
application:
name: order-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka
远程调用
远程调用时,我们需要从 eureka-server
中获取 product-service
的列表(可能存在多个服务),并选择其中一个进行调用
package org.example.order.service;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.order.mapper.OrderMapper;
import org.example.order.model.OrderInfo;
import org.example.order.model.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Slf4j
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Resource
private DiscoveryClient discoveryClient;
@Autowired
private RestTemplate restTemplate;
public OrderInfo selectOrderById(Integer orderId) {
OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
//String url = "http://127.0.0.1:9090/product/" + orderInfo.getProductId();
// 根据应用名称获取服务列表
List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
// 服务可能有多个,获取第一个
EurekaServiceInstance instance = (EurekaServiceInstance) instances.get(0);
log.info(instance.getInstanceId());
// 拼接 URL
String url = instance.getUri() + "/product/" + orderInfo.getProductId();
ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
orderInfo.setProductInfo(productInfo);
return orderInfo;
}
}
启动服务
刷新注册中心: http://127.0.0.1:10010/
- 可以看到
order-service
已经注册到eureka
上了
访问接口: http://127.0.0.1:8080/order/1
可以看到,远程调用也成功了:
Eureka 和 Zookeeper 区别
Eureka
和 Zookeeper
都是用于服务注册和发现的工具,区别如下:
Eureka
是NetFlix
开源的项目,而Zookeeper
是Apache
开源的项目Eureka
基于AP
原则,保证高可用,Zookeeper
基于CP
原则,保证数据一致性Eureka
每个节点都是均等的,Zookeeper
的节点区分Leader
和Follower
或Observer
,也正因为这个原因,如果Zookeeper
的Leader
发生故障时,需要重新选举,选举过程集群会有短暂时间的不可用
更多推荐
所有评论(0)