北京电商网站开发平台,WordPress中英文旅游模板,常州制作网站价格,企业即时通讯平台SpringCloud Alibaba 实战#xff1a;搭建第一个 SpringCloud Alibaba 项目
引言
在现代微服务架构中#xff0c;SpringCloud 已经成为开发者构建分布式系统的首选工具之一。SpringCloud Alibaba 是 SpringCloud 生态中的一个重要子集#xff0c;提供了一整套微服务开发的…SpringCloud Alibaba 实战搭建第一个 SpringCloud Alibaba 项目
引言
在现代微服务架构中SpringCloud 已经成为开发者构建分布式系统的首选工具之一。SpringCloud Alibaba 是 SpringCloud 生态中的一个重要子集提供了一整套微服务开发的解决方案集成了阿里巴巴的中间件和服务。这篇博客将带领大家一步一步搭建一个简单的 SpringCloud Alibaba 项目帮助开发者快速上手。
目录
SpringCloud Alibaba 简介项目准备 环境要求工具安装 创建 SpringCloud Alibaba 项目 使用 Spring Initializr 初始化项目项目结构分析 引入 SpringCloud Alibaba 依赖 Maven 依赖配置 搭建服务注册中心Nacos Nacos 简介Nacos 安装与配置 实现服务消费者与提供者 创建服务提供者创建服务消费者服务调用测试 配置网关Spring Cloud Gateway 网关简介网关配置与路由规则 服务降级与熔断Sentinel Sentinel 简介配置与使用示例 配置中心Nacos Config 动态配置管理 总结与展望
SpringCloud Alibaba 简介
SpringCloud Alibaba 是一个为微服务解决方案提供全面支持的工具集。它集成了阿里巴巴的开源技术栈如 Nacos、Sentinel、RocketMQ 等旨在简化微服务架构的构建与管理。其主要特点包括
服务注册与发现通过 Nacos 实现服务注册与发现。负载均衡与路由使用 Spring Cloud Gateway 提供 API 网关功能。限流与熔断Sentinel 提供了流量控制和熔断降级功能。配置管理Nacos Config 允许动态配置管理便于服务的配置更新。
项目准备
环境要求
在开始搭建项目之前需要确保本地环境满足以下要求
Java 11 或以上Maven 3.6.0 或以上IDEA 或 Eclipse 开发工具
工具安装 Java 安装从 Oracle 或 OpenJDK 下载并安装 JDK 11。 Maven 安装从 Maven 官方网站 下载并安装 Maven。 IDEA/Eclipse 安装推荐使用 IDEA下载地址JetBrains IDEA
创建 SpringCloud Alibaba 项目
使用 Spring Initializr 初始化项目
Spring Initializr 是 Spring 官方提供的项目初始化工具能够快速生成项目结构。 访问 Spring Initializr选择如下配置 ProjectMaven ProjectLanguageJavaSpring Boot2.7.xProject Metadata Groupcom.exampleArtifactspringcloud-alibaba-demoNameSpringCloudAlibabaDemoPackage namecom.example.springcloudalibabademoPackagingJarJava11 Dependencies Spring WebSpring Cloud Alibaba Nacos DiscoverySpring Cloud Alibaba Nacos ConfigSpring Cloud GatewaySpring Cloud Alibaba Sentinel
点击“Generate”生成项目并下载。
项目结构分析
解压并导入项目后项目结构大致如下
springcloud-alibaba-demo
│ pom.xml
└───src├───main│ ├───java│ │ └───com.example.springcloudalibabademo│ │ └───SpringCloudAlibabaDemoApplication.java│ └───resources│ ├───application.properties│ └───static└───test└───java引入 SpringCloud Alibaba 依赖
Maven 依赖配置
在 pom.xml 中添加 SpringCloud Alibaba 相关依赖
dependencyManagementdependenciesdependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion2021.1/versiontypepom/typescopeimport/scope/dependency/dependencies
/dependencyManagementdependencies!-- Spring Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Nacos Discovery --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- Nacos Config --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId/dependency!-- Spring Cloud Gateway --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependency!-- Sentinel --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependency
/dependencies搭建服务注册中心Nacos
Nacos 简介
Nacos 是一个易于使用的动态服务发现、配置和服务管理平台。它帮助开发者快速实现微服务架构中的注册、配置与发现功能。
Nacos 安装与配置 下载 NacosNacos Releases 启动 Nacos 解压后进入 bin 目录执行启动命令 Windowsstartup.cmd -m standaloneLinux/Unix/Macsh startup.sh -m standalone 访问控制台 启动成功后访问 http://localhost:8848/nacos 进入 Nacos 控制台。
实现服务消费者与提供者
创建服务提供者 在项目中创建一个模块 provider并添加以下代码 pom.xml dependencies!-- Spring Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Nacos Discovery --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency
/dependenciesapplication.properties server.port8081
spring.application.nameservice-provider
spring.cloud.nacos.discovery.server-addrlocalhost:8848ProviderApplication.java package com.example.springcloudalibabademo.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;SpringBootApplication
RestController
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}GetMapping(/hello)public String hello() {return Hello from Provider!;}
}创建服务消费者 在项目中创建一个模块 consumer并添加以下代码 pom.xml dependencies!-- Spring Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Nacos Discovery --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- OpenFeign --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency
/dependenciesapplication.properties server.port8082
spring.application.nameservice-consumer
spring.cloud.nacos.discovery.server-addrlocalhost:8848ConsumerApplication.java package com.example.springcloudalibabademo.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;SpringBootApplication
EnableFeignClients
RestController
public class ConsumerApplication {Autowiredprivate ProviderClient providerClient;public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}GetMapping(/hello)public String hello() {return providerClient.hello();}
}ProviderClient.java package com.example.springcloudalibabademo.consumer;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;FeignClient(name service-provider)
public interface ProviderClient {GetMapping(/hello)String hello();
}服务调用测试
启动服务提供者和消费者模块。访问消费者服务的接口http://localhost:8082/hello返回结果应为“Hello from Provider!”
配置网关Spring Cloud Gateway
网关简介
Spring Cloud Gateway 是 Spring 官方提供的 API 网关解决方案提供路由、断言和过滤器等功能支持对微服务进行统一管理。
网关配置与路由规则 在项目中创建一个模块 gateway并添加以下代码 pom.xml dependencies!-- Spring Cloud Gateway --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependency!-- Nacos Discovery --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency
/dependenciesapplication.properties server.port8080
spring.application.namegateway
spring.cloud.nacos.discovery.server-addrlocalhost:8848
spring.cloud.gateway.discovery.locator.enabledtrue
spring.cloud.gateway.discovery.locator.lower-case-service-idtrueGatewayApplication.java package com.example.springcloudalibabademo.gateway;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}配置路由规则 在 application.properties 中添加路由规则使请求 /api/hello 路由到 service-consumer 服务 spring.cloud.gateway.routes[0].idconsumer
spring.cloud.gateway.routes[0].urilb://service-consumer
spring.cloud.gateway.routes[0].predicates[0]Path/api/hello启动网关模块访问 http://localhost:8080/api/hello应返回“Hello from Provider!”
服务降级与熔断Sentinel
Sentinel 简介
Sentinel 是阿里巴巴开源的一款流量防护和熔断降级解决方案用于保护服务的稳定性和可用性。
配置与使用示例 在消费者模块中添加 Sentinel 依赖 pom.xml dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependencyConsumerApplication.java 修改 ProviderClient 接口以支持熔断降级 FeignClient(name service-provider, fallback ProviderClientFallback.class)
public interface ProviderClient {GetMapping(/hello)String hello();
}Component
public class ProviderClientFallback implements ProviderClient {Overridepublic String hello() {return Fallback: Service Unavailable;}
}启动消费者模块停止提供者服务访问 http://localhost:8082/hello应返回“Fallback: Service Unavailable”。
配置中心Nacos Config
动态配置管理 在 Nacos 控制台添加配置 Data IDservice-provider-dev.yamlGroupDEFAULT_GROUPContentserver:port: 8081在服务提供者的 application.properties 中启用 Nacos Config spring.profiles.activedev
spring.cloud.nacos.config.server-addrlocalhost:8848
spring.cloud.nacos.config.file-extensionyaml启动服务提供者查看是否读取了 Nacos 配置的端口号。
总结与展望
通过本次实践我们成功搭建了一个简单的 SpringCloud Alibaba 项目涵盖了服务注册与发现、网关路由、服务降级与熔断以及配置中心等核心功能。SpringCloud Alibaba 提供了一整套解决方案帮助开发者轻松构建和管理微服务架构。在未来的开发中可以进一步探索更多高级功能如分布式事务Seata、消息队列RocketMQ等构建更加复杂和健壮的微服务系统。 通过这种方式读者可以逐步理解和实践 SpringCloud Alibaba 的核心功能从而在真实项目中应用这些技术。希望这篇博客能够为您的开发旅程提供帮助如果有任何疑问或建议欢迎在评论区留言。