Spring Cloud之注册中心之Eureka

目录

Eureka

[搭建Eureka Server](#搭建Eureka Server)

创建Eureka-server子模块

引入eureka-server依赖

项目构建插件

完善启动类

编写配置文件

启动服务并访问

服务注册

引入eureka-client依赖

完善配置文件

启动服务并访问

服务发现

引入依赖

完善配置文件

启动服务并访问

Eureka和Zookeeper的区别


Eureka

Eureka是Netflix OSS套件中关于服务注册和发现的解决⽅案. Spring Cloud对Eureka进⾏了集成, 并作为优先推荐⽅案进⾏宣传, 虽然⽬前Eureka 2.0已经停⽌维护, 新的微服务架构设计中, 也不再建议使⽤, 但是⽬前依然有⼤量公司的微服务系统使⽤Eureka作为注册中心。

Eureka主要分为两个部分:

Eureka Server: 作为注册中⼼Server端, 向微服务应⽤程序提供服务注册, 发现, 健康检查等能⼒.
Eureka Client: 服务提供者, 服务启动时, 会向Eureka Server 注册⾃⼰的信息(IP,端⼝,服务信息等),Eureka Server 会存储这些信息。

关于Eureka的学习, 主要包含以下三个部分:

  1. 搭建Eureka Server

  2. 将order-service, product-service 都注册到Eureka

  3. order-service远程调⽤时, 从Eureka中获取product-service的服务列表, 然后进⾏交互

搭建Eureka Server

Eureka-server 是⼀个独⽴的微服务.

创建Eureka-server子模块
引入eureka-server依赖
复制代码
<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>
完善启动类
java 复制代码
package 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);
    }
}
编写配置文件
java 复制代码
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/
logging:
  pattern:
    console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
启动服务并访问

可以看到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

完整的配置文件

复制代码
server:
  port: 9090
spring:
  application:
    name: product-service
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/cloud_product?characterEncoding=utf8&useSSL=false
    username: root
    password: #密码

    driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:
  configuration: # 配置打印 MyBatis 执行的 SQL
    #    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true  #自动驼峰转换
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10010/eureka/
logging:
  pattern:
    console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
启动服务并访问

可以看到product-service已经注册到 eureka上了。

服务发现

接下来我们修改order-service, 在远程调⽤时, 从eureka-server拉取product-service的服务信息, 实现服务发现。

引入依赖

服务注册和服务发现都封装在eureka-client依赖中, 所以服务发现时, 也是引⼊eureka-client依赖。

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完善配置文件

添加如下配置

复制代码
spring:
  application:
  name: order-service
eureka:
  client:
  service-url:
  defaultZone: http://127.0.0.1:10010/eureka

完整的配置文件

复制代码
server:
  port: 8080
spring:
  application:
    name: order-service
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/cloud_order?characterEncoding=utf8&useSSL=false
    username: root
    password: #密码
    driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:
  configuration: # 配置打印 MyBatis 执行的 SQL
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true  #自动驼峰转换
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10010/eureka/
logging:
  pattern:
    console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
启动服务并访问

可以看到order-service已经注册到 eureka上了。

可以看到, 远程调⽤也成功了。

Eureka和Zookeeper的区别

Eureka和Zookeeper都是⽤于服务注册和发现的⼯具,区别如下:

  1. Eureka是Netflix开源的项⽬, ⽽Zookeeper是Apache开源的项⽬.

  2. Eureka 基于AP原则, 保证⾼可⽤, Zookeeper基于CP原则, 保证数据⼀致性.

  3. Eureka 每个节点 都是均等的, Zookeeper的节点区分Leader 和Follower 或 Observer, 也正因为这个原因, 如果Zookeeper的Leader发⽣故障时, 需要重新选举, 选举过程集群会有短暂时间的不可⽤。

相关推荐
李白的天不白10 小时前
404问题 请求根本没到 Nginx 图片服务器
eureka
小肥君12 小时前
docker无法连接GPU资源解决方案
docker·容器·eureka
心之伊始13 小时前
Java 后端接入大模型:从 Token、并发到推理成本的完整估算方法
java·spring boot·性能优化·大模型·llm
BlackTurn13 小时前
技术经理投标
java
YG亲测源码屋13 小时前
java配置环境变量、jdk环境变量配置、java环境变量设置方法
java·开发语言
MIUMIUKK13 小时前
从语法层面,看懂 Python 的特殊处
java·开发语言·python
hujinyuan2016014 小时前
2026年3月 中国电子学会青少年软件编程(Python)三级考试试卷 真题及答案
java·python·算法
basketball61614 小时前
C++ 高级编程:2. 基本线程池实现
java·开发语言·c++
MageGojo14 小时前
天气 API 接入实战:基于 ApiZero 实现实时天气、分钟级降水和 15 天预报查询
java·后端·spring·api 接口接入·接口实战
自动跟随15 小时前
UWB自动跟随技术全栈解析:从定位算法到“位控一体化“
java·网络·人工智能