当前位置: 首页 > news >正文

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

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

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)创建子工程
<?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、服务提供者
(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
{
"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
http://www.rkmt.cn/news/51477.html

相关文章:

  • DateUtil
  • (链表)判断是否回文
  • (链表)判断两个单链表是否存在交点
  • (链表)任意删除一个结点
  • 在抖音直播推广开源作品的可行性?
  • DLSS Swapper商业模式:开源软件商业化探索 - 指南
  • irm steam.work|iex 风险分析
  • Pandas --DataFrame基本操作
  • 2025年11月全国旗杆厂家综合实力排行榜TOP5权威发布
  • 入侵防护技术深度解析:最新漏洞与威胁态势
  • 20232427 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • 解决Elctron打包成功,IPC无法注册问题。
  • 在Windows系统置顶窗口不被Win+D快捷键影响
  • 点分树
  • HTTP请求走私漏洞介绍 - 实践
  • 深入解析:Spring MVC 拦截器interceptor
  • 《重生之我成为世界顶级黑客》第八章:未来野望
  • 打开工作空间时,但未在 DTD/架构中声明
  • 从 LLM 到 Agentic AI:构建下一代智能平台的全栈路径
  • 20232418 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • Claude Code教程:从零构建AutoPost GPT自动内容生成系统
  • python多进程 —— multiprocessing.Manager —— 跨主机共享内存的读写
  • 详细介绍:UVa 11129 An Antiarithmetic Permutation
  • 3天掌握OpenHarmony+Python开发:高效适配教程与真实项目案例精讲 - 教程
  • 25.11.15随笔联考总结(补)
  • 《重生之我成为世界顶级黑客》第六章:一线生机
  • 遥感建筑物变化检测内容集
  • 【UE源码向】GameplayTag增加ToolTip
  • 基于c++ eigen的Nelder-Mead算法(仿照scipy)
  • 2D3D-MATR论文学习