Spring Cloud 整合教程
Spring Cloud 是一个用于构建分布式系统的工具集,基于 Spring Boot 提供了一系列微服务解决方案。以下是一个完整的 Spring Cloud 整合教程,涵盖核心组件和配置步骤。
环境准备
- JDK 1.8 或更高版本
- Maven 3.x 或 Gradle
- Spring Boot 2.x
- Spring Cloud 最新稳定版本(如 Hoxton.SR12)
核心组件整合
服务注册与发现(Eureka)
-
添加依赖
在pom.xml中添加 Eureka Server 依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> -
启用 Eureka Server
在主类上添加@EnableEurekaServer注解:@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } -
配置 Eureka Server
在application.yml中配置:server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false
服务提供者(Eureka Client)
-
添加依赖
在pom.xml中添加 Eureka Client 依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> -
启用 Eureka Client
在主类上添加@EnableEurekaClient注解:@SpringBootApplication @EnableEurekaClient public class ServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(ServiceProviderApplication.class, args); } } -
配置服务注册
在application.yml中配置:spring: application: name: service-provider eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
负载均衡(Ribbon)
-
添加依赖
Ribbon 已集成在 Eureka Client 中,无需额外添加依赖。 -
使用 RestTemplate 调用服务
通过@LoadBalanced注解启用负载均衡:@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } -
调用其他服务
使用服务名替代具体 IP 地址:String url = "http://service-provider/api/resource"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
声明式服务调用(Feign)
-
添加依赖
在pom.xml中添加 OpenFeign 依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> -
启用 Feign
在主类上添加@EnableFeignClients注解:@SpringBootApplication @EnableFeignClients public class FeignClientApplication { public static void main(String[] args) { SpringApplication.run(FeignClientApplication.class, args); } } -
定义 Feign 接口
通过接口声明服务调用:@FeignClient(name = "service-provider") public interface ServiceProviderClient { @GetMapping("/api/resource") String getResource(); }
断路器(Hystrix)
-
添加依赖
在pom.xml中添加 Hystrix 依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> -
启用 Hystrix
在主类上添加@EnableHystrix注解:@SpringBootApplication @EnableHystrix public class HystrixApplication { public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); } } -
定义降级方法
使用@HystrixCommand注解:@HystrixCommand(fallbackMethod = "fallbackMethod") public String callService() { // 调用远程服务 } public String fallbackMethod() { return "Fallback response"; }
网关(Zuul 或 Spring Cloud Gateway)
-
添加依赖
以 Spring Cloud Gateway 为例:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> -
配置路由规则
在application.yml中配置:spring: cloud: gateway: routes: - id: service-provider uri: lb://service-provider predicates: - Path=/api/** -
启用网关
无需额外注解,Spring Boot 自动配置。
配置中心(Spring Cloud Config)
-
添加依赖
在pom.xml中添加 Config Server 依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> -
启用 Config Server
在主类上添加@EnableConfigServer注解:@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } -
配置 Git 仓库
在application.yml中配置:spring: cloud: config: server: git: uri: https://github.com/your-repo/config-repo
分布式追踪(Sleuth + Zipkin)
-
添加依赖
在pom.xml中添加 Sleuth 和 Zipkin 依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> -
配置 Zipkin 服务器
在application.yml中配置:spring: zipkin: base-url: http://localhost:9411 sleuth: sampler: probability: 1.0
通过以上步骤,可以快速搭建一个完整的 Spring Cloud 微服务架构。根据实际需求,可以进一步扩展或替换组件(如使用 Nacos 替代 Eureka)。
6213

被折叠的 条评论
为什么被折叠?



