尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

Spring Cloud - Spring Cloud 注册中心与服务提供者(Spring Cloud Eureka 概述、微服务高效入门、微服务应用实例)

Spring Cloud - Spring Cloud 注册中心与服务提供者(Spring Cloud Eureka 概述、微服务高效入门、微服务应用实例)
📅 发布时间:2026/6/19 21:17:58

一、搭建微服务系统核心中枢

1、服务治理的核心组件
  • 注册中心

  • 服务提供者

  • 服务消费者

2、服务治理概述
功能说明
服务注册分布式系统架构中,每个微服务在启动时,会将自己的信息存储在注册中心
服务发现服务消费者从注册中心查询服务提供者的网络信息,并通过此信息调用服务提供者的接口
  • Spring Cloud 的服务治理可以使用 Eureka 组件
3、注册中心对各个微服务的管理
  • 通过心跳机制,即每隔一定的时间微服务会向注册中心进行汇报,如果注册中心⻓时间无法与某个微服务通信,就会自动销毁该服务

  • 当某个微服务的网络信息发生变化时,会重新注册

4、服务提供者、服务消费者、注册中心的关联
  1. 启动注册中心

  2. 服务提供者启动,在注册中心注册一个可以提供服务的实例

  3. 服务消费者启动,在注册中心订阅需要调用的服务

  4. 注册中心将服务提供者的信息推送给服务调用者

  5. 服务消费者通过相关信息(IP、端口)调用服务提供者的服务

5、注册中心核心模块
  • 服务注册表

  • 服务注册

  • 服务发现

  • 服务检查


二、Spring Cloud Eureka 概述

1、基本介绍
  • 提供服务注册和服务发现功能
2、Spring Cloud Eureka 的组成
* Eureka Server 服务端
* Eureka Client 客户端

三、微服务快速入门

1、父工程
(1)创建父工程
  • 创建 Maven 工程(父工程,不使用模板),在 pom.xml 文件中配置相关依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>SpringCloudTest</artifactId>
<version>1.0-SNAPSHOT</version><!-- Spring Boot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version></parent><dependencies><!-- Spring Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- JDK 9 以上额外配置 --><!--        <dependency>--><!--            <groupId>javax.xml.bind</groupId>--><!--            <artifactId>jaxb-api</artifactId>--><!--            <version>2.3.0</version>--><!--        </dependency>--><!--        <dependency>--><!--            <groupId>com.sun.xml.bind</groupId>--><!--            <artifactId>jaxb-impl</artifactId>--><!--            <version>2.3.0</version>--><!--        </dependency>--><!--        <dependency>--><!--            <groupId>com.sun.xml.bind</groupId>--><!--            <artifactId>jaxb-core</artifactId>--><!--            <version>2.3.0</version>--><!--        </dependency>--><!--        <dependency>--><!--            <groupId>javax.activation</groupId>--><!--            <artifactId>activation</artifactId>--><!--            <version>1.1.1</version>--><!--        </dependency>--></dependencies><!-- Spring Cloud --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
2、注册中心
(1)创建子工程
  • 在父工程目录下创建子工程(Module),实现 Eureka Server
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringCloudTest</artifactId><groupId>com.my</groupId><version>1.0-SNAPSHOT</version></parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurekaserver</artifactId><!-- Eureka Server --><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>
(2)配置文件
  • 在 resources 目录下,创建并配置 application.yaml 配置文件
server:
port: 8761
eureka:
client:
# 是否将当前的 Eureka 服务作为客户端注册
register-with-eureka: false
# 是否获取其他 Eureka 服务
fetch-registry: false
# 注册中心 URL
service-url:
defaultZone: http://localhost:8761/eureka/
(3)启动类
  • 创建并配置启动类
package com.my;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// 注册中心
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
(4)启动注册中心
  • 访问地址:http://localhost:8761/
3、服务提供者
  • 服务提供者和服务消费者都是通过 Eureka Client 连接到 Eureka Server 完成注册
(1)创建子工程
  • 在父工程目录下创建子工程(Module),实现 Eureka Client
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringCloudTest</artifactId><groupId>com.my</groupId><version>1.0-SNAPSHOT</version></parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ServerProvider</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
</project>
(2)配置文件
  • 在 resources 目录下,创建并配置 application.yaml 配置文件
server:
port: 8010
spring:
application:
# 服务名
name: ServerProvider
eureka:
client:
# 注册路径
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
# 是否将 IP 进行注册
prefer-ip-address: true
(3)启动类
  • 创建并配置启动类
package com.my;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServerProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServerProviderApplication.class, args);
}
}
(4)启动服务提供者
  • 在注册中心启动后启动服务提供者,访问注册中心,查看注册的服务消费者

四、微服务应用实例

1、需求
  • 在服务提供者中完成简单的增删改查操作服务
2、具体实现
(1)依赖配置
  • 在父工程中引入 Lombok 简化开发
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
(2)Entity
  • 创建 Student 实体类
package com.my.entity;
import lombok.Data;
@Data
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
}
(3)Repository
  • 创建 StudentRepository 接口
package com.my.repository;
import com.my.entity.Student;
import java.util.Collection;
public interface StudentRepository {
public Collection<Student> findAll();public Student findById(Integer id);public void saveOrUpdate(Student student);public void deleteById(Integer id);}
  • 创建 StudentRepositoryImpl 类,实现 StudentRepository 接口
package com.my.repository.impl;
import com.my.entity.Student;
import com.my.repository.StudentRepository;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository {
private static Map<Integer, Student> map;static {map = new HashMap<>();map.put(1, new Student(1,"张三"));map.put(2, new Student(2,"李四"));map.put(3, new Student(3,"王五"));}@Overridepublic Collection<Student> findAll() {return map.values();}@Overridepublic Student findById(Integer id) {return map.get(id);}@Overridepublic void saveOrUpdate(Student student) {map.put(student.getId(), student);}@Overridepublic void deleteById(Integer id) {map.remove(id);}}
(4)Controller
  • 创建 StudentHandler 类
package com.my.controller;
import com.my.entity.Student;
import com.my.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public Collection<Student> findAll() {return studentRepository.findAll();}@GetMapping("findById/{id}")public Student findById(@PathVariable("id") Integer id) {return studentRepository.findById(id);}@PostMapping("/save")public void save(@RequestBody Student student) {studentRepository.saveOrUpdate(student);}@PutMapping("/update")public void update(@RequestBody Student student) {studentRepository.saveOrUpdate(student);}@DeleteMapping("/deleteById/{id}")public void deleteById(@PathVariable("id") Integer id) {studentRepository.deleteById(id);}}
3、测试
  • 依次启动注册中心和服务消费者,使用接口测试工具 Postman 进行测试
(1)findAll
参数项说明
请求类型GET 请求
访问地址http://localhost:8010/student/findAll
(2)findById
参数项说明
请求类型GET 请求
请求地址http://localhost:8010/student/findById/1
(3)save
参数项说明
请求类型POST 请求
请求地址http://localhost:8010/student/save
  • JSON 数据
{
"id": 4,
"name": "nono"
}
(4)update
参数项说明
PUT请求
请求地址http://localhost:8010/student/update
  • JSON 数据
{
"id": 3,
"name": "jack"
}
(5)deleteById
参数项说明
DELETE请求
请求地址http://localhost:8010/student/deleteById/4

相关新闻

  • DateUtil
  • (链表)判断是否回文
  • (链表)判断两个单链表是否存在交点

最新新闻

  • 修复kkFileView XSS漏洞与POI文件预览兼容性问题实战
  • 弱监督学习与概率提示技术在3D目标检测中的应用
  • Hoppscotch自托管部署与API自动化测试实战指南
  • Qwen3.6-A3B:面向本地Agent的MoE实时推理引擎解析
  • 微信防撤回失效?RevokeMsgPatcher 2.0 技术原理与实战指南
  • 普宁连锁眼镜店哪家靠谱|自营和加盟的本质区别是什么 - 品牌观察

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号