Spring Cloud 微服务全面概述

Spring Cloud 微服务全面概述

1. 微服务架构概述

微服务架构(Microservices Architecture)是将应用程序拆分为多个小的、独立的服务,每个服务围绕特定的业务功能构建。这种架构使得应用程序能够更灵活地扩展和维护。

1.1 微服务的特点

  • 模块化:服务可以独立开发、测试、部署和扩展。
  • 异构技术:不同的服务可以使用不同的技术栈(编程语言、数据库等)。
  • 分布式:服务通常通过 HTTP REST、消息队列等网络协议通信。
  • 弹性:系统能在部分服务故障时保持可用。

1.2 微服务架构的优势

  • 可扩展性:可以根据需求独立扩展某些服务。
  • 开发效率:小团队可以并行开发不同的服务,加快交付速度。
  • 容错性:某个服务的失败不会导致整个系统崩溃。

2. Spring Cloud 介绍

Spring Cloud 是一套基于 Spring Boot 的开源工具,帮助开发者快速构建分布式系统。它提供了多个模块,支持服务发现、配置管理、负载均衡、断路器等功能。

2.1 Spring Cloud 的核心模块

  • Spring Cloud Netflix:提供了与 Netflix 生态系统集成的组件,如 Eureka、Ribbon、Hystrix、Zuul 等。
  • Spring Cloud Config:集中管理服务的配置。
  • Spring Cloud Gateway:API 网关,用于路由和负载均衡。
  • Spring Cloud Sleuth:提供分布式追踪功能。
  • Spring Cloud Bus:用于事件传播和配置更新。

3. Spring Cloud 核心知识点

3.1 服务注册与发现

Eureka 是一个服务注册与发现的组件。它允许微服务在启动时注册到 Eureka 服务器,并可以通过服务名查找其他服务。

3.1.1 Eureka 服务器的设置
  1. 添加依赖
    pom.xml 中添加 Eureka 服务器依赖:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    
  2. 主类配置
    在应用主类上添加 @EnableEurekaServer 注解,启动 Eureka 服务器。

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
    }
    
  3. 配置文件
    application.yml 中配置 Eureka 服务器:

    server:port: 8761
    eureka:client:register-with-eureka: falsefetch-registry: false
    
  4. 启动 Eureka 服务器
    运行应用,访问 http://localhost:8761 查看 Eureka Dashboard。

3.1.2 Eureka 客户端的设置
  1. 添加依赖
    在微服务的 pom.xml 中添加:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-client</artifactId>
    </dependency>
    
  2. 主类配置
    在微服务的主类上添加 @EnableEurekaClient 注解。

    @SpringBootApplication
    @EnableEurekaClient
    public class MyServiceApplication {public static void main(String[] args) {SpringApplication.run(MyServiceApplication.class, args);}
    }
    
  3. 配置文件
    application.yml 中配置 Eureka 客户端信息:

    spring:application:name: my-servicecloud:discovery:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
    
  4. 服务启动
    启动微服务,查看 Eureka Dashboard 可以看到注册的服务。

3.2 负载均衡

Ribbon 是一个客户端负载均衡器,支持服务的自动负载均衡。

3.2.1 Ribbon 的设置
  1. 添加依赖
    在微服务的 pom.xml 中添加:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    
  2. 使用 Ribbon
    在微服务中,可以使用 RestTemplate 发起请求,并利用 Ribbon 进行负载均衡。

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {return new RestTemplate();
    }@GetMapping("/call-service")
    public String callOtherService() {return restTemplate.getForObject("http://my-service/api", String.class);
    }
    

3.3 API 网关

Spring Cloud Gateway 提供了一种简单的方式来路由请求到后端服务,支持负载均衡和安全性。

3.3.1 Gateway 的设置
  1. 添加依赖
    pom.xml 中添加:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    
  2. 配置路由
    application.yml 中配置路由规则:

    spring:cloud:gateway:routes:- id: my-service-routeuri: lb://my-servicepredicates:- Path=/my-service/**
    
  3. 访问网关
    启动网关服务后,通过网关访问后端服务,例如 http://localhost:8080/my-service/api

3.4 配置管理

Spring Cloud Config 提供了集中管理微服务配置的功能。

3.4.1 Config 服务器的设置
  1. 添加依赖
    在 Config 服务器的 pom.xml 中添加:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
  2. 启用 Config 服务器
    在主类上添加 @EnableConfigServer 注解。

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
    }
    
  3. 配置 Git 仓库
    application.yml 中配置 Git 仓库地址:

    spring:cloud:config:server:git:uri: https://github.com/your-repo/config-repo
    
  4. 客户端配置
    微服务客户端在 application.yml 中配置:

    spring:application:name: my-servicecloud:config:uri: http://localhost:8888
    

3.5 断路器

Hystrix 是一个用于服务容错的库,通过断路器模式来保护服务。

3.5.1 Hystrix 的设置
  1. 添加依赖
    在微服务的 pom.xml 中添加:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    
  2. 启用 Hystrix
    在主类上添加 @EnableCircuitBreaker 注解。

    @SpringBootApplication
    @EnableCircuitBreaker
    public class MyServiceApplication {public static void main(String[] args) {SpringApplication.run(MyServiceApplication.class, args);}
    }
    
  3. 使用 Hystrix 注解
    在需要保护的方法上使用 @HystrixCommand 注解:

    @HystrixCommand(fallbackMethod = "fallbackMethod")
    public String callExternalService() {// 可能会失败的外部调用
    }public String fallbackMethod() {return "服务暂时不可用,请稍后再试";
    }
    

好的,我们接着讲解 Spring Cloud Bus 的设置以及其他相关内容。

3.6 消息总线

Spring Cloud Bus 可以用于将配置更改、事件等信息广播到所有服务实例,从而实现服务间的事件传播和配置更新。

3.6.1 消息总线的设置
  1. 添加依赖
    在微服务的 pom.xml 中添加 RabbitMQ 或 Kafka 相关依赖。这里以 RabbitMQ 为例:

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-rabbit</artifactId>
    </dependency>
    
  2. 配置 RabbitMQ
    application.yml 中配置 RabbitMQ 的连接信息:

    spring:rabbit:host: localhostport: 5672username: guestpassword: guest
    
  3. 发送事件
    在微服务中,使用 @RefreshScope 注解来自动刷新配置:

    @RefreshScope
    @RestController
    public class MyController {@Value("${my.property}")private String myProperty;@GetMapping("/property")public String getProperty() {return myProperty;}
    }
    

    当配置发生变化时,发送 POST 请求到 /bus/refresh 来通知所有服务更新配置:

    curl -X POST http://localhost:8888/bus/refresh
    

4. 实际案例

下面是一个简单的微服务案例,演示如何将前面的组件整合在一起。

4.1 项目结构
/microservices/eureka-server/config-server/gateway/service-a/service-b
4.2 Eureka 服务器

使用之前介绍的方法设置 Eureka 服务器。

4.3 Config 服务器

使用之前介绍的方法设置 Config 服务器,并在 Git 仓库中存放配置文件,如 application.yml

4.4 微服务 A 和 B
  1. 创建微服务 A

    @SpringBootApplication
    @EnableEurekaClient
    @EnableCircuitBreaker
    public class ServiceAApplication {public static void main(String[] args) {SpringApplication.run(ServiceAApplication.class, args);}@HystrixCommand(fallbackMethod = "fallbackMethod")public String callServiceB() {// 通过服务名调用服务 Breturn restTemplate.getForObject("http://service-b/endpoint", String.class);}public String fallbackMethod() {return "服务 B 暂时不可用";}
    }
    
  2. 创建微服务 B

    @SpringBootApplication
    @EnableEurekaClient
    public class ServiceBApplication {public static void main(String[] args) {SpringApplication.run(ServiceBApplication.class, args);}@GetMapping("/endpoint")public String endpoint() {return "Hello from Service B";}
    }
    
4.5 Gateway

配置 Gateway,以便路由请求到微服务 A 和 B。

spring:cloud:gateway:routes:- id: service-auri: lb://service-apredicates:- Path=/service-a/**- id: service-buri: lb://service-bpredicates:- Path=/service-b/**

5. 总结

通过使用 Spring Cloud 的一系列组件,你可以快速构建一个健壮的微服务架构,具备服务发现、负载均衡、断路器、配置管理等特性。每个组件都能为你的微服务提供必要的功能,帮助你提高开发效率和系统的可靠性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/57973.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2进制管理器的使用

这个 BinaryDataMgr 是一个用于管理二进制数据的工具类&#xff0c;主要功能是将数据存储为二进制文件&#xff0c;读取二进制数据并将其转换为对象&#xff0c;以及管理与 Excel 表相关的数据。它使用单例模式确保在整个应用程序中只有一个实例。 核心功能概述&#xff1a; …

Javascript基础面试题

仅学习使用&#xff0c;若有侵权将修改或删除|面试鸭 Javascript 有哪些数据类型?它们的区别是什么? 7 原始类型Undefined、Null、Boolean、Number、String、Symbol和BigInt 引用类型&#xff1a;Object(对象、函数和数组等&#xff09; 如何判断 JavaScript 变量是数组? …

《BLEU: a Method for Automatic Evaluation of Machine Translation》翻译

文章目录 0. 摘要1. 引言1.1 理由1.2 观点 2. 基准 BLEU 指标2.1 修正的 n-gram 精度2.1.1 对文本块的修正 n-gram 精度2.1.2 仅使用修正 n-gram 精度对系统进行排序2.1.3 结合修正的 n-gram 精度 2.2 句子长度2.2.1 召回率的问题2.2.2 句子简短惩罚 2.3 BLEU 细节 3. BLEU 评估…

【C#】DevExpress 提供 自定义皮肤功能、注册皮肤库 。SkinManager

DevExpress.Skins.SkinManager.EnableFormSkins(); DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.BonusSkins).Assembly);这两行代码用于启用 DevExpress 提供的自定义皮肤功能&#xff0c;并注册皮肤库&#xff0c;使应用程序能够使用…

java基础全篇(已完结)

完结撒花&#xff01;&#xff01;&#xff01; 在经历了漫长而充实的创作过程后&#xff0c;我终于完成了关于Java基础的全面教程系列。在这个系列中&#xff0c;我们一起走过了从Java环境的搭建到复杂编程概念的掌握&#xff0c;一起吃了各种各样的bug。 章节目录 1.基础篇…

MySQL MHA 的部署

MySQL高可用方案 MHA 什么是 MHA MHA&#xff08;MasterHigh Availability&#xff09;是一种经典的高可用架构&#xff0c;专门用于在主从复制环境中实现自动故障切换和最小化数据丢失。 MHA 作为 MySQL 主从复制环境下的高可用解决方案&#xff0c;具有自动化、低成本和稳定…

JS常用的公共方法

1.获取当前年月日 // 获取当前年月日 export function getNowFormatDate(interval "/") {let date new Date(),year date.getFullYear(), //获取完整的年份(4位)month date.getMonth() 1, //获取当前月份(0-11,0代表1月)strDate date.getDate(); // 获取当前日…

网络安全——防火墙技术

目录 前言基本概念常见防火墙技术防火墙的主要功能防火墙的不足之处相关题目1.组织外部未授权用户访问内部网络2.DMZ区3.包过滤防火墙和代理服务防火墙 前言 这是在软件设计师备考时编写的资料文章&#xff0c;相关内容偏向软件设计师 基本概念 防火墙技术是网络安全领域中的…

如何在Node.js中执行解压缩文件操作

一、解压文件 1.安装依赖&#xff1a; 安装adm-zip依赖包&#xff1a;npm install adm-zip --save 安装iconv-lite依赖包&#xff1a;npm install iconv-lite --save 解压前的file文件夹结构&#xff1a; update-1.0.2.zip压缩包内容&#xff1a; 2.在depresssFile.js文件&…

鸿蒙是必经之路

少了大嘴的发布会&#xff0c;老实讲有点让人昏昏入睡。关于技术本身的东西&#xff0c;放在后面。 我想想来加把油~ 鸿蒙发布后褒贬不一&#xff0c;其中很多人不太看好鸿蒙&#xff0c;一方面是开源性、一方面是南向北向的利益问题。 不说技术的领先点&#xff0c;我只扯扯…

【网络原理】网络地址转换----NAT技术详解

&#x1f490;个人主页&#xff1a;初晴~ &#x1f4da;相关专栏&#xff1a;计算机网络那些事 我们在 IP协议 一文中介绍过&#xff0c;由于IPv4协议中 IP地址只有32位&#xff0c;导致最多只能表示 42亿9千万个IP地址。但我们需要通过IP地址来标识网络上的每一个设备&#x…

【p2p、分布式,区块链笔记 IPFS】go-ipfs windows系统客户端节点实现 kubo试用

Kubo &#xff08;go-IPFS&#xff09; 是最早和使用最广泛的 IPFS 实现。它包括&#xff1a; 一个 IPFS 守护程序服务器广泛的命令行工具用于控制节点的 HTTP RPC API用于向 HTTP 浏览器提供内容的 HTTP 网关 下载 https://dist.ipfs.tech/#go-ipfs 解压 初始化 C:\User…

docker-minio启动参数

完整命令 docker run -p 9000:9000 -p 9090:9090 -v /opt/minio/data:/data -d --name -d --restartalways minio -e "MINIO_ACCESS_KEYminio" -e "MINIO_SECRET_KEYminioadmin123" minio/minio server --console-address ":9090" -address &q…

IDEA开发工具使用技巧积累

一、IDEA 工具设置默认使用maven的settings.xml文件 第一步&#xff1a;打开idea工具&#xff0c;选中 File ——> New Projects Setup ——> Settings for New Projects 第二步&#xff1a;先设置下自动构建项目这个选项 第三步&#xff1a;选中 Build Tools ——>…

正点原子阿尔法ARM开发板-IMX6ULL(九)——关于SecureCRT连接板子上的ubuntu

文章目录 一、拨码器二、SecureCRT 一、拨码器 emmm,也是好久没学IMX6ULL了&#xff0c;也是忘了拨码器决定了主板的启动方式 一种是直接从TF卡中读取文件&#xff08;注意这里是通过imdownload软件编译好了之后&#xff0c;通过指令放入TF卡&#xff09; 一种是现在这种用串口…

日常笔记记录

1、Http 1.1 概念 HTTP 是 HyperText Transfer Protocol&#xff08;超文本传输协议&#xff09;的简写&#xff0c;它是 TCP/IP 协议集中的一个应用层协议&#xff0c;是客户端与服务端进行交互时必须遵循的规则。它用于定义 Web 浏览器与 Web 服务器之间交换数据的过程以及…

Golang | Leetcode Golang题解之第504题七进制数

题目&#xff1a; 题解&#xff1a; func convertToBase7(num int) string {if num 0 {return "0"}negative : num < 0if negative {num -num}s : []byte{}for num > 0 {s append(s, 0byte(num%7))num / 7}if negative {s append(s, -)}for i, n : 0, len…

verilog实现一个5bit序列检测器

以下是用 Verilog 实现一个 5bit 序列检测器的代码&#xff1a; module five_bit_sequence_detector(input clk,input reset,input [4:0] in,output reg detected );// 定义状态参数localparam IDLE 4b0000;localparam STATE1 4b0001;localparam STATE2 4b0010;localparam …

《虚拟现实的边界:探索虚拟世界的未来可能》

内容概要 在虚拟现实&#xff08;VR&#xff09;技术的浪潮中&#xff0c;我们见证了其从实验室的奇想逐渐走向日常生活的非凡旅程。技术发展的背后是不断突破的创新&#xff0c;早期的设备虽然笨重&#xff0c;但如今却趋向精致、轻巧&#xff0c;用户体验显著提升。想象一下…

ELK Stack与Graylog:强大的日志分析和可视化工具

ELK Stack的使用方法 ELK Stack由Elasticsearch、Logstash和Kibana三个核心组件组成&#xff0c;它们协同工作&#xff0c;提供了从日志收集、解析、存储到可视化的完整解决方案。 安装与配置Elasticsearch Elasticsearch是ELK Stack的存储和查询引擎&#xff0c;负责存储日…