在SpringBoot中怎么使用SpringCloud(3)
wptr33 2024-12-07 17:45 34 浏览
在 Spring Boot 中使用 Spring Cloud,主要是通过集成 Spring Cloud 提供的一些功能来构建微服务应用。Spring Cloud 提供了一些有用的组件来支持微服务架构,如服务注册与发现、负载均衡、API 网关、配置管理、断路器等。
下面是如何在 Spring Boot 项目中使用 Spring Cloud 的基本步骤。
1.设置 Spring Boot 项目
首先,确保你已经创建了一个 Spring Boot 项目。你可以使用 Spring Initializr(https://start.spring.io/)来快速生成一个 Spring Boot 项目,选择 Spring Web 和 Spring Boot DevTools 等常见的依赖。
2.添加 Spring Cloud 依赖
在 pom.xml 或 build.gradle 文件中添加 Spring Cloud 相关的依赖。
使用 Maven:
在 pom.xml 文件中,添加 Spring Cloud 相关的依赖,并指定 Spring Cloud 的版本。在 Spring Boot 2.x 和 Spring Cloud 2020.x 版本中,Spring Cloud 提供了一个 BOM(Bill of Materials)来管理依赖的版本。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.4</version> <!-- 选择适合的版本 -->
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 选择你需要的Spring Cloud组件,例如服务注册与发现 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Actuator(用于监控) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
查看全部
使用 Gradle:
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2020.0.4" // 选择适合的版本
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
3.启用 Spring Cloud 组件
在 application.properties 或 application.yml 配置文件中启用 Spring Cloud 相关组件的功能。
服务注册与发现:Eureka 客户端
例如,如果你想使用 Eureka 作为服务注册与发现框架,你需要添加 @EnableEurekaClient 注解,并配置 Eureka 的相关属性。
在application.yml中配置 Eureka:
spring:
application:
name: my-service
cloud:
discovery:
enabled: true
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka # Eureka 服务器地址
在 Spring Boot 主类上启用 Eureka 客户端:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // 启用服务发现功能
public class MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceApplication.class, args);
}
}
服务注册与发现:Eureka 服务器
如果你也想启动一个 Eureka 服务器,可以创建另一个 Spring Boot 项目,并配置为 Eureka 服务器。
在application.yml中配置 Eureka 服务器:
server:
port: 8761
spring:
application:
name: eureka-server
cloud:
eureka:
server:
enable-self-preservation: false # 禁用自我保护机制(方便开发测试)
client:
register-with-eureka: false # 该应用作为 Eureka 服务器,不注册自己
instance:
hostname: localhost
启动 Eureka 服务器:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 启用 Eureka 服务器功能
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4.API 网关:Spring Cloud Gateway
如果你想为微服务应用设置一个 API 网关(统一的入口点),你可以使用 Spring Cloud Gateway。它可以帮助你处理请求路由、认证、限流等。
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
配置 Gateway 路由规则:
在 application.yml 中配置网关路由:
spring:
cloud:
gateway:
routes:
- id: my-service-route
uri: lb://MY-SERVICE # 微服务的名称(Eureka 服务名)
predicates:
- Path=/my-service/** # 以 `/my-service` 开头的请求都会转发到该服务
5.分布式配置管理:Spring Cloud Config
Spring Cloud Config 提供了集中式配置管理服务,允许将配置文件存储在 Git 仓库或文件系统中,微服务应用可以从 Config Server 拉取配置。
配置 Spring Cloud Config 服务器:
- 添加 Spring Cloud Config 服务器依赖:
- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
- 启动 Config Server:
- import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer // 启用 Config Server public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
- 在 application.yml 中配置 Config Server 的 Git 仓库路径:
- spring: cloud: config: server: git: uri: https://github.com/my-repo/config-repo # 配置文件的 Git 仓库地址 search-paths: '{application}' # 按服务名查找配置
配置 Spring Cloud Config 客户端:
- 添加 spring-cloud-starter-config 依赖:
- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- 配置客户端获取配置:
- spring: application: name: my-service # 从 Config Server 获取与该名称相关的配置 cloud: config: uri: http://localhost:8888 # Config Server 地址
6.断路器:Hystrix
Spring Cloud 集成了 Netflix Hystrix 作为断路器,用于处理服务间的调用故障。
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
启用 Hystrix 注解:
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@HystrixCommand(fallbackMethod = "fallbackMethod")
public String callExternalService() {
// 外部服务调用
return "Success";
}
public String fallbackMethod() {
return "Fallback response";
}
}
7.启动微服务
- 启动 Eureka 服务器。
- 启动服务提供者(如 MyServiceApplication)。
- 启动 API 网关(如 GatewayApplication),通过网关访问微服务。
通过 Spring Cloud 你可以非常方便地在 Spring Boot 中构建一个完整的微服务架构。通过配置服务发现、负载均衡、API 网关、分布式配置等功能,能够解决许
相关推荐
- redis的八种使用场景
-
前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...
- 基于Redis的3种分布式ID生成策略
-
在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...
- 基于OpenWrt系统路由器的模式切换与网页设计
-
摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...
- 这篇文章教你看明白 nginx-ingress 控制器
-
主机nginx一般nginx做主机反向代理(网关)有以下配置...
- 如何用redis实现注册中心
-
一句话总结使用Redis实现注册中心:服务注册...
- 爱可可老师24小时热门分享(2020.5.10)
-
No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...
- Apportable:拯救程序员,IOS一秒变安卓
-
摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...
- JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透
-
以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...
- 3月26日更新 快速施法自动施法可独立设置
-
2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...
- Redis 是如何提供服务的
-
在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...
- lua _G、_VERSION使用
-
到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...
- China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
-
BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...
- 移动工作交流工具Lua推出Insights数据分析产品
-
Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...
- Redis 7新武器:用Redis Stack实现向量搜索的极限压测
-
当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...
- Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求
-
重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)